Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

merge

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.

like image 624
Mica Avatar asked Aug 18 '09 16:08

Mica


People also ask

Does merging master into branch change master?

git merge master will update your current branch with the changes from your local master branch, the state of which will be that of when you last pulled while on that branch.

Will git merge master overwrite my changes?

Usually git does not overwrite anything during merge.

What happens to branch after merge to master?

The answer is: nothing happens to the feature branch as a result of the merge. The new merge commit is added on the current branch (which is master when the merge is done).


2 Answers

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' commented here, 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 'Computer Linguist', and plus his comment up so he gets creds.

git merge -s recursive -X theirs branch 
like image 193
Robert Avatar answered Sep 22 '22 06:09

Robert


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.

like image 34
Alan W. Smith Avatar answered Sep 25 '22 06:09

Alan W. Smith