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.
Update: Wrote ugit to undo you last git command
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
git commit
Undo most recent commit message
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
git merge
Not yet pushed to remote
When you have already PUSHed the merge to remote
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
Undo git push
git push
Undo git add
git add
Undo most recent git checkout
git checkout
Undo git pull
git pull
Get SHA1 of state before pull using
git reflog
Reset state using
Optionally you can go back in time using git reset --hard master@{"5 minutes ago"}
Undo accidental git branch -D
delete
git branch -D
deleteUse
git reflog
to get the commit SHA before you deleted the branchNow if the change isn't pushed to remote, run
Otherwise recreate the branch
Stop a file from being tracked
When you committed the file previously but now realise it shouldn't have been
Most recent git reset
git reset
Get last good state using
git reflog
Use
git reset
to reset :)
Most recent git stash pop/drop/clear
git stash pop/drop/clear
Find the lost stash,
Update the stash refs:
Most recent git stash apply
git stash apply
Make sure that diff coloring is set to auto in your
.gitconfig
otherwise the command will fail with unrecognised input.
Undo accidental git tag delete
Only works for annotated tags, tags created using
git tag -a
Use
git fsck
to check for dangling/unreachable commits.Verify that its the correct tag:
Update tag refs:
Tag is now restored, run
git tag -l
to verify
Undo/Restore a file to a previous version
Get the commit you want to restore the file to
Use
git restore
to restore the file :)
Undo accidental file delete
When you have not committed the changes yet
When committed the file delete
Choose commit that deleted the file
Checkout the file
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
When the merge is unpushed
Reference ORIG_HEAD
points to the original commit before the merge. So we are just resetting that
When you pushed the merge commit
Switch to your main/default branch
Get the merge commit from git log
Revert that merge commit
Also must read: How to revert a faulty merge
Undo all current uncommitted changes
Storing everything in stash
Resources
Last updated