Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge in wrong direction

Tags:

git

merge

I have finished a lengthy merge of a devel branch onto master, then I realised it should have been a devel update instead, meaning the direction should be master -> devel. Is it possible to change the destination of the commit, so that the arrow is pointing from master to devel?

note: The resulting changes in files would be exactly same for both d->m and m->d. It's just the final destination (and arrow in history) which is wrong.

now: enter image description here desired: enter image description here

blue: master
red: devel

*Changes are not commited or pushed yet.

Recapitulation

  • I resolved all conflicts in a devel->master merge.
  • I haven't commited or pushed the changes.
  • Changes are made on top of master.
  • I need to apply the resolved conflicts in a master->devel direction.
like image 550
Qwerty Avatar asked Jul 28 '15 09:07

Qwerty


People also ask

Can git merge be reversed?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.

How do you reverse a merge conflict?

In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.

What is backward merge?

A reintegration merge, also known as a back merge is appropriate when the changes made in a branch need to be integrated back into the branch's ancestor.


2 Answers

Reverting a merge on the branch is as simple as determining which commit was the previous one.

First, checkout on the branch you want to modify. It's a git-golden-rule: you can only modify the branch you're on.

git checkout master

Then, figure out the previous-master commit it, and force master back into its previous state

git reset --hard <previous-commit-id>

Finally, do your merge right

git checkout devel
git merge master

What you could also do, if you want to be lazy, is invert branch states.

You still need to find master's previous commit-id.

Then:

git checkout devel
git reset --hard master
git checkout master
git reset --hard <previous-commit-id>
like image 98
blue112 Avatar answered Oct 25 '22 07:10

blue112


It's actually a bit simpler than anwser of @blue112 . First commit the master locally.

git checkout master
git commit

This will create a shared commit for master and devel. Then switch to devel and merge it also with master. This will result in a fast-forward merge as there aren't any conflicts left.

git checkout devel
git merge master

It should be save to push devel now.

git push devel origin/devel

Now revert master back to its previous commit, so it remains as it was before the merge:

git checkout master
git reset --hard <previous-commit-id>

Workspace should be clean and safe now.

like image 31
user11012304 Avatar answered Oct 25 '22 07:10

user11012304