7

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
  • 128,036
  • 21
  • 144
  • 167
Wendy Adi
  • 1,397
  • 3
  • 18
  • 26

3 Answers3

15

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
  • 17,443
  • 4
  • 47
  • 54
1

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
  • 128,036
  • 21
  • 144
  • 167
0

One more option how to resolve the case when you need to merge a branch that was previously merged and reverted, is - switch to that feature branch and squash commits (this is needed to generate new commit hash), you can do this by git reset --soft HEAD~2 (2 is total number of commits on that branch) after this, you can push your changes to feature branch by git push (you may need either the --force or --force-with-lease flag). Now when you try to create merge request you will see that all changes are there and git sees it as a new changes (because of new commit hash)

stephematician
  • 844
  • 6
  • 17