Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git force merge

Tags:

git

github

I am wondering what I am supposed to do in order to force a merge from a dev branch into my master branch? Using 'git merge dev' results in quite a number of conflicts. However, I don't want to handle them individually. Instead, I simply want to use all the files from my dev branch and merge it into master. But this "forced merge" is not as easy as I thought. Can somebody please point me into the right direction?

like image 763
Andi Avatar asked Jan 01 '23 14:01

Andi


1 Answers

I simply want to use all the files from my dev branch

If you mean you want to use all the files from your dev branch - i.e. any changes made on the master branch since the branches diverged should be undone - then there are a couple of ways.

merge -s ours

You could

git checkout dev
git merge -s ours master
git checkout master
git merge dev

This is a bit round-about, because there is no "theirs" strategy. (There is a "theirs" strategy option for the default merge strategy, but it would still try to apply changes from master as long as they don't conflict with changes from dev; if what you want is to just keep the dev versions, that won't work.) So you use the ours strategy to create a merge commit between master and dev with the dev versions of all files, then fast-forward master onto that merge.

That means the order of parents will be reversed on the merge commit, which is something you usually wouldn't notice, but it could matter in some cases (like if you use --first-parent on logs or something).

commit-tree

If the order of parents matter, you could resort the plumbing command commit-tree

git checkout master
git merge $(git commit-tree dev -p master -p dev -m "merging dev over master")

history rewrite

The potential down-side to the above methods is that they produce a merge commit that may not be intuitive, in that its result ignores the changes from one side of the merge (whereas a merge is expected to combine the changes from both sides). Because your merge would conflict under the normal strategy, it's not as big a problem as it could be, but still some people would categorize it as an "evil merge". If you want to avoid that, the other option is to rewrite history.

That has a down side as well, particularly if you use this repo to collaborate with other developers. You can read about the problem (and generally how to solve it) in the git rebase docs under "recovering from upstream rebase". (While the docs refer to the problem as "upstream rebase", it really applies to any history rewrite of a shared branch.)

If you decide that a history rewrite makes sense, then the simplest thing is just to move the master branch so that it points to the same commit as dev.

git checkout dev
git branch -f master
like image 138
Mark Adelsberger Avatar answered Jan 05 '23 10:01

Mark Adelsberger