Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git merge: take everything from "theirs" branch

I have two branches, "private" and "public". I work on the private and periodically, I want to replace everything in "public" branch to have a clean copy of files in "private".

As I don't want the history of "private" go to "public", I use git merge --squash, but I still manually have solve merge files one by one. Is there a better way to do this?

like image 848
Fred Yang Avatar asked Feb 06 '13 14:02

Fred Yang


1 Answers

Squashing the merge will not change the fact that there are conflicts if the same lines have changed on both files of the commit.

To completely discard everything on the "public" branch and take over the exact state of "public", you can do one of these:

  1. Use the ours merge strategy (not the strategy option) as pointed out in several other commits:
    1.a assume you are on branch "private" (otherwise, do git checkout private)
    1.b git merge -s ours public
    1.c git checkout public
    1.d git merge --ff-only private
    1.e (optional: both branches are now pointing to the merge commit; if you want "private" to point to the commit before the merge, do:) git checkout private; git reset --hard private@{1} (Note that this uses the reflog and will only work like this if you havent changed "private" in the meantime)

  2. Check out the "theirs" side manually:
    2.a assume you are on branch "public" (otherwise, git checkout public)
    2.b git merge --no-commit private will prepare a merge-commit, but stop before committing no matter if there is a merge conflict or not
    2.c git checkout -f private -- . to check out the current state of "private"
    2.d git add . to mark conflicts as resolved (if any)
    2.e git commit to save the merge commit; "public" will now have the exact same contents as "private"

Edit: Note that this is the inverted case of How do I 'overwrite', rather than 'merge', a branch on another branch in Git?

Edit 2: If you want to hide your single commits of public, you can use the --squash switch here too, of course (in steps 1.b and 2.b, respectively)

like image 180
Nevik Rehnel Avatar answered Sep 19 '22 11:09

Nevik Rehnel