Introduction
Everyone commits the wrong file, writes a bad message, or breaks something that worked. Git gives a precise tool for each kind of mistake, and almost nothing is ever truly lost. Knowing which tool affects which of the three trees (HEAD, the index and the working tree) is the key to undoing safely, and to not making things worse.
How it works
Throw away uncommitted work: git restore file copies the index version back into the working tree; git restore --staged file unstages it (copies HEAD's version into the index) and leaves your edits alone.
Move the branch back: git reset <commit> moves the current branch, and HEAD with it, to another commit. Its mode decides what else changes:
| Mode | Branch and HEAD | Index | Working tree |
|---|---|---|---|
--soft | moved | unchanged | unchanged |
--mixed (default) | moved | reset to the commit | unchanged |
--hard | moved | reset to the commit | reset to the commit |
--soft is useful to redo the last commits as one; --mixed to restage differently; --hard throws away everything since that commit, so use it carefully.
Undo a shared commit: git revert <commit> creates a new commit whose changes are the reverse of the old one. History only grows, so it is safe on branches that others have pulled. If later commits changed the same lines, the revert can conflict like a merge.
Fix the last commit: git commit --amend replaces the last commit with a new one that includes the staged changes and, optionally, a new message. It gets a new id, so only amend commits you have not pushed.
Recover: the reflog records every position HEAD has had: commits, resets, checkouts, amends. Even after git reset --hard, the old commit is still in the database and listed in the reflog, so git reset --hard HEAD@{1} brings it back.
git restore app.py # discard unstaged edits
git restore --staged app.py # unstage
git reset --soft HEAD~1 # undo the last commit, keep its changes staged
git reset --hard origin/main # make the branch exactly like the remote
git revert a1b2c3d # new commit undoing a1b2c3d
git commit --amend -m "Better message"
git reflog # where has HEAD been?
Example
commits = ["v1", "v2", "v3"]
head, index, work = 2, "v3", "v3"
def show(title):
print(f"{title:41} HEAD=c{head + 1}({commits[head]}) index={index} work={work}")
def reset(mode, target):
global head, index, work
head = target
if mode in ("mixed", "hard"):
index = commits[head]
if mode == "hard":
work = commits[head]
show("start")
work = index = "v4"
show("edit and git add")
reset("soft", 1)
show("git reset --soft HEAD~1")
reset("mixed", 1)
show("git reset --mixed (index back to c2)")
reset("hard", 1)
show("git reset --hard (everything back to c2)")
reset("hard", 2)
show("git reset --hard c3 (from the reflog)")
commits.append(commits[1])
head = 3
index = work = commits[head]
show("git revert c3 (new commit c4)")Line by line
- Three commits hold versions v1 to v3 of one file, and HEAD, the index and the working tree all agree.
- After v4 is edited and staged,
--softmoves HEAD back to c2 but keeps v4 in the index and working tree. --mixedalso resets the index to c2's version and leaves the working tree's v4 alone.--hardresets all three and discards v4; the reflog still knows c3, so resetting to it brings c3 back, though not the uncommitted v4.- Reverting c3 adds a new commit c4 whose content is c2's version, without removing c3 from history.
Expected result
start HEAD=c3(v3) index=v3 work=v3
edit and git add HEAD=c3(v3) index=v4 work=v4
git reset --soft HEAD~1 HEAD=c2(v2) index=v4 work=v4
git reset --mixed (index back to c2) HEAD=c2(v2) index=v2 work=v4
git reset --hard (everything back to c2) HEAD=c2(v2) index=v2 work=v2
git reset --hard c3 (from the reflog) HEAD=c3(v3) index=v3 work=v3
git revert c3 (new commit c4) HEAD=c4(v2) index=v2 work=v2
In the real world
Teams forbid force-pushing to main precisely because reset and amend rewrite history that others rely on; they undo bad merges on main with git revert. Developers use git reset --soft to tidy local commits before sharing them, and the reflog regularly rescues work after a mistaken reset or rebase.
Common mistakes
- Using reset --hard with uncommitted work you wanted to keep: uncommitted changes are not in the reflog.
- Resetting or amending commits that were already pushed, which forces everyone else to repair their copies.
- Expecting git revert to delete a commit: it adds a new one.
- Confusing git restore (files) with git reset (branches).
Best practices
- Commit or stash work before any reset.
- Use revert on shared branches and reset or amend only on your own unpublished commits.
- When something seems lost, look in
git reflogbefore anything else.
Summary
git restore fixes files in the working tree or index. git reset moves the branch: --soft keeps index and working tree, --mixed resets the index, --hard resets everything. git revert undoes a commit with a new commit, --amend replaces the last commit, and the reflog lets you return to any recent position of HEAD.
Quick check
Key takeaways
- reset --soft moves only the branch; --mixed also resets the index; --hard also resets the working tree.
- Never reset or amend commits others have; revert them instead.
- Commits are hard to lose: the reflog remembers where HEAD has been for weeks.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.