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
- HEAD is the marker of where you are now: the active branch and its latest commit. Right now HEAD is on
main, atc9e1f47. HEAD~1means one commit before HEAD, which isb2d4f60.HEAD~2means two commits before HEAD, which isa1c3e5f.- Remember the three places Git uses: the working directory (the files in your folder), the staging area (what you've
git added, ready for the next commit), and the repository (the commit history in.git).
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.
--softonly moves the branch. The staging area and your files aren't touched. This mode is good when you want to commit again with a better message or better content. The command prints nothing.--mixedmoves the branch, and also makes the staging area match the target commit (hereb2d4f60), but your files stay. The changes become "modified, not added", so you can choose again what togit add.git reset HEAD~1with no mode means--mixed. Git printsUnstaged changes after reset:and thenM menu.html.--hardmakes the staging area and the working directory match the target commit. Git printsHEAD is now at b2d4f60 add menu. Uncommitted changes are gone, and Git keeps no record that could bring them back. The commitc9e1f47itself can still be found through the reflog, which the next lesson covers.
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
- Revert makes a new commit,
5d2a8b0, whose content is the opposite ofc9e1f47:menu.htmlis back to$15. Your commit hash will be different. c9e1f47stays in the history. Nothing is removed, something is only added.- Revert can undo any commit, not just the last one:
git revert <hash>. - Git opens a text editor for the commit message. The default message is fine, so just save it. Want no editor? Use
git revert --no-edit c9e1f47.
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:
git restore menu.html: throws away edits you have not added. The file goes back to its content in the staging area, which is the last commit's content if you haven't rungit addon that file. The edits you throw away can't be brought back.git restore --staged menu.html: takes the file out of the staging area without changing its content.git statusitself suggests both of thesegit restorecommands.git commit --amend -m "new message": replaces the last commit with a new commit. The new commit has the new message, and it also holds the changes now in the staging area. Its hash is new too, so, like reset, use it only if that commit is not pushed yet.
⚠️ Resets that really do lose work
--hardwith uncommitted work. Those changes are gone. Checkgit statusbefore any--hard.- Counting
HEAD~wrong.HEAD~1undoes 1 commit,HEAD~3undoes 3 commits at once. Checkgit log --onelinebefore resetting. - Resetting a shared branch. Your push will be rejected. Don't force it with
--force, usegit revert. - Stuck in the revert editor. Git often opens Vim, a text editor inside the terminal. Type
:wqand press Enter to save the default message, or use--no-edit.
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.