我正在调整基地在一次git拉之后,调整基地。我有一些文件合并冲突。我如何接受特定文件的“他们的”更改或“我的”更改?

$ 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

通常我只是打开文件或合并工具,手动接受所有“他们的”或“我的”更改。但是,我怀疑我错过了一个方便的git命令。

另外,请注意,只有当我看到哪些文件发生冲突或冲突可能是什么时,我才能为每个文件选择合并策略。


当前回答

对于您获得的每个冲突文件,您可以指定

git checkout --ours -- <paths>
# or
git checkout --theirs -- <paths>

从git签出文档

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 --ours -- <paths>
# or
git checkout --theirs -- <paths>

从git签出文档

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—我们的|—他们的将完全覆盖文件,通过选择他们的或我们的版本,这可能是或可能不是您想要做的(如果您有来自另一端的任何不冲突的更改,它们将丢失)。

如果你想在文件上执行一个三向合并,并且只使用——我们的|——他们的解决冲突的块,同时保持来自双方的不冲突的块,你可能想要诉诸于git merge-file;详见这个答案。

尽管已经回答了这个问题,但还是提供了一个例子,说明在git rebase和merge的情况下,“他们的”和“我们的”意味着什么。请看这个链接

Git变基 他们的实际是目前的分支,在情况下的调整。因此下面这组命令实际上是通过远程分支接受您当前的分支更改。

# see current branch
$ git branch
... 
* branch-a
# rebase preferring current branch changes during conflicts
$ git rebase -X theirs branch-b

Git合并 对于合并,他们和我们的意义是颠倒的。因此,为了在合并过程中获得相同的效果,即保持您当前的分支更改(我们的)超过被合并的远程分支(他们的)。

# assuming branch-a is our current version
$ git merge -X ours branch-b  # <- ours: branch-a, theirs: branch-b

引用@user456814从上面的评论:

git rebase -s递归-X <ours/their > 或 git merge -s recursive -X <ours/their > 请记住,对于rebase,“our”和“their”是颠倒的 他们在合并期间

@user456814在2014年的评论中给出了这个有用的答案。我在这里将它作为一个社区wiki来呈现,以便于查找、评估、更新等等,因为在这方面评论是有限的。