Undoing the merge in Git so that no commits from the other branch exist

I’ve two branches: development, and bug_fixes, and I accidentally merged development branch in bug_fixes two to three weeks ago, and have been working on bug_fixes since then, and I’ve pushed the changes in bug_fixes.

Now, I need to un-merge the development branch. I want a pure bug_fixes branch with no commits form development branch. By googling, I’ve come up with the following scenarios:

  • git command to emit name of remote tracking branch
  • Is there an easy way to change to previously active branch?
  • Can I mark a branch as 'not going to push'?
  • git remote prune – didn't show as many pruned branches as I expected
  • git root branches… how do they work?
  • Mercurial - what is “future auditing”?
  • 1) Revert the commit that merged development branch in bug_fixes branch, but it partially did the job. The commits from development branch can still be seen in bug_fixes branch, something that isn’t required.

    2) Rebase, and delete the commit that did the merging, but it still has the same issue described in point 1. bug_fixes branch still contains the commits from development branch.

    Is there a way I can delete the merging commit, and all those commits that came in bug_fixes branch form development branch, would be deleted as well? Or, it isn’t possible?

  • git-p4 — any great explanations for how it works
  • Gitignore exclude certain files in all subdirectories
  • SSH key passphrase with git pull using Fabric
  • git tfs: How to rcheckin git commits when there is another checkin in tfs?
  • manage multiple github repos from the same system and different user names
  • Git push upstream
  • 4 Solutions collect form web for “Undoing the merge in Git so that no commits from the other branch exist”

    IF you know which commits are coming from the development branch (in addition of the merge commit), you can do a git rebase -i (interactive rebase) from the last good commit of bug fixes:

     b--b--(B)--M--d--b--d--b--b (bugfix)
               /
        d--d--d  (devel)
    
    git checkout bugfix
    git rebase -i (B)
    # drop M, d, and d
    
     b--b--(B)--b'--b'--b' (bugfix)
    
        d--d--d  (devel)
    

    The more complex alternative would be a git filter-branch, but hopefully, the interactive rebase is enough.

    In the OP’s case, the full command was:

    git rebase -p -i (b)
    

    -p allows to preserve the merge commit.

    I would try the following approach:

    First of all, make a list of the commits in both branches:

    git checkout development
    git log --oneline > log-devel.txt
    
    git checkout bug_fixing
    git log --oneline > log-bugfixing.txt
    

    Second, identify in each branch the id of the last commit before executing the merge. Let’s name these commits devel-0 and bugfixing-0
    Then, take your branches to the status they were before the accidental merge:

    git checkout development
    git reset --hard <devel-0-hash>
    
    git checkout bug_fixing
    git reset --hard <bugfixin-0-hash>
    

    At this point you have two clean but outdated git branches. Now, you should take from log-devel.txt and log-bugfixing.txt all the commits that are not included in the current branches, and add them to the corresponding branch. In order to add a commit with id abc0123 to your branch development you can use cherry pick:

    git checkout development
    git cherry-pick `abc0123`
    

    Take into account that the commits should better be added to the clean branches in the same order they were added to the bug fixing branch, in order to minimize the number of conflicts appearing.

    you need to know your latest commit message for bug_fixes before merge, if you know the sha hash then evne better.

    now first you need to find the sha hash using the command

    $ git reflog

    the output is something like

    1fb5738 HEAD@{10}: commit…

    4d47df6 HEAD@{11}: commit…

    5f32c4b HEAD@{12}: merge…

    428f91d HEAD@{13}: checkout…

    5f32c4b HEAD@{14}: checkout…

    the first part is the sha hash. In this case the state before merge has hash value 428f91d

    so now, in bug_fixes branch we reset to this hash value

    git reset –hard 428f91d

    now it is undone

    Let me describe the scenario with an example below

    Bug Fix branch: Revision 10

    Merged Dev Branch: Revision 11

    Multiple changes after merge: Revision 12 – Revision 15

    Now there are 2 steps that needs to be done(Easy method Without git native commands)

    1. Go back to Revision 10

      Clone Latest code to a folder called “latest”

      Clone Revision 10 to a folder called “rev10”

      Remove all the content(except .git folder) from latest folder

      Copy all the content(except .git folder) from rev10 folder to latest folder

      Checking the latest folder content

    Now the Bug fix branch will be having the content equal to Revision 10 without any developer branch changes

    1. Optional – Merging Revision 12 – Revision 15 to bug_fix branch

      As we have revert back to the old revision the latest commits to bugfix branch is also gone
      This revisions needs to be manually merged one by one back to bug fix branch

    Git Baby is a git and github fan, let's start git clone.