Hands-On: Amend and Rewrite

Updated

October 30, 2026

Hands-On: Amend and Rewrite a Commit

Practice on ~/desk/desk-api. If that tree is missing, run chapter 3’s tiny-project page first.

 commit "stuff"     A ← B ← C
 amend subject      A ← B ← C'     C in reflog only
 amend add file     A ← B ← C''    tree now includes version.py

1. A sloppy commit

cd ~/desk/desk-api

echo "Returns a fixed string." >> README.md
git add README.md
git commit -m "stuff"
git log -1 --oneline

2. Fix the subject

git commit --amend -m "docs: note that ping returns a fixed string"
git log -1 --format='%h %s'

The hash changed. The tree is the same if you staged nothing else.

3. Forgot a file

cat > src/version.py <<'EOF'
VERSION = "0.1.0"
EOF
git add src/version.py
git commit --amend --no-edit
git show --name-only --oneline HEAD

version.py is now in the same commit as the docs line.

4. Staged too much, then unstaged

echo "WIP" >> src/ping.py
echo "also" >> README.md
git add -A
git restore --staged src/ping.py
git diff --staged
git commit --amend --no-edit
git restore src/ping.py
git status

The WIP line is gone. README’s “also” is in HEAD if it was staged. Status is clean.

5. Confirm the graph

git log --oneline -5
git status

You should be on main, clean, with a conventional subject on HEAD. Next chapter: branches, so this main history can grow a second line of work.