Niranjan Kumar Niranjan Kumar - 25 days ago 5
Git Question

Remove a git commit between two commits

I added some functionality in my project which took 4 git commits, now business is asking that the functionality is no more needed(After more than a month). So I need to remove those particular git commit(s) from my repo which now has 27 more commits after that.

Answer

Because you are dealing with a published branch, which is presumably being used by someone other than you, I would recommend that you revert the two commits using git revert. From the branch in question type:

git log

and find the SHA-1 hashes of two commits in question. Then revert them using:

git revert abcd1234..foobar12

where abcd1234 is the hash of the first (oldest) of the two commits, and foobar12 is the more recent of the two commits.

Using git revert will add commits which effectively undo the two commits buried in your branch. The alternative to this approach would be to use rebase or filter branch. But both these methods involve rewriting the history of your branch. This could cause a headache for anyone else using your branch since they would no longer be able to pull or push.