Since everything we do in git can be done in countless different ways, there are a few ways to get rid of commits. The most common way to undo one or more recent commits is using the command
git reset that you’ll see in this pro tip.
What is the HEAD?
HEAD is a pointer that indicates which branch and commit you’re on. It is used frequently, and often without you even knowing it. For example, did you know the
HEAD is used to switch branches?
What is git reset?
On the other hand,
git reset is a command that restores a previous state of the
HEAD. That’s why we use
HEAD when undoing commits, to indicate which previous state you want to go back to.
For example, let’s suppose you have a state like the following:
If you want to undo the last commit
B, you want to go back to the state of commit
Undoing the last commit
To undo the last commit made, you should use the following command:
git reset HEAD~1
Or the following command, which is a shortcut for the command above:
git reset HEAD^
Or even the following shortcut:
git reset HEAD~
These three commands mean:
Go back to the state before the last commit.
Note that when executing these commands, you will not see a message stating that the commit was undone, but if you run the command
git status after executing any of these three commands you will see that files added and/or changes made went back to being marked as changes to be committed (added to a commit).
Suppose you have a history like the one in the following image, in which the last commit (
48ccb8) adds the file called
And if you run any of the commands above, followed by a
git status, you will see a result like this:
And you can see that
arquivo-4.txt has returned to its previous state, which was waiting to be committed. And if you check the history again you will see that the commit
48ccb8 no longer appears.
You can now discard the changes or keep up with them and make a new commit.
Undoing the last three commits
Now that you know how to undo a commit, you can use the first command you’ve just seen and adapt it to undo more commits. To do so, just add the number of commits you want to undo after
~. Let’s take another look at our history, which now contains only four commits (since I already undid one):
Now let’s suppose I want to go back to the state of commit
d815be which is the initial commit that added the
README.md file. To do so it is necessary to undo three commits, so for that a suitable command is the following:
Agora vamos supor que eu quero voltar ao estado do commit
d815be que é o commit inicial que adicionou o arquivo
README.md. Para isso é necessário desfazer três commits, então para isso o comando indicado é este:
git reset HEAD~3
After executing this command and running
git status once more, we have the three files that were added by the unmade commits:
Note that before running these commands I removed
arquivo-4.txt as I won’t need it anymore. And looking into the history again we only see the initial commit:
Before finishing this pro tip I want to leave a recommendation: it is a good practice to avoid undoing commits that you have already pushed to prevent causing detached
HEAD problems for other people working with you.
GitFichas ß| GitStudyCards
Below you can find some Git study cards to help you remember these commands and shortcuts:
Hope this pro tip helps you undo commits. 😉