Question

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.

Was it helpful?

Solution

(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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top