Undo without losing work · Lesson 12 of 15
Amend and reflog: rescue recent work
Repair the last commit and understand the local safety log of HEAD movements.
Forgot a small file or wrote a poor message? git commit --amend replaces the latest commit with a new one containing the current staging area. Even with similar content its hash changes because it is a new object. Use it before pushing or with team agreement.
A—old B
amend
A—new B′
reflog records the move locally
git init -q -b main echo "code" > app.txt git add app.txt git commit -q -m "Ad app" echo "docs" > README.md git add README.md git commit -q --amend -m "Add app and docs" git log --format=%s
reflog records local positions visited by HEAD and branches after commits, resets and rebases. If a reset loses a commit name, find its hash in git reflog and create a rescue branch. The simulator explains this command; practise it in a real terminal.
Quick question
Does --amend edit the same commit object?
Exercise
Commit with the wrong subject Ad file, amend it to Add file, then print the subject.
Expected output: Add file
git init -q -b main echo "x" > file.txt git add file.txt git commit -q -m "Ad file" git log --format=%s
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.