Skip to content

Instantly share code, notes, and snippets.

@kalkulus
Last active October 22, 2019 08:51
Show Gist options
  • Save kalkulus/294423642ac86d98f02a07220c420f28 to your computer and use it in GitHub Desktop.
Save kalkulus/294423642ac86d98f02a07220c420f28 to your computer and use it in GitHub Desktop.
https://stackoverflow.com/questions/37420642/how-to-undo-the-last-commit-in-git
git reset HEAD^
This will bring the dir to state before you've made the commit, HEAD^ means the parent of the current commit
(the one you don't want anymore), while keeping changes from it (unstaged).
=====
https://nakkaya.com/2009/09/24/git-delete-last-commit/
Git Delete Last Commit
Once in a while late at night when I ran out of coffee, I commit stuff that I shouldn't have. Then I spend the next 10 - 15 minutes googling how to remove the last commit I made. So after third time I wanted to make a record of it so I can refer to it later.
If you have committed junk but not pushed,
git reset --soft HEAD~1
HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. --soft option will delete the commit but it will leave all your changed files "Changes to be committed", as git status would put it.
If you want to get rid of any changes to tracked files in the working tree since the commit before head use --hard instead.
Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,
git revert HEAD
This will create a new commit that reverses everything introduced by the accidental commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment