Git write

From ferdinandyb/dotfiles · View interactive version

Git write is an AI agent skill from the GitHub repository ferdinandyb/dotfiles. Load this skill when performing git operations that modify repository state: add, commit, amend, rebase, merge, stash, worktree, reset, checkout, switch, branch create/delete.

How to use this skill

Skill file

Git Write Operations Best Practices

Core Principles

Atomic Commits

the code

separate

if they're logically distinct changes

In other words: a commit must be able to become a fully functional pull/merge

request on it's own if needed.

Commit Message Format

Title

conventional commit prefixes.

nouns and acronyms. Example: accounts: retire obsolete features.

Body

machine (and different platform if applicable)

Fine-Grained Staging

Never stage entire files blindly. Agents cannot answer git add -p prompts

or use an editor. Stage part of a file like this:

  1. Run git diff <path> to see the hunks and their order.
  2. If the wanted and unwanted changes are in separate hunks, pipe the answers

in hunk order (git reset -p works the same way for unstaging):

```bash

printf 'y\nn\n' | git add -p <path>

```

  1. If they are in the same hunk (for example, adjacent added lines), use

git add -e with GIT_EDITOR set to a sed command. Delete unwanted +

lines; replace the leading - with a space on unwanted - lines. Match by

content, not line numbers. Git applies the result with --recount, so hunk

headers need no changes (macOS requires sed -i '', Linux uses sed -i):

```bash

GIT_EDITOR="sed -i '' '/^+.*role: firewall/d'" git add -e <path>

GIT_EDITOR="sed -i '' -e '/^+TEXT/d' -e '/^-TEXT/s/^-/ /'" git add -e -- <path>

```

These exact forms run without approval: double-quoted GIT_EDITOR, only

/^+TEXT/d and /^-TEXT/s/^-/ / scripts, literal paths. TEXT must not

contain quotes, /, \, ;, $ or backticks; use . for such

characters. Any other editor value asks the user.

  1. Always check the result with git diff --cached.

Do not write patch files by hand for git apply --cached. Commit a finished

unit of work before you start an unrelated change, so hunks do not mix.

Rewriting History

Before pushing to shared branches, actively rewrite history to create clean atomic commits.

Amending the Last Commit

git add <files>
git commit --amend           # edit message
git commit --amend --no-edit # keep message

Fixup Commits (for older commits)

# Create a fixup commit targeting a specific commit
git add <files>
git commit --fixup=<commit-hash>

# Later, autosquash during rebase, does not use $EDITOR
git rebase --autosquash origin/main

Splitting a Commit

# Reset to previous commit, keeping changes in working directory
git reset HEAD^

# Or soft reset to keep changes staged
git reset --soft HEAD^

# Then re-stage selectively with git add -p

Programmatic Rebase (No Editor)

Agents cannot use git rebase -i because it opens an interactive editor.

Instead, use GIT_SEQUENCE_EDITOR to manipulate the rebase plan

programmatically:

# Squash the last 2 commits into one
GIT_SEQUENCE_EDITOR="sed -i '2s/^pick/squash/'" git rebase -i HEAD~2

# Drop the 3rd-to-last commit
GIT_SEQUENCE_EDITOR="sed -i '3s/^pick/drop/'" git rebase -i HEAD~3

# Reword a commit (then follow up with git commit --amend -m "new message")
GIT_SEQUENCE_EDITOR="sed -i '1s/^pick/reword/'" git rebase -i HEAD~1

# Fixup all commits except the first (squash without keeping messages)
GIT_SEQUENCE_EDITOR="sed -i '2,\$s/^pick/fixup/'" git rebase -i HEAD~4

# Apply --autosquash (processes fixup!/squash! commits automatically)
git rebase --autosquash origin/main

Available actions: pick, reword, edit, squash, fixup, drop

The GIT_SEQUENCE_EDITOR command receives the todo file path as its argument.

The GIT_SEQUENCE_EDITOR command receives the todo file path as its argument. Use sed -i with one or more -e expressions (macOS requires sed -i '', Linux uses sed -i):

# Single change (macOS)
GIT_SEQUENCE_EDITOR="sed -i '' '2s/^pick/squash/'" git rebase -i HEAD~3

# Multiple changes — chain with -e
GIT_SEQUENCE_EDITOR="sed -i '' -e '2s/^pick/squash/' -e '3s/^pick/squash/' -e '4s/^pick/drop/'" git rebase -i HEAD~5

Line numbers are 1-indexed; line 1 = oldest commit in the range.

Recovering After Rebase

Workflow Summary

  1. Make changes in working directory
  2. Stage selectively as in Fine-Grained Staging (not git add . or git add -A)
  3. Commit atomically with good message explaining WHY
  4. Amend/fixup as you iterate
  5. Rebase and clean up before pushing to shared branches

Commands That Open $EDITOR

Some git commands open $EDITOR for interactive input. Agents CANNOT run these commands directly because they require interactive text editor input.

Examples:

Workarounds:

General Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.