Sometimes there is a need to undo some commit. Sometimes last, sometimes not.
This article presents situations from my
Someday I was working on my branch and I needed to switch to another one.
I created a commit with a temporary message because this commit was only for switching. I did not want to publish this commit. After a while I came back to my branch and I continued to work on it. I have done two commits and I pushed it to a remote repository. I finished my task, so I wanted to do merge my changes with a master branch. I noticed I did not remove temporary commit.
Another situation: I committed a file which I did not want to and pushed it to a remote. Then I realized it.
In the first situation, a commit which needs to be edited is third.
In the second, a commit is first.
A solution is the same for both situations. However, the
To undo third commit, two previous needed to be removed as well.
git reset –soft HEAD~3
In the second situation only one commit will be removed – the first.
git reset –soft HEAD~1
In both situations, solution is the same.
Ordinary git push won’t work.
It must be executed with force option.
It is simple to undo commits. Thanks to option soft local changes will not be removed and they still will be set as ‘to be committed’. There are several other options for reset, I encourage you to check them in git documentation. In my dev-life, another option which I often use is hard, you can find it in the documentation.