Git: Merge to master while automatically choosing to overwrite master files with branch

41,358

Solution 1

To disregard master, when you have branch checked out then:

git merge master --strategy=ours

http://schacon.github.com/git/git-merge.html

As 'Computer Linguist' Lilith River commented, this will "ignore everything from 'master', even if it has changes to new, independent files". So if you are not the OP and want a more safe merge that does not as the OP says "forget the merging", then use this excellent safe command from Lilith River, and upvote his comment up so he gets credit.

git merge -s recursive -X theirs <branch>

Solution 2

In version 1.7.1 of Git, you can use "-Xtheirs" to merge in the branch itself.

For example, if you start in your master branch, starting in master

git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git merge -Xtheirs editBranch

Another way that I've seen to do this based off this post is to do a hard reset off the editBranch. For example:

git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git reset --hard editBranch

I think this second way might be the better play, but I haven't had a chance to play around with it enough yet.

Solution 3

I believe you can do this with the 'ours' merge strategy:

git checkout branch
git merge -s ours master

But this doesn't do exactly what you want: it override the contents of the current working branch branch and what you want is to get the same contents onto master. What you really want is a merge strategy theirs, and for that I point you at this similar question for a way to do it. In practice what it boils down to is resetting master to point at branch.

Share:
41,358

Related videos on Youtube

Mica
Author by

Mica

words on words, sentences on sentences.

Updated on March 19, 2022

Comments

  • Mica
    Mica about 2 years

    I am using Git to track my documentation latex source. I want to keep the master branch full of documents that are suitable for end user release, so when someone needs something, i can just switch to the master branch, compile and hand out the document.

    I make new branches when a manual needs a major update. But, when the manual is approved, it needs to get merged back into the master. When merging from branch into master, I would like to pass some command to Git to say, "forget the merging, just use the the file from branch to overwrite the file in master." Is there a way to do this? Specifically, I want to avoid opening up a merge tool every time. Thanks in advance.

    • R. Martinho Fernandes
      R. Martinho Fernandes over 14 years
      You could do an interactive merge and spoon feed it (with yes) whatever the answer is when it asks what to do...
    • S B
      S B over 12 years
      @r-martinho-fernandes: What's the interactive merge command?
  • Mica
    Mica almost 14 years
    thanks. I'll have to update git at home, and check this out. Much appreciated.
  • Amala
    Amala over 13 years
    From here: stackoverflow.com/questions/366860/…, this says: Ours == I want to pull in another head, but throw away all of the changes that head introduces. So is opposite of what OP wanted.
  • Robert
    Robert over 13 years
    If you're in branch and you git merge master --strategy=ours then it keeps branch's changes. (what OP wants) But if you are in master and use "ours" then it keeps master's. (opposite)
  • Arne Claassen
    Arne Claassen over 13 years
    From my reading of the question, Mica wants to merge branch into master, keeping the state of branch i.e. git merge branch onto master, so --strategy=ours is the opposite of what he wants
  • Lilith River
    Lilith River over 12 years
    From branch 'master', running git merge -s recursive -X theirs branch will resolve all conflicts by picking the 'branch' version. using -s ours will ignore everything from 'master', even if it has changes to new, independent files.
  • S B
    S B over 12 years
    OP wants to target the merge to master, but @Robert's answer targets onto branch
  • John John Pichler
    John John Pichler about 10 years
    This answer became a lot confusing to me. That command are doing the opposite.
  • Dheeraj Bhaskar
    Dheeraj Bhaskar over 8 years
    Instead of the second command here, isn't it sufficient to do git merge branch while on master? IIRC it will be simply Fast-Forwarded. Am I missing something?
  • dougmacklin
    dougmacklin almost 7 years
    for anyone who is still confused who wants to overwrite all files on master with those on the branch (regardless of whether or not there are conflicts), see this answer on a duplicate question: stackoverflow.com/a/2862938/1179207