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 – or “git uncommit last commit“. Whether you want to just undo a commit and keep your changes, or remove the last commit and discard your changed files. Git reset is what you need – I’ll explain how to undo the last commit and keep the changes. It is important to be able to ‘git uncommit’ 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 reset
or git revert to remove your commit.
Table of contents
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.
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. 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.
Undo last commit and keep the changes
If you’ve been searching for “git undo commit keep changes” or “git uncommit” this section is for you. 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 and keep your changes. The files that the changes were made in have gone back to their uncommitted state.
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 effectively remove the last git commit and discard all changes that were made in the files from that commit. You will lose work if you do this, so proceed with caution!
How to undo more than one commit
In order to undo more than one commit you can use a variation of the 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
How to remove / undo 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
Summary
You should now know how to undo a commit, and drop or remove your last git commit. You’ll also know how to remove a commit, whether it’s your last one, or a specific commit in a long list of commits! You have examples that show you how to ‘git uncommit’ and keep the changes, and how to discard the changes.