---
name: opencognitos-safe-git
title: safe git
kind: skill
description: >
  Rules for committing, branching and pushing safely as a coding agent. Use
  before any git commit, push, merge, rebase or history rewrite, and when the
  user asks to "ship", "commit" or "push" changes.
updated: 2026-12-05
authored_by: OpenCognitOS
author_url: https://github.com/OpenCognitOS
source_url: https://github.com/OpenCognitOS/skills/tree/HEAD/safe-git
brought_by: kt
---

# Safe git

Git mistakes made by agents are expensive: lost work, leaked secrets, broken main branches. Follow these rules for every git operation.

## Before you commit

1. Run `git status` and `git diff --staged`. Read what you are about to commit.
2. Stage files by name (`git add path/to/file`). Avoid `git add -A` and `git add .` unless you checked every untracked file.
3. Never commit:
   - `.env`, `.env.*` (except `.env.example`), `*.pem`, `*.key`, `id_rsa*`, `credentials.json`, `*.p12`, service-account files
   - local settings such as `.claude/settings.local.json`, `.vscode/` with personal paths
   - build output, `node_modules/`, large binaries, database files
4. Scan the staged diff for secrets: API keys (`sk-`, `ghp_`, `AKIA`, `xox`), private key headers, passwords in URLs, tokens in config. If you find one, unstage the file and tell the user. Do not "fix" it by committing a removal: the secret stays in history.
5. Run the project's tests, linter and type check if they exist and are fast. Do not commit red builds without telling the user.

## Commit messages

- One commit per logical change. Do not mix a refactor with a bug fix.
- Subject line: imperative, at most ~72 characters, says *what* changes ("Fix token refresh on expired sessions").
- Body (optional): *why* the change was needed and anything a reviewer must know.
- Follow the repository's existing style and language (`git log --oneline -20`).

## Branches

- Never commit directly to `main`/`master` unless the user asked for it. Create a branch: `git switch -c feature/<short-name>` or `fix/<short-name>`.
- Keep branches small and short-lived.

## Pushing and rewriting history

- Push only when the user asked for it.
- Never use `git push --force` on shared branches. If a rewrite is really needed on your own branch, use `git push --force-with-lease` and say so.
- Never run `git reset --hard`, `git clean -fd`, `git checkout -- .` or `git stash drop` without explicit permission: they destroy uncommitted work, possibly the user's.
- Never rewrite history that is already on the remote (`rebase`, `commit --amend`, `filter-branch`) without asking.
- Do not skip hooks (`--no-verify`) to get a commit through. Fix the cause or report it.

## When something goes wrong

- Merge conflict: resolve only the conflicting hunks, keep both sides' intent, run tests, and tell the user which files you resolved.
- Wrong commit on main (not pushed): create a branch at the current commit, then ask before moving `main` back.
- Pushed a secret: stop, tell the user immediately. The key must be revoked; deleting it from history is not enough.
