Git Reset & Revert

git reset <mode> HEAD~1 moves the branch back one commit (HEAD~1 is one commit before HEAD, where you are now), and the last commit is removed from the…

You've just committed the wrong price. Or worse: the commit is already pushed. Your first reflex is usually to delete that commit as fast as possible. Wait. How you undo it depends on one thing: whether the commit has reached anyone else yet.

git reset and git revert both undo a commit, but in very different ways. git reset moves the branch back: it moves the branch label to an older commit, as if the last commit had never been made. git revert makes a new commit that reverses the old one. Think of a newspaper that printed the wrong price. Copies that already reached readers' homes can't be taken back, so the next day it prints a correction. But you can tear up a draft on your own desk, which nobody has seen yet, and write it again. In Git, the newspaper that reached readers is a commit that is already pushed, and the correction is revert. The draft on your desk is a commit that is not pushed yet, and tearing it up is reset.

The example: a wrong price

The cake-shop repo has 3 commits. The last one, c9e1f47 change price, changes menu.html from Layer cake $15 to Layer cake $18. It turns out the price isn't going up after all. This is the history:

$ git log --oneline
c9e1f47 (HEAD -> main) change price
b2d4f60 add menu
a1c3e5f first page

The three reset modes

git reset <mode> HEAD~1 always moves main (and HEAD) back to b2d4f60, so c9e1f47 disappears from git log. In place of <mode> you write one of three options: --soft, --mixed or --hard. The mode decides what happens to the $18 change. Before you look at the table, guess: after git reset --soft HEAD~1, where is that change? Gone, in the staging area, or only in the working directory?

Mode main moves to Staging area menu.html in the working directory git status
--soft b2d4f60 $18 $18 modified: menu.html under Changes to be committed
--mixed (default) b2d4f60 $15, same as the commit $18 modified: menu.html under Changes not staged for commit
--hard b2d4f60 $15 $15 nothing to commit, working tree clean

This is what the terminal shows when you run git reset --soft HEAD~1, then git status:

$ git reset --soft HEAD~1
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   menu.html

So with --soft, the changes stay in the staging area, ready to be committed again.

One more: want to take a single file out of the staging area without undoing a commit? Use git restore --staged menu.html, or the older way, git reset HEAD menu.html. The file still has your changes.

git revert: undoing with a new commit

Back to the same 3 commits, before any reset. This time, the commit c9e1f47 is undone with revert:

$ git revert c9e1f47
[main 5d2a8b0] Revert "change price"
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git log --oneline
5d2a8b0 (HEAD -> main) Revert "change price"
c9e1f47 change price
b2d4f60 add menu
a1c3e5f first page

Reset or revert?

Situation Use
The commit is pushed and the team may have pulled it git revert <hash>
Not pushed, you want to commit again with a tidier message or content git reset --soft HEAD~1
Not pushed, you want to choose again which files go in git reset HEAD~1 (--mixed)
Not pushed, you want to throw away the commit and its changes git reset --hard HEAD~1

Why not reset a branch that's already pushed? After the reset, your main is behind origin/main (your laptop's note of where main is on GitHub). So a normal push is rejected, the same as in the Git Push & Pull lesson. If you force it with --force, the commit disappears from GitHub. But your teammates who already pulled still have it, and it comes back to GitHub when they merge and push again. Revert only adds a commit, so it's safe for everyone.

For smaller mistakes, there are three other commands:

⚠️ Resets that really do lose work

TL;DR: git reset <mode> HEAD~1 moves the branch back one commit (HEAD~1 is one commit before HEAD, where you are now), and the last commit is removed from the history. --soft: the changes stay in the staging area. --mixed (the default): the changes are in the working directory (your folder), not added. --hard: every change is thrown away, uncommitted ones included. git revert <hash> makes a new commit that reverses that commit, and the history stays whole. Not pushed yet: reset is fine. Already pushed: use revert.

⚡ Reset: move the branch back

Write:

git reset --soft HEAD~1
Guess first: git reset moves the branch back, as if the last commit was never made. After git reset --soft HEAD~1, where do that commit's changes go?

They stay in the staging area (the list of changes that go into the next commit), ready to commit again. Only the commit disappears from git log.

Meaning: HEAD~1 is one commit before HEAD (where you are now). git reset moves the branch back there.

⚡ Three reset modes

Meaning: git reset has three modes. --soft keeps the changes in the staging area. --mixed (the default) keeps them in the working directory (your folder), not added. --hard throws everything away, and Git keeps no record that could bring back uncommitted changes.

⚡ Revert: a correction as a new commit

Write:

git revert c9e1f47

Meaning: Revert makes a new commit that reverses the old commit, here c9e1f47. The old commit stays in the history. So reset removes a commit from the branch's history, while revert adds one.

⚡ Already pushed? Revert

Meaning: A commit that is not pushed yet can be reset. A commit that is already pushed is undone with revert: revert only adds a commit, so it's safe for everyone. After a reset on a shared branch (a branch the team also uses), your push is refused.

What you will learn