Remove a merge/commit in git

git
87,534

Solution 1

If you want to completely remove it from you history, you could do the following:

  1. git rebase -i <commit to remove>^

  2. This will open your default editor (usually vi) with a list of commits, with the one you want to remove first. Remove it from the list, save, and quit. This should no rebase your branch without the commit you want to remove.

A "safer" approach is to leave the history in tact, so you can show that this feature used to exist, and was purposely removed. Just use git revert <commit to remove>. This will create an additional patch that undoes the commit you want to get rid of. The main advantage is that you can edit the commit message, and explain why this feature is being removed.

Solution 2

If you want to revert a merge commit, here is what you have to do.

  1. First, check the git log to find your merge commit's id. You'll also find multiple parent ids associated with the merge (see image below).

enter image description here

Note down the merge commit id shown in yellow. The parent IDs are the ones written in the next line as Merge: parent1 parent2. Now...

Short Story:

  1. Switch to branch on which the merge was made. Then Just do the git revert <merge commit id> -m 1 which will open a vi console for entering commit message. Write, save, exit, done!

Long story:

  1. Switch to branch on which the merge was made. In my case, it is the test branch and I'm trying to remove the feature/analytics-v3 branch from it.

  2. git revert is the command which reverts any commit. But there is a nasty trick when reverting a merge commit. You need to enter the -m flag otherwise it will fail. From here on, you need to decide whether you want to revert your branch and make it look like exactly it was on parent1 or parent2 via:

git revert <merge commit id> -m 1 (reverts to parent2)

git revert <merge commit id> -m 2 (reverts to parent1)

You can git log these parents to figure out which way you want to go and that's the root of all the confusion.

Share:
87,534

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin 3 months

    Using git I merged a branch to master and now after a few weeks and many commits I'd like to remove that feature/branch. How do I do it? it doesn't seem trivial

    • Mario
      Mario over 8 years
      It's not 100% clear to me what you want to do. Do you want to undo the merge commit or do you just want to remove the now obsolete branch?
  • Mario
    Mario over 8 years
    Sounds to me like undoing the merge might be the cleaner solution, the branch still exists anyway?
  • Mureinik
    Mureinik over 8 years
    Yes, such a change won't remove the branch.
  • barfuin
    barfuin almost 3 years
    The list of commits does not include the merge commit in my case.
  • Tio
    Tio about 2 years
    Just to say that the git revert worked perfectly on my case to rever an git merge of a feature branch onto another feature branch.
  • erb
    erb over 1 year
    @barfuin you probably forgot the caret at the end of the <commit to remove>^