1

I have a master branch and development branch in Git. I don't want any merge complications -- I know that the master branch should be exactly identical to the development branch.

How can I forcefully replace the master branch with the contents of the development branch, without any merge prompts and without any merge effort?

This is one idea I have, but I would like to know if it's the best solution before attempting it:

git checkout master
git merge -X theirs dev_branch
git push origin master

One requirement is that it keeps the history of the master branch so I can always go back to a previous commit of the master branch if needed.

ktm5124
  • 11,861
  • 21
  • 74
  • 119
  • Don't `merge`. You want `reset` – William Pursell Jan 31 '22 at 18:54
  • @WilliamPursell Why is reset better than merge? It sounds like both approaches work – ktm5124 Jan 31 '22 at 18:55
  • 1
    You're not 'replacing' the contents of the master branch. Rather, you are just changing the commit that `master` references. – William Pursell Jan 31 '22 at 18:55
  • 2
    Both approaches work, but if you want to avoid merge conflics then `reset` is much simpler. – William Pursell Jan 31 '22 at 18:56
  • 2
    The key question here is if you want to keep the history of the master branch prior to overwriting it with the development branch. – Lasse V. Karlsen Jan 31 '22 at 18:59
  • @LasseV.Karlsen Which solution keeps the history? I do want to keep the history – ktm5124 Jan 31 '22 at 18:59
  • I'm tempted to flag this question as a duplicate of [this one](https://stackoverflow.com/questions/28774194/reset-revert-a-whole-branch-to-another-branches-state). Guys, thoughts? – Romain Valeri Jan 31 '22 at 19:02
  • @RomainValeri It's not a duplicate because it emphasizes the need to keep history and also because it asks about a specific solution, the merge -s theirs strategy – ktm5124 Jan 31 '22 at 19:04
  • 2
    A merge solution will keep the history. A reset won't. A reset will simply move the branch marker to point to the same commit as the development branch, and any commits on the master branch that wasn't on the development branch will thus be "lost". You can of course create a new branch on top of current master, to keep the reference, and then do a reset to move the branch. – Lasse V. Karlsen Jan 31 '22 at 19:07
  • @LasseV.Karlsen Thanks. Merge makes the most sense then. – ktm5124 Jan 31 '22 at 19:08
  • Does this answer your question? [GIT replace branch's contents](https://stackoverflow.com/questions/9055793/git-replace-branchs-contents) – miken32 Feb 10 '23 at 16:16

1 Answers1

0

The solution is simple and straightforward. In order to keep the history of the master branch, so I can checkout a previous commit if needed, I use the merge approach.

git checkout master
git merge -X theirs dev
git push origin master

This merges the dev branch into the master branch and prevents all merge conflicts.

ktm5124
  • 11,861
  • 21
  • 74
  • 119