Skip to main content

Dev tools and workflow · Intermediate · 17 min

How to Undo Mistakes in Git: restore, revert, reset and reflog

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Git 2.53 (Ubuntu package)

You will discard edits, unstage files, fix the last commit, undo a commit safely with revert, move a branch back with reset, and bring back commits you thought you had lost with the reflog.

On this page

Everyone makes mistakes in Git: an edit you regret, a file staged by accident, a commit with the wrong message, a commit that should never have happened. Git has a different undo for each, and picking the right one depends on one question: has the change been shared yet? Changes that are only on your computer can be rewritten freely. Changes other people already have should be undone by adding a new commit, not by rewriting history.

This tutorial works in a throwaway repository, ~/git-demo/undo, so you can run every command, including the destructive ones, without risk. Each step says what the command destroys before you run it.

Step 1 · Make a practice repository

Create a repository with three commits, each adding a line to a file:

Bash
mkdir -p ~/git-demo/undo
cd ~/git-demo/undo
git init
echo "line one" > notes.txt
git add notes.txt
git commit -m "Add line one"
echo "line two" >> notes.txt
git commit -am "Add line two"
echo "line three" >> notes.txt
git commit -am "Add line three"
git log --oneline

The log lists three commits, newest first. The rest of the tutorial changes and repairs this history. If you ever want to start again, delete the ~/git-demo/undo folder and repeat this step.

Step 2 · Discard changes you have not committed

Say you edited a file and want it back as it was at the last commit. Warning: this destroys the edit. Git cannot recover changes that were never staged or committed, so check what you are throwing away first:

Bash
echo "a mistake" >> notes.txt
git diff

The diff shows the line you added. To throw it away, use git restore with the file name:

Bash
git restore notes.txt
cat notes.txt

The file has three lines again. git restore takes the version from the index, or from HEAD if nothing is staged, and puts it in the working tree. Older tutorials use git checkout -- <file> for this, which also works but is easy to confuse with switching branches.

Check it worked

Bash
git status

The output says nothing to commit, working tree clean.

Step 3 · Unstage a file

Staging is also easy to undo. Edit the file, stage it, and decide that the change should not go into the next commit:

Bash
echo "line four" >> notes.txt
git add notes.txt
git status --short
git restore --staged notes.txt
git status --short

The first status shows M ` with the letter in the left column, meaning staged. After `git restore --staged, the letter moves to the right column, M: the edit is still in your file, and it is no longer staged. Nothing was lost. Old tutorials use git reset HEAD <file> for the same thing.

Put the file back as it was, so the next step starts clean:

Bash
git restore notes.txt

Step 4 · Fix the last commit

You committed, and a second later you see a typo in the message, or you forgot a file. git commit --amend replaces the last commit with a new one. Warning: this rewrites history. The old commit gets a new hash, so do this only for commits you have not pushed. If other people already have the commit, use the revert step below instead.

Fix a message:

Bash
git commit --amend -m "Add the third line"
git log --oneline

Add a forgotten file to the last commit, keeping its message:

Bash
echo "extra" > extra.txt
git add extra.txt
git commit --amend --no-edit
git show --stat HEAD

The commit now contains extra.txt as well. The hash in git log --oneline is different from before, since a commit's hash depends on its content, its parent and its message.

Check it worked

Bash
git log --oneline
git status --short

There are still three commits, and the newest is called Add the third line. The status is empty.

Step 5 · Undo a commit safely with revert

git revert makes a new commit that does the opposite of an earlier one. The history keeps both the mistake and the correction, and nobody who already has the old commit gets a surprise. This is the right undo for shared work. Make a bad commit, and revert it:

Bash
echo "oops, wrong text" >> notes.txt
git commit -am "Add a wrong line"
git revert --no-edit HEAD
git log --oneline

The log shows the bad commit and then a Revert "Add a wrong line" commit on top of it. The file has no oops line. --no-edit accepts the default message. To revert a commit from further back, give its hash, such as git revert <hash>. If later commits changed the same lines, the revert may stop with a conflict, which you resolve like any merge conflict: see how to resolve a Git merge conflict.

Check it worked

Bash
cat notes.txt

You see line one, line two and line three. The mistake is undone, and the history is intact.

Step 6 · Move the branch back with reset

git reset moves the current branch to another commit. It rewrites history on your branch, so as with --amend, use it on commits that only exist on your computer. It has three modes that differ in what happens to the changes in the commits you move past. Warning: --hard also discards your uncommitted work.

Make two commits you want to redo:

Bash
echo "draft A" >> notes.txt
git commit -am "Draft A"
echo "draft B" >> notes.txt
git commit -am "Draft B"
git log --oneline -3

--soft moves the branch and keeps everything else. The undone changes stay staged, ready to commit again as one commit:

Bash
git reset --soft HEAD~2
git status --short
git commit -m "Add both drafts as one commit"

HEAD~2 means two commits before the current one. After the reset, the status shows the file staged, and the commit that follows replaces the two drafts with one. The default mode, --mixed, moves the branch and unstages the changes, but keeps them in your files:

Bash
git reset HEAD~1
git status --short

The status shows M notes.txt: the change is still in your file, not staged, and not in any commit. Commit it again, so you have something to lose in the next step:

Bash
git commit -am "Add both drafts again"

The last mode, --hard, moves the branch and makes your files match. It also discards uncommitted edits in tracked files, and the reflog cannot bring those back, because they were never committed. Anything that was in those commits or in your working tree, and was not committed elsewhere, disappears from view:

Bash
git reset --hard HEAD~1
git log --oneline -3
cat notes.txt

The "Add both drafts again" commit is gone from the log, and the draft lines are gone from the file.

Step 7 · Get back a commit you lost

Git rarely deletes a commit right away. Every time HEAD moves, Git writes a line in the reflog, a private diary of where HEAD has been. The commit you reset away is still stored, and the reflog tells you where:

Bash
git reflog -5

The output has lines such as a1b2c3d HEAD@{1}: commit: Add both drafts again. HEAD@{1} means "where HEAD was one move ago". The lost commit is the entry just before the reset: moving to HEAD~1 line. Move the branch back to it, naming it by its position in the reflog:

Bash
git reset --hard "HEAD@{1}"
git log --oneline -3
cat notes.txt

The commit and the draft lines are back. The same trick recovers a branch you deleted with git branch -D: find the last commit of that branch in git reflog, and run git branch <name> <hash>.

Two limits apply. The reflog is local to your repository, and a fresh clone has none. Entries expire, after 90 days by default for reachable commits and 30 days for unreachable ones, and then git gc can delete the commits for good. So recovery works for mistakes you notice quickly, not for last year's.

Check it worked

Bash
git status
git log --oneline -3

The status is clean, and the log shows the "Add both drafts again" commit on top.

Step 8 · Bring back one file from an older commit

You do not always want to move the whole branch. git restore --source copies a file as it was in any commit, without touching the rest. Restore notes.txt as it was five commits ago, in your first commit, and then look at the status:

Bash
git restore --source=HEAD~5 notes.txt
cat notes.txt
git status --short

The file now holds only line one, as a change in your working tree that is not staged. You could commit that, or throw it away with a plain git restore notes.txt. Do the second, to leave the repository clean:

Bash
git restore notes.txt
git status --short

Step 9 · Remove untracked files

git restore does not touch files that Git has never tracked. To remove them, use git clean. Warning: untracked files are not in Git's history, so a deleted untracked file is gone for good. Always do a dry run with -n first:

Bash
echo "scratch" > scratch.txt
git clean -n

The dry run prints Would remove scratch.txt and deletes nothing. When the list is what you expect, run it with -f, which Git requires before it deletes anything:

Bash
git clean -f
ls

git clean -f leaves ignored files alone. Adding -x removes them as well, and -d also removes untracked folders, so use those flags only after a dry run with the same flags.

Troubleshooting

  • You amended or reset a commit that you had already pushed. Pushing now fails, because your history no longer contains what the server has. Do not force-push a shared branch just to make the error go away. If the commit was shared, revert it instead. If the branch is yours alone, git push --force-with-lease is a safer force, since it refuses when someone else added commits you have not seen.
  • git revert stops with error: commit <hash> is a merge but no -m option was given. A merge commit has two parents, and you must choose which line to keep. git revert -m 1 <hash> keeps the first parent, usually the branch you merged into.
  • fatal: ambiguous argument 'HEAD~3': unknown revision means the history is shorter than you asked for. Check with git log --oneline.
  • You committed a password. Undoing the commit does not remove it from history that others have. Treat the secret as leaked, rotate it, and read keep secrets out of Git.

Step 10 · Clean up

Remove the practice repository:

Bash
cd ~
rm -rf ~/git-demo/undo

The ~/git-demo/notes repository from the earlier tutorials is not affected.

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.