2

How could I remove a branch I had merged into master in the past ?

From something like :

master
... a---> b ---> c ---------> d -----> e ---> f ---> g ---> h
                  \                   /
                    x ---> y ------> z
                     branch1

to :

... a---> b ---> c ---------> d -----> f ---> g ---> h
                  \                
                    x ---> y ---> z
                     branch1

I want to undo/remove merging of a branch into master I had done sometime earlier.


I have tried out something like but am getting conflicts. Is it possible ?

# currently we are in <h> commit in master branch
git checkout master   

# new temporary branch
git branch tmp_master 

git checkout tmp_master

# reseting temp. branch to reqd. commit
git reset <d> --hard  

# cherry-picking subsequent commits
git cherry-pick <f>..<h> 

After it was done --as I had expected to do :

# change temp. branch to master branch
git branch -D master

git branch -m master
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
maan81
  • 3,469
  • 8
  • 36
  • 53
  • 3
    Look into [rebasing](https://git-scm.com/docs/git-rebase) – vrwim Sep 15 '16 at 11:50
  • 1
    Seconded. Do an interactive rebase of master and delete commit `e`. Ditto with `branch1`. – Mad Physicist Sep 15 '16 at 11:51
  • Check out this question for a similar situation: http://stackoverflow.com/questions/17577409/git-remove-merge-commit-from-history – alexbclay Sep 15 '16 at 11:52
  • Although if branch 1 hasn't moved after the commit, you can just reset it back one. – Mad Physicist Sep 15 '16 at 11:52
  • 1
    You're getting *errors* or *conflicts*? If actual errors, then please share those, otherwise your approach is sound and likely what I would do. – Jeff Puckett Sep 15 '16 at 11:53
  • @JeffPuckettII conflicts. As there number of commits, as I go resolving a conflict & `rebase --continue`, conflict for the next commit appears. Do I need to handle the same conflict in each step ? – maan81 Sep 15 '16 at 12:12
  • Use [rerere](https://git-scm.com/blog/2010/03/08/rerere.html) so you don't have to keep resolving the same conflict. – Jeff Puckett Sep 15 '16 at 13:19

2 Answers2

1

You either need to:

  • rebase --interactive (which would allow you to drop "e", the merge commit),
  • or git revert -m 1 e (see "Undoing Merges "), which creates a new commit cancelling the changes introduced by the merge.

The second solution allows for a simple push (you are pushing new commits), while the first solution would require a push --force (problematic in case of multiple user collaborating on the same remote repo)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

git rebase --onto d e will remove e from the branch history of h. See here for more information.

kfb
  • 6,252
  • 6
  • 40
  • 51
  • As there number of commits, as I go resolving a conflict & `rebase --continue`, conflict for the next commit appears. Do I need to handle the same conflict in each step ? – maan81 Sep 15 '16 at 12:13
  • 1
    If subsequent commits depend upon something that was removed in `e`, then you will have to fix them up individually as far as I'm aware. However, someone else may know of a faster way (that I would definitely be interested in myself!). – kfb Sep 15 '16 at 12:34