Lessons · Git · amend replaces, it does not edit
Amend replaces, it does not edit
Commits cannot be changed, so --amend builds a new one and moves the branch to it. The hash is different, and the original is still in the repository.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
It explains both halves: why amending after a push causes trouble, and why the commit you amended away is always recoverable.
How to think about it
Amend freely before you push. After, treat it exactly like reset -- it is the same rewrite with a friendlier name.
Worked example
git log --onelineOne commit, with a typo in its message.
git rev-parse HEADIts hash. Remember the first few characters.
git commit -q --amend -m 'a message that reads properly'Fixed -- or rather, replaced.
git rev-parse HEADA different hash. This is a different commit.
git log --onelineStill one commit, so the old one is unreachable.
git reflogAnd there it is, with the typo, exactly where it was.
Your turn
Replace the last commit with one that has a better message.
git commit --amend 'a message that reads properly'
Try it in a real repository
The trap
Amending a pushed commit and pushing again. The remote refuses, and the temptation at that moment is --force.
Practise amend replaces, it does not edit on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.