Hone

Lessons · Git · redoing the last commit

Fixing the last commit

git commit --amend replaces the last commit with a new one, to fix its message or add a forgotten file. It has a new hash: amend rewrites, it does not edit.

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

The typo in the message, the file you forgot to add: both happen constantly, and amend makes them disappear before anyone sees them. After a push, the same command makes trouble.

How to think about it

Amend freely before pushing. After pushing, prefer a new commit or a revert, because the amended commit has a different hash and the remote still has the old one.

Worked example

git commit -m "Add repot"
A typo in the message.
git log --oneline -1
The commit, with a hash.
git commit --amend -m "Add report"
Replaces the last commit with a corrected one.
git log --oneline -1
Same change, a different hash: amend makes a new commit.

Your turn

Fix the message of the last commit.

git commit  -m "Add report"

The trap

Amending a commit that is already pushed rewrites shared history; the next push is refused and colleagues must untangle it. Amend before pushing, revert after.

Practise redoing the last commit on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.