Have you ever accidentally committed something to your git repo, or realised after you’ve committed your changes that you’ve made a mistake? If so, this blog post is for you and it will teach you how to ‘git undo’ a commit. Git reset is what you need and I will walk through the steps of how to undo a git commit in this post. Git is one of the most popular version control systems being used today and if you are not familiar with it yet, you should read this post which explains Git in plain English. It is important to be able to undo a commit if you make a mistake and luckily git lets us do this by using git reset. Lots of people search for git undo commit, but there is no such command – you are looking for git reset. But before you use it you need to understand the different options when using git reset, and also whether you should be using git rest or git revert.
Git reset or git revert?
This post focuses on discarding your changes using git reset and will enable you to discard your changes with no record of the undo. If you want new commits to reflect the ‘undoing’ then you need to use git revert.
Git reset (not ‘git undo commit’!)
In order to undo a git commit, you need to use the git reset command. This will remove the most recent commits from your current branch. Beware though, depending on the flags you set when you use this command you may lose some of your work, so read on to ensure you use the correct options for your situation. Also how many commits it removes, and what it does with the now uncommitted changes also depend on the flags you set when you run the command.
Git undo last commit and keep the changes
To undo only the last commit in git and keep the changes (presuming you have not already pushed the code):
git reset --soft HEAD~1
This will undo the last commit but all your changes will still exist, it’s just that the files that the changes were made in have gone back to their uncommitted state.
Git undo last commit and discard the changes
To undo only the last commit in git and discard the changes (be careful, you may lose work here as your changes will be discarded completely):
git reset --hard HEAD~1
This will undo the last commit discard all changes that were made in the files from that commit. You will lose work if you do this, so proceed with caution!
Undo more than one commit
In order to undo more than one commit you can use a variation of a command shown above but just change the number of commits. So this command would remove the last 4 commits and keep the changed files:
>git reset --soft HEAD~4
Undo back to a specific commit
If you want to roll back to a specific commit then you will need to know it’s hash id. To see all your commits you can use git log which will list all commits and their hash id numbers.
Once you have found the relevant commit, copy its hash id and then type:
>git reset --soft 123456
…where 123456 is the hash id of the commit. This will undo your changes back to that commit and will keep your files. If you want to discard your files then use:
>git reset --hard 123456