Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to force a merge in git?

Tags:

git

merge

I have a project with a dev branch and a production branch. Recently I've been working on a big new set of features in dev, so I haven't merged into production for about two weeks. In the meantime, though, there were some bugs that needed to be fixed in production.

For the most part I was able to make the fixes in dev and cherry-pick them into production. Sometimes, however, I would need to fix production by hand, as the fixes were substantially different in the two branches. Point is, the two branches have diverged a fair bit since they split.

Now I want to just push all of dev into production. I don't care about keeping any of the commits in production since the split, I just want production to look exactly like dev. [EDIT: I want production to look exactly like dev since the split, but don't want to rewrite history before the split] However, when I try to merge I get dozens of conflicts that I'd prefer not to fix by hand.

What's the best way to force a merge in git? Can I revert my production changes back to the split and then just fast-forward to the dev branch?

like image 820
Sam Fen Avatar asked Feb 24 '12 17:02

Sam Fen


People also ask

What is the best git merge strategy?

The most commonly used strategies are Fast Forward Merge and Recursive Merge. In this most commonly used merge strategy, history is just one straight line. When you create a branch, make some commits in that branch, the time you're ready to merge, there is no new merge on the master.

How do you trigger a merge conflict?

Merge conflicts can happen when merging a branch, rebasing a branch, or cherry picking a commit. If Git detects a conflict, it will highlight the conflicted area and ask which code you wish to keep. Once you tell Git which code you want, you can save the file and proceed with the merge, rebase, or cherry pick.

How do I force merge a pull request?

If the pull request has merge conflicts, or if you'd like to test the changes before merging, you can check out the pull request locally and merge it using the command line. You can't merge a draft pull request.


1 Answers

You can just push your dev branch onto the master repo production branch:

git push --force upstream-remote dev:production 

upstream-remote may just be origin if you're on a default clone.

Updated for mod'ed question:

You probably don't want to revert in the git sense but, yes, that's more or less what you want to do. Something like

git checkout -b merge <split hash> git merge dev git push --force origin merge:production 

<split hash> was the last commit on production that you want to keep.

like image 198
smparkes Avatar answered Sep 16 '22 12:09

smparkes