为特定文件(“我们的”、“我的”、“他们的”)选择 Git 合并策略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16825849/
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
Choose Git merge strategy for specific files ("ours", "mine", "theirs")
提问by Steven Wexler
I am in the middle of rebasing after a git pull --rebase
. I have a few files that have merge conflicts. How can I accept "their" changes or "my" changes for specific files?
在git pull --rebase
. 我有几个文件存在合并冲突。我如何接受特定文件的“他们的”更改或“我的”更改?
$ git status
# Not currently on any branch.
# You are currently rebasing.
# (fix conflicts and then run "git rebase --continue")
# (use "git rebase --skip" to skip this patch)
# (use "git rebase --abort" to check out the original branch)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: CorrectlyMergedFile
#
# Unmerged paths:
# (use "git reset HEAD <file>..." to unstage)
# (use "git add <file>..." to mark resolution)
#
# both modified: FileWhereIWantToAcceptTheirChanges
# both modified: FileWhereIWantToAcceptMyChanges
Normally I just open the file or a merge tool and manually accept all "their" or "my" changes. However, I suspect I'm missing a convenient git command.
通常我只是打开文件或合并工具并手动接受所有“他们的”或“我的”更改。但是,我怀疑我缺少一个方便的 git 命令。
Also, note that I will only be able to choose a merge strategy for each file when I see what files hit conflicts an possibly what the conflicts are.
另外,请注意,只有当我看到哪些文件遇到冲突以及冲突可能是什么时,我才能为每个文件选择合并策略。
回答by
For each conflicted file you get, you can specify
对于您获得的每个冲突文件,您可以指定
git checkout --ours -- <paths>
# or
git checkout --theirs -- <paths>
From the git checkout
docs
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
--ours
--theirs
When checking out paths from the index, check out stage #2 (ours
) or #3 (theirs
) for unmerged paths.The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using
-f
will ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using--ours
or--theirs
. With-m
, changes made to the working tree file can be discarded to re-create the original conflicted merge result.
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
--ours
--theirs
从索引中检出路径时,检出阶段 #2 (ours
) 或 #3 (theirs
) 以获取未合并的路径。由于先前的合并失败,索引可能包含未合并的条目。默认情况下,如果您尝试从索引中检出这样的条目,检出操作将失败并且不会检出任何内容。使用
-f
将忽略这些未合并的条目。可以使用--ours
或将来自合并特定一侧的内容从索引中检出--theirs
。使用-m
,可以放弃对工作树文件所做的更改以重新创建原始的冲突合并结果。
回答by Abe
Even though this question is answered, providing an example as to what "theirs" and "ours" means in the case of git rebase vs merge. See this link
即使回答了这个问题,也提供了一个示例,说明在 git rebase 与 merge 的情况下“他们的”和“我们的”是什么意思。看这个链接
Git Rebasetheirs
is actually the current branch in the case of rebase. So the below set of commands are actually accepting your current branch changes over the remote branch.
theirs
在rebase的情况下,Git Rebase实际上是当前分支。因此,下面的命令集实际上是在远程分支上接受您当前的分支更改。
# see current branch
$ git branch
...
* branch-a
# rebase preferring current branch changes during conflicts
$ git rebase -X theirs branch-b
Git Merge
For merge, the meaning of theirs
and ours
is reversed. So, to get the same effect during a merge, i.e., keep your current branch changes (ours
) over the remote branch being merged (theirs
).
Git的合并
对于合并的意义theirs
和ours
反转。因此,要在合并期间获得相同的效果,即,将当前分支更改 ( ours
)保持在正在合并的远程分支( ) 之上theirs
。
# assuming branch-a is our current version
$ git merge -X ours branch-b # <- ours: branch-a, theirs: branch-b
回答by jakub.g
Note that git checkout --ours|--theirs
will overwrite the files entirely, by choosing either theirs
or ours
version, which might be or might not be what you want to do (if you have any non-conflicted changes coming from the other side, they will be lost).
请注意,通过选择或版本,git checkout --ours|--theirs
将完全覆盖文件,这可能是您想要做的,也可能不是(如果您有任何来自另一端的非冲突更改,它们将丢失)。theirs
ours
If instead you want to perform a three-way merge on the file, and only resolve the conflicted hunksusing --ours|--theirs
, while keeping non-conflicted hunksfrom both sides in place, you may want to resort to git merge-file
; see details in this answer.
相反,如果你想对文件执行三路合并,只有解决冲突的帅哥使用--ours|--theirs
,同时保持非冲突的帅哥从到位两侧,你可能要诉诸git merge-file
; 请参阅此答案中的详细信息。