git - 为特定文件选择 Git merge 策略 ("ours", "mine", "theirs")

标签 git git-rebase git-merge-conflict

git pull --rebase 之后,我正处于 rebase 过程中。我有几个文件存在 merge 冲突。我如何接受“他们的”更改或“我的”对特定文件的更改?

$ 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

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

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

最佳答案

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

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

来自 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 - 为特定文件选择 Git merge 策略 ("ours", "mine", "theirs"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825849/

相关文章:

python - Git merge 意外失败

git - 从另一个分支上的选定提交创建一个 git 分支

git - 有没有办法恢复在 rebase 期间意外跳过的提交?

git - 解决 SmartGit 中的冲突 - 查找冲突文件

git - 如何将特定文件从一个 git 存储库复制到另一个 git 存储库,并保留历史记录

git - 如何简单的解释 "git pull --rebase"?

git - 冲突解决后如何将文件移回冲突状态?

windows - 如何从局域网内的其他电脑在 windows 中 git clone 一个 repo?

node.js - 通过 NPM 安装私有(private)依赖项

git - git rm cached 和 git reset HEAD 之间的区别