DEV Community

Cover image for Undo Anything in Git: 7 Commands That Fix Every Mistake
Muiduzzaman Lipu for Refactorly

Posted on

Undo Anything in Git: 7 Commands That Fix Every Mistake

Every Git mistake has an undo. The hard part is knowing which command fixes which mistake, and which one can destroy your work.

Here are seven, from "I edited the wrong file" to "I already pushed it". Every output below comes from a real run in a test repo (Git 2.50).

Video version:

1. Throw away changes in a file: git restore

$ git status --short
 M app.js
$ git restore app.js
$ git status --short
            # clean: back to the last committed version
Enter fullscreen mode Exit fullscreen mode

⚠️ Uncommitted changes you restore are gone. Git never saved them, so even reflog can't bring them back.

2. Unstage a file: git restore --staged

You ran git add on something you didn't mean to commit:

$ git add secret.env
$ git status --short
A  secret.env
$ git restore --staged secret.env
$ git status --short
?? secret.env
Enter fullscreen mode Exit fullscreen mode

The file is out of the staging area but still on disk, and nothing is lost. (Then add it to .gitignore.)

3. Fix the last commit: git commit --amend

Forgot a file, or made a typo in the message:

$ git add test.js                # the file you forgot
$ git commit --amend -m "Add login form + tests"
$ git log --oneline
252f523 Add login form + tests
129f852 Initial commit
Enter fullscreen mode Exit fullscreen mode

There's still one commit, now with both files. --amend replaces the commit (it gets a new hash), so only do it before you push.

4. Undo a commit but keep your code: git reset

$ git reset --soft HEAD~1      # commit undone, changes stay staged
$ git status --short
A  login.js
A  test.js

$ git reset HEAD~1             # commit undone, changes unstaged
$ git status --short
?? login.js
?? test.js
Enter fullscreen mode Exit fullscreen mode

Either way your code is still in your files. Only the commit is gone, so you can rework it and commit again.

5. Delete a commit completely: git reset --hard ⚠️

$ git reset --hard HEAD~1
HEAD is now at 129f852 Initial commit
$ ls
app.js            # login.js and test.js are gone
Enter fullscreen mode Exit fullscreen mode

This moves the branch back and wipes the files. Use it when you really want the work gone. And if you didn't…

6. Rescue "lost" commits: git reflog

Git records every place HEAD has pointed. A commit you "deleted" with reset --hard is still in there:

$ git reflog
129f852 HEAD@{0}: reset: moving to HEAD~1
252f523 HEAD@{1}: commit: Add login form + tests
$ git reset --hard HEAD@{1}
HEAD is now at 252f523 Add login form + tests
$ ls
app.js  login.js  test.js        # back ✅
Enter fullscreen mode Exit fullscreen mode

The reflog is local and keeps entries for about 90 days by default. It's the reason almost nothing you committed is ever truly lost.

7. Undo a pushed commit safely: git revert

Once a commit is pushed and others may have pulled it, don't rewrite history with reset or --amend. Add a new commit that undoes it instead:

$ git revert --no-edit HEAD
[main a6b2146] Revert "Add buggy feature"
 1 file changed, 1 deletion(-)
$ git log --oneline
a6b2146 Revert "Add buggy feature"
2ccf2cf Add buggy feature
Enter fullscreen mode Exit fullscreen mode

The history stays intact, and a normal git push is all you need. Nobody's clone breaks.

Cheat sheet

Mistake Command Loses work?
Edited a file, want it back git restore <file> ⚠️ the uncommitted edits
Staged the wrong file git restore --staged <file> no
Last commit needs a fix git commit --amend no (don't use after pushing)
Undo commit, keep code git reset --soft HEAD~1 no
Undo commit and code git reset --hard HEAD~1 ⚠️ yes (recoverable via reflog)
"Lost" a commit git reflog + git reset --hard HEAD@{n} no
Already pushed git revert <commit> no

Which one saved you last? 👇


I post one tested coding tip every day on YouTube at Refactorly. The 30-second version of this one is here.

Top comments (0)