Git:合并到主文件,同时自动选择用分支覆盖主文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1295171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Merge to master while automatically choosing to overwrite master files with branch
提问by Mica
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.
我正在使用 Git 来跟踪我的文档乳胶源。我想让 master 分支充满适合最终用户发布的文档,所以当有人需要什么时,我可以切换到 master 分支,编译并分发文档。
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 branchinto master, I would like to pass some command to Git to say, "forget the merging, just use the the file from branchto 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.
当手册需要重大更新时,我会创建新分支。但是,当手册被批准时,它需要合并回母版。当从branch合并到master 时,我想通过一些命令给 Git 说,“忘记合并,只需使用来自branch的文件覆盖master 中的文件。” 有没有办法做到这一点?具体来说,我想避免每次都打开合并工具。提前致谢。
回答by Robert
To disregard master
, when you have branch
checked out then:
无视master
,当你branch
签出然后:
git merge master --strategy=ours
http://schacon.github.com/git/git-merge.html
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.
正如“计算机语言学家”在这里评论的那样,这将“忽略来自 'master' 的所有内容,即使它对新的独立文件进行了更改”。因此,如果您不是 OP 并且想要更安全的合并,而不是 OP 所说的“忘记合并”,那么请使用来自“计算机语言学家”的这个出色的安全命令,并加上他的评论,以便他获得信任。
git merge -s recursive -X theirs branch
回答by Alan W. Smith
In version 1.7.1 of Git, you can use "-Xtheirs" to merge in the branch itself.
在 Git 1.7.1 版本中,您可以使用“-Xtheirs”来合并分支本身。
For example, if you start in your master branch, starting in master
例如,如果您从 master 分支开始,则从 master 开始
git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git merge -Xtheirs editBranch
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 postis to do a hard reset off the editBranch. For example:
我根据这篇文章看到的另一种方法是对 editBranch 进行硬重置。例如:
git checkout -b editBranch
-- edit your files --
git add .
git commit -m "Updated the files"
git checkout master
git reset --hard editBranch
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.
我认为第二种方式可能是更好的玩法,但我还没有机会玩够。
回答by quark
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 currentworking 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 questionfor a way to do it. In practice what it boils down to is resetting master
to point at branch
.
但这并不完全符合您的要求:它会覆盖当前工作分支的内容,branch
而您想要的是将相同的内容放到master
. 您真正想要的是合并策略theirs
,为此我向您指出了这个类似的问题,以寻求一种方法。实际上,它归结为重置master
为指向branch
.