Home > OS >  Git merging branches after unmerging
Git merging branches after unmerging

Time:04-28

I'm having problems with git merge. I have 2 branches, develop and feature. What I did:

  1. I merged feature into develop by mistake, and so I unmerged it ( git revert -m 1 )
  2. I merged develop into feature, but it deleted all the code changes in my feature branch, so I unmerged the merged ( git revert -m 1 )
  3. I try to create another merge from develop to feature, it shows no changes.

Now, how do I merged develop into my feature branch with all the code in develop and not lose code in feature?

CodePudding user response:

First thing, go back to the commit on develop before the one where you merged the feature branch. Then, force-push it to the hosted repository:

# check out the (local) develop branch first
git checkout develop
# go back the commit you want to end up in there
git reset --hard abc123def456
# push it to the remote
git push --force-with-lease origin HEAD:develop

Now, you still have the messed up feature branch. For that, you effectively do the same. Note that if you don't have a remote branch, the last step is left out.

That said, if you think you are missing some commits somewhere, check out git reflog. From that, you can re-trace most changes you made to the repo. If you find a commit there that's missing, you can git cherry-pick it into whichever branch you want.

CodePudding user response:

I think by now you understand that you can't merge the same commit IDs in again after reverting the merge, because those commit IDs are already in the history. In order to merge them in again you will need to give them new commit IDs.

In general, if you mess up something on a shared branch such as develop, then unless you catch it immediately, reset it and can force push out the fix before other people start using it or merging other things in, then reverting, like you did, is probably the best course of action there.

However, for your own feature branch, generally you would not need to revert since you can just rewrite your own branch however you please, without affecting anyone else. If you can rewrite your branch, then instead of merging the latest develop into it, it's slightly cleaner to just rebase your own branch onto the latest develop. If you hadn't already merged your branch into develop you could normally do this with your branch checked out already:

git fetch
git rebase origin/develop

You can do that anytime you wish. (I probably do it at least a few times per day in active repos.)

In your current situation, you won't be able to do that because the commit IDs on your branch are already in develop. So this first time, you'll need to do this instead (with your branch checked out already):

git fetch
git reset --hard <commit-before-merge-of-develop>
# Now use a fancy rebase to rewrite your branch on top of develop
git rebase --onto origin/develop <original-commit-from-develop>

Note that <commit-before-merge-of-develop> represents the commit ID on your branch before you merged in develop, and <original-commit-from-develop> represents the original merge-base of your branch with develop before you merged it into develop accidentally. In other words, this is the parent of the first new commit on your branch. The "fancy" rebase will rewrite all the new commit IDs on your branch so you can then merge them into develop again whenever you are ready.

  • Related