Notes & TILs
Search…
How to undo anything in Git
Posted on 03 Apr, 2021
Posting this as a reference for my future self, so I don't have to search every time.
If the reader has a better way to undo something, please edit the page and send a PR. More than happy to correct myself.

Undo git commit

1
# undo last commit (unstage everything)
2
$ git reset HEAD~
3
​
4
# undo last commit (don't unstage everything)
5
$ git reset --soft HEAD^
Copied!

Undo most recent commit message

1
$ git commit --amend
2
# or
3
$ git commit --amend -m "Fixes bug #42"
Copied!
git commit --amend will update and replace the most recent commit with a new commit that combines any staged changes with the contents of the previous commit. With nothing currently staged, this just rewrites the previous commit message.

Undo git merge

Not yet pushed to remote
1
$ git reset --hard <commit-before-merge>
2
​
3
# assuming the merge was your most recent commit
4
$ git reset HEAD~
Copied!
When you have already PUSHed the merge to remote
1
git revert -m 1 <merge-commit-hash>
Copied!
git revert will make sure that a new commit is created to revert the effects of that unwanted merge. The -m 1 option tells Git that we want to keep the parent side of the merge (which is the branch we had merged into). Finally, also make sure to provide the correct commit hash: when using git revert, we have to specify the actual merge commit's hash.
Now, when you have fixed changes in your branch you would have to revert the revert before merging again. Read How to revert a faulty merge​
1
$ git checkout feature
2
...
3
# commits to fix the bug.
4
$ git checkout master
5
...
6
$ git revert e443799
7
...
8
$ git merge feature
9
...
10
# Fix any merge conflicts introduced by the bug fix
11
$ git commit # commit the merge
12
...
13
$ git push
Copied!

Undo git push

1
$ git revert HEAD~
Copied!

Undo git add

1
$ git restore --staged index.html
Copied!

Undo most recent git checkout

1
$ git checkout -
Copied!

Undo git pull

  1. 1.
    Get SHA1 of state before pull using git reflog
  2. 2.
    Reset state using
    1
    # WARNING: Uncommited changes will be lost
    2
    git reset --hard <SHA1>
    Copied!
Optionally you can go back in time using git reset --hard [email protected]{"5 minutes ago"}

Undo accidental git branch -D delete

  1. 1.
    Use git reflog to get the commit SHA before you deleted the branch
    1
    git reflog
    Copied!
  2. 2.
    Now if the change isn't pushed to remote, run
    1
    git checkout -b branch_name <SHA1>
    Copied!
    Otherwise recreate the branch
    1
    git branch <branchName> <SHA1>
    Copied!

Stop a file from being tracked

When you committed the file previously but now realise it shouldn't have been
1
git rm --cached application.log
Copied!

Most recent git reset

  1. 1.
    Get last good state using git reflog
  2. 2.
    Use git reset to reset :)
    1
    git reset <COMMIT_HASH>
    Copied!

Most recent git stash pop/drop/clear

  1. 1.
    Find the lost stash,
    1
    git fsck --no-progress --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --oneline --merges --no-walk
    Copied!
  2. 2.
    Update the stash refs:
    1
    git update-ref refs/stash "$LOST_STASH_COMMIT" --create-reflog -m "my recovered stash"
    Copied!

Most recent git stash apply

Make sure that diff coloring is set to auto in your .gitconfig otherwise the command will fail with unrecognised input.
1
git stash show -p | git apply --reverse
Copied!

Undo accidental git tag delete

Only works for annotated tags, tags created using git tag -a
  1. 1.
    Use git fsck to check for dangling/unreachable commits.
    1
    git fsck --unreachable | grep tag
    Copied!
  2. 2.
    Verify that its the correct tag:
    1
    git cat-file -p "COMMIT_HASH"
    Copied!
  3. 3.
    Update tag refs:
    1
    git update-ref refs/tags/"TAG_NAME" --create-reflog "COMMIT_HASH"
    Copied!
  4. 4.
    Tag is now restored, run git tag -l to verify

Undo/Restore a file to a previous version

  1. 1.
    Get the commit you want to restore the file to
    1
    git log --oneline <FILE>
    Copied!
  2. 2.
    Use git restore to restore the file :)
    1
    git restore --source=<COMMIT> <FILE>
    Copied!

Undo accidental file delete

When you have not committed the changes yet
1
git checkout HEAD <file-path>
Copied!
When committed the file delete
  1. 1.
    Choose commit that deleted the file
    1
    git log --diff-filter=D --oneline
    Copied!
  2. 2.
    Checkout the file
    1
    git checkout <COMMIT>~1 -- <FILE>
    Copied!

Undo a git merge 😟️

Undoing a git merge is a risky business. Please proceed with caution
When the merge has conflicts, and you want to give up
1
git merge --abort
Copied!
When the merge is unpushed
1
git reset --merge ORIG_HEAD
Copied!
Reference ORIG_HEAD points to the original commit before the merge. So we are just resetting that
When you pushed the merge commit
  1. 1.
    Switch to your main/default branch
    1
    git checkout main
    Copied!
  2. 2.
    Get the merge commit from git log
    1
    git log --oneline
    Copied!
  3. 3.
    Revert that merge commit
    1
    git revert -m 1 <COMMIT>
    Copied!
Also must read: How to revert a faulty merge​

Undo all current uncommitted changes

Storing everything in stash
1
# -a is for adding untracked changes
2
git stash save -au "custom message"
Copied!

Resources

Last modified 1mo ago