4

Lets say I have two branches A and B

(A)--------(A+B)--------------(A+B+R)
 \        / merge   \ revert  /
  \      /           \       /
 (B)----(B+)         (R)----

First, I merged branch B to A, Then I reverted the merge request with GitHub's revert feature.

Now when I fix some code on branch B and need to merge to A again, almost all changes (except the new one that I fix) are ignored. How can I get the changes again?

CodeWizard
  • 110,388
  • 20
  • 126
  • 153
Wendy Adi
  • 1,187
  • 3
  • 16
  • 26

2 Answers2

11

You need to revert the revert, i.e. revert the commit that your previous revert created.

The reason for this is that a merge really does 2 things: it changes the file contents to reflect the merge, and also creates a commit with 2 parents to tell git what was merged. When you revert, it undoes the first thing, but not the second. So when you try to re-do the merge, git has no idea that you reverted the previous merge, so it ignores everything before then.

David Deutsch
  • 15,353
  • 4
  • 45
  • 52
2

Read here how to "reset" your changes How to move HEAD back to a previous location? (Detached head)

Once you have the code you want it push it again to the repository. Since you did a revert and not reset you can simply push the code without any problem.

If you have done a reset and you wish to update the remote branch you will have to force push with the git push -f origin master and this will result in a rebase which will affect all your co workers as well.


How can i get the changes again?

git cherry-pick

The easiest way is simply to do a git cherry-pick to pick the desired commit back to your branch again.

# Find out the range of commits you wish to re-add to your branch.
# then use cherry-pick to add them back to the branch

git cherry-pick start..end

# If you wish to include the start commit as well add the ^
# This will result in a cherry-pick of the start commit included as well 
git cherry-pick start^..end

git rebase --onto (Carefull = rebase)

# reset it to the start commit
git reset --hard start

# rebase every commit after b and transplant it onto a
git rebase --onto commit1 commit2 commit3 ... commitN

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 110,388
  • 20
  • 126
  • 153