I use git for pretty much all my projects these days. When ever I do a commit, I make sure that it is atomic and has a proper commit message. One of the main reason why I keep the commits atomic is that, I can rollback the commit if needed.
Recently, I faced a situation, where in one of my projects, the feature I added a couple of commits back wasn’t working properly and I decided to completely get rid of it. Since I follow semantic versioning, I wanted to make sure that I maintain the different release of my project properly.
After a bit of digging up, I found the exact command in git to do that. Since the command was not that intuitive I thought of documenting it here, so that I know where to lookup when I face the situation again 😉
Problem
Let me explain problem properly. Let’s assume that I have the following list of commits in my history.
A -> B -> C -> D -> E
The head
is at E
now. The feature that I was talking about was introduced in commit C
. Now I want to rollback the changes that I did in commit C
, after E
, but still maintain the history. In short I want the commits to look like
A -> B -> C -> D -> E -> C'
where C'
is the opposite of C
Solution
I could have manually removed that changes I did in commit C
. But being a fan of fancy one-liners, I was looking for a solution and found that there is a command in git which can do this for you.
All you need to do is to execute the following command.
git revert 'SHA_of_C'
It’s really fascinating to see, how the developers of git have thought of these different cases 🙂
Leave a Reply