Module 3: Rewriting Local History Safely
Amend and Reset: What Moves and What Stays
Amend and Reset: What Moves and What Stays
Module: Module 3: Rewriting Local History Safely
Amend and reset are local history editing tools. They are safe when used on private commits and dangerous when used blindly on shared commits. The key is knowing which ref moves and what happens to the index and working tree.
git commit --amend creates a new commit that replaces the current branch tip. The old commit usually remains recoverable through reflog for a while, but the branch no longer points to it. Use amend for a missing file, typo, or improved message before pushing.
git reset moves the current branch. With --soft, changes remain staged. With mixed reset, changes remain in the working tree but unstaged. With --hard, the index and working tree are forced to match the target commit. The first two are useful editing tools; the third is a destructive cleanup tool.
Command Walkthrough
git commit --amend
git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1
git reflog --date=relative
git branch rescue-before-resetd4e5f6a HEAD@{0}: commit: Add focused change
a1b2c3d HEAD@{1}: checkout: moving from main to featureHands-on Lab
Create a commit with a typo in the message and a missing README update. Add the missing update, amend the commit, then use reflog to identify the replaced commit.
What to Watch For
- Do not amend a commit that teammates already based work on unless you coordinate.
- Use git reset --hard only after git status confirms you do not need local changes.
- Create a rescue branch before practicing risky reset operations.
Completion Check
You should be able to explain what each command changed, inspect the resulting history, and describe how you would undo or recover from the operation before using it in a shared repository.