3

I've stumbled upon this problem with my repo:

I have the following structure of my branch tree:

(branch)         a - b - c - d - e - f - g
               /           /
(master)  1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 ...

Here, I branched off from master at commit 1 when I created the branch, then merged master into branch at commit 5.

The trouble is: while merging, I discarded some changes and I would like to revert them, so that I can merge master into my branch again as if the merge at commit 5 never happened.

In other words, how do I undo effects of the merge on commit 5 (or commit d) without losing my changes in subsequent commits? (namely, e - f - g)

My goal is to then be able to merge master back into branch, but now I would be more careful with my merge.

Thanks.

  • Has your branch been shared with other people (pushed)? Or is it OK to rebase it? – Richard Hansen Jun 14 '13 at 03:38
  • It has been uploaded to the server, but no one touched it. Yes, it is OK to rebase it as long as I get to keep the changes. So, say, if you wanted to rebase to c, it is fine as long as I get to keep e - f - g, but lose d. – Levko Ivanchuk Jun 14 '13 at 03:40
  • It seems like this question is a duplicate of [this question](http://stackoverflow.com/q/4084013). Is it? – Richard Hansen Jun 14 '13 at 03:42
  • I already tried the approach in the answer. However, these two branches have been out of sync for a long time and I simply don't see commit d in the shell. However, the last answer seems to look promising, I will try that tomorrow. – Levko Ivanchuk Jun 14 '13 at 03:50

1 Answers1

3
(branch)         a - b - c - d - e - f - g
               /           /
(master)  1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 ...

git rebase --onto c d g
git checkout -b newbranch

(newbranch)                e' - f' - g'
                          /
(branch)         a - b - c - d - e - f - g
               /           /
(master)  1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 ...

If you are happy with the results, then throw away the old branch and rename the new branch:

git branch -D branch
git branch -m newbranch branch

Finally push the new branch.

git push -n origin branch # check what you're about to push
git push -f origin branch # -f is required to rewrite history; be careful
Peter Lundgren
  • 8,787
  • 2
  • 26
  • 21
  • I used your idea of creating a new branch, however after that I took a different approach.I basically cherry picked commits e - f - g ... from the branch into the newbranch. Thanks for directing me to the right track, though! – Levko Ivanchuk Jun 14 '13 at 16:54