jinjineos
jinjineos

Reputation: 157

Remove a specific commit from a branch

How do I remove a specific commit from a branch after I pushed it?

Suppose I have a history like this (from latest to oldest) in the remote repository called v0.1and I would like to remove 765th4 from v0.1, without removing it from other branch. How do I achieve this?

2fsf34 
32t6e0
54r346
765th4
9r34gg

thank you!

Upvotes: 1

Views: 446

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522636

The safest bet here is to not actually remove the 765th4 commit from your v0.1 branch, but rather to revert it using git revert. Try the following command:

# from v0.1 branch
git revert 765th4

If you run git log after running the above, you will notice that a new commit has been made to your v0.1 branch, such that the history now looks something like:

abc123
2fsf34 
32t6e0
54r346
765th4
9r34gg

Here abc123 is the SHA-1 for the revert commit which now sits on the HEAD of your v0.1 branch. This commit functionally undoes everything which the 765th4 commit introduced. This has the same effect as completely removing the 765th4 commit, without the potentially bad side effects.

If you really wanted to excise the 765th4 commit, you could doing an interactive rebase. However, this generally is not recommended for branches which you have already pushed to the remote. The reason is that rewriting history can cause problems for anyone besides yourself who might already be using the v0.1 branch. So, stick with git revert for a simple and safe option here.

Upvotes: 2

Related Questions