CS Fundamentals Trainer · Guides

Git: undoing mistakes with the reflog, reset, revert, rebase and recovery

A practical guide to getting out of trouble in Git: the three areas (working tree, index, repository), what each reset mode touches, the reflog as a recovery tool with its limits, reset against revert, amend and fixup with autosquash, interactive rebase, cherry-pick, merge conflicts (zdiff3, rerere), stash, force-with-lease, and finding what changed.

9 min read

Most people learn Git as a list of commands and then freeze the first time something goes wrong: a commit on the wrong branch, a reset --hard that ate an afternoon of work, a merge full of conflict markers. The way out is a small mental model plus a safety net. Git almost never destroys committed work; it moves pointers, and the old commits stay in the object store for weeks. This guide explains the three places where your changes live, the commands that move them, the reflog that lets you undo almost anything, how to rewrite history safely before it is shared, how to handle conflicts, and how to push without hurting a teammate. Every command output below came from a real run with Git 2.43. For the object model behind it (blobs, trees, commits, refs) and for bisect, branching strategies and CI, see the guide "Git internals and CI/CD pipelines".

1. Three places and who touches them

A change moves through three areas, and almost every undo question is "which area do I want to reset?".

AreaWhat it isMoved by
Working treethe files on diskyour editor, git restore, git switch, git stash
Index (staging area)the snapshot that the next commit will recordgit add, git restore --staged, git reset
Repositorycommits and the branch pointer HEADgit commit, git reset, git revert, git rebase

git reset has three modes that differ only in how far they go:

CommandMoves the branch pointerResets the indexOverwrites the working tree
git reset --soft <c>yesnono
git reset --mixed <c> (default)yesyesno
git reset --hard <c>yesyesyes

Only --hard (and git restore <file>, git checkout -- <file>, git clean) can lose uncommitted work, because that work was never in the repository. Everything that was committed is recoverable, as the next section shows.

2. The safety net: the reflog

Every time HEAD or a branch moves, Git writes a line to the reflog, a private, local diary of where your pointers have been. After a bad reset, the commits are not gone, they are just unreachable from any branch:

$ git reset --hard HEAD~2
$ git log --oneline
0decb64 c1
$ git reflog
0decb64 HEAD@{0}: reset: moving to HEAD~2
2a2c20c HEAD@{1}: commit: c3
703d80e HEAD@{2}: commit: c2
0decb64 HEAD@{3}: commit (initial): c1
$ git reset --hard HEAD@{1}
$ git log --oneline
2a2c20c c3
703d80e c2
0decb64 c1

Remember three facts about it:

A detached HEAD is the other case where the reflog saves you: commits made there belong to no branch, and git switch -c rescue <sha> gives them a name.

3. Undoing a change: reset or revert?

$ git revert --no-edit HEAD
$ git log --oneline | head -2
0ad11db Revert "c3"
2a2c20c c3

To revert a merge commit you must say which side to keep with -m 1 (usually the mainline). Reverting a merge also makes Git treat those changes as already merged, so merging the branch again later brings nothing; you must revert the revert first.

For single files use the newer, clearer commands (Git 2.23): git restore <file> throws away working-tree edits, git restore --staged <file> unstages, and git restore --source=<commit> <file> brings a version from the past. git switch changes branches and git switch -c name creates one.

4. Fixing the last commits

Wrong message, or you forgot a file: git commit --amend replaces the last commit (with the staged changes and a new message). It changes the commit's id, so only amend before pushing.

A fix that belongs to an earlier commit: commit it with --fixup, then let Git reorder it:

$ git commit -qa --fixup HEAD
$ git log --oneline | head -2
3328826 fixup! feat g
9744119 feat g
$ GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash HEAD~2
$ git log --oneline | head -2
3e798ea feat g
0ad11db Revert "c3"

Interactive rebase (git rebase -i <base>) opens a to-do list with one line per commit. Change the word at the start of a line:

WordEffect
pickkeep the commit
rewordkeep it, edit the message
editstop here so you can change the commit
squash / fixupmerge into the previous commit (fixup drops the message)
dropremove the commit

Reordering lines reorders commits. If it goes wrong, git rebase --abort returns to the state before it started, and the reflog has the original tip.

5. Moving a commit between branches

git cherry-pick <sha> copies one commit onto the current branch as a new commit (new id, same change). Use it for a hotfix that must land on two branches, or when you committed on the wrong branch: cherry-pick onto the right one, then git reset --hard HEAD~1 on the wrong one. Many commits at once: git cherry-pick A..B. A cherry-picked commit can conflict like any merge, and Git will not know that two commits are "the same change", so avoid cherry-picking something you will later merge.

6. Merge conflicts

A conflict happens when both sides changed the same lines. Git stops, marks the files and waits:

<<<<<<< HEAD
B
=======
A
>>>>>>> a

Edit the file to the result you want, remove the markers, git add it, and finish with git commit (or git rebase --continue in a rebase). If you want out: git merge --abort or git rebase --abort, and the working tree returns to how it was.

Three settings make conflicts easier:

Rebasing resolves conflicts commit by commit and can repeat the same conflict; merging resolves them once, at the end.

7. Stash: putting work aside

git stash saves modified tracked files and cleans the working tree; git stash pop restores them (and drops the entry), git stash list shows them, and git stash -u includes untracked files. It is meant for short interruptions, such as switching branches for a quick fix. For anything that lives longer than an hour, a work-in-progress commit on a branch is safer than a stash: stashes have no branch, are easy to forget and can conflict when popped.

8. Pushing history you rewrote

Once a commit is pushed and others may have it, rewriting it forces everyone else to repair their branches. So: rewrite your own, unshared branches freely, and never force-push a shared branch such as main. When you must update your own feature branch after a rebase, use:

git push --force-with-lease

It overwrites the remote branch only if it still points where you last saw it, so you do not erase a commit a colleague pushed in the meantime. Plain --force has no such check. Many teams protect main with branch rules that reject force pushes entirely.

9. Finding what happened

A few safe deletions: git branch -d name refuses to delete an unmerged branch ("error: the branch 'dead' is not fully merged"), and -D forces it. The commits are still in the reflog if you regret it.

10. Common mistakes

  1. Running reset --hard or clean -fd with uncommitted work you wanted.
  2. Amending or rebasing commits that were already pushed to a shared branch.
  3. git push --force instead of --force-with-lease.
  4. Using revert for a private mistake (noise) or reset for a shared one (breaks teammates).
  5. Resolving a conflict by choosing one side blindly instead of reading both.
  6. Panicking after a bad command instead of reading git reflog.
  7. Large "fix everything" commits that are impossible to revert or bisect.
  8. Committing secrets, then deleting them in a later commit: they remain in history. Rotate the secret and rewrite history if needed.

11. Things to mention in an interview

Quick recap

Git moves pointers; commits survive. Check git status, commit before risky operations, and use git reflog when you think work is lost. reset for private history, revert for shared history, --force-with-lease if you must overwrite your own branch, and never force-push shared branches.

Sources

Practise this topic: DevOps & Cloud

More guides