如何解决git合并冲突以支持拉取的更改?
我希望从工作树中删除所有冲突的更改,而不必使用gitmergetool处理所有冲突,同时保持所有无冲突的更改。最好是,我想在拉的时候做,而不是事后。
如何解决git合并冲突以支持拉取的更改?
我希望从工作树中删除所有冲突的更改,而不必使用gitmergetool处理所有冲突,同时保持所有无冲突的更改。最好是,我想在拉的时候做,而不是事后。
当前回答
请注意,有时这是行不通的:
git checkout--我们的路径/to/file
or
git checkout--他们的路径/to/file
我改为这样做,假设HEAD是我们的,MERGE_HEAD是他们的
git checkout HEAD -- path/to/file
or:
git checkout MERGE_HEAD -- path/to/file
在我们这样做之后,我们很好:
git add .
如果你想了解更多,请点击此处查看精彩的torek帖子:git checkout--我们不会从未合并文件列表中删除文件
其他回答
您可以使用递归的“他们的”策略选项:
git merge --strategy-option theirs
来自男子:
ours
This option forces conflicting hunks to be auto-resolved cleanly by
favoring our version. Changes from the other tree that do not
conflict with our side are reflected to the merge result.
This should not be confused with the ours merge strategy, which does
not even look at what the other tree contains at all. It discards
everything the other tree did, declaring our history contains all that
happened in it.
theirs
This is opposite of ours.
注意:正如手册页所说,“我们”的合并策略选项与“我们”合并策略非常不同。
2023年更新!
如果您想接受所有当前更改并忽略任何传入的更改,可以执行以下操作:
git merge [branch] --strategy-option ours
[branch]应替换为要合并到当前分支中的分支的名称。
相反,如果您知道要覆盖任何当前更改并接受来自传入更改的所有冲突,则可以使用他们的策略:
git merge [branch] --strategy-option theirs
在Emacs使用smerge模式中,为了使用我的或他们的冲突标记来解决所有冲突标记,我们可以定义:
(defun aj/smerge-keep-mine-all ()
""
(interactive)
(save-excursion
(beginning-of-buffer)
(while (ignore-errors 'user-error (progn (smerge-next) t))
(smerge-keep-mine))))
(defun aj/smerge-keep-other-all ()
""
(interactive)
(save-excursion
(beginning-of-buffer)
(while (ignore-errors 'user-error (progn (smerge-next) t))
(smerge-keep-other))))
git pull-X他们的答案可能会创建一个难看的合并提交,或者发出
错误:您对以下文件的本地更改将被合并覆盖:
如果您想简单地忽略对repo中文件的任何本地修改,例如,在始终应为源镜像的客户端上,请运行以下命令(用所需的分支替换master):
git fetch && git reset --hard origin/master
它是如何工作的?git fetch执行git pull,但不合并。然后gitreset-hard使您的工作树与上一次提交相匹配。您对回购中文件的所有本地更改都将被丢弃,但新的本地文件将被保留。
要解决与特定分支中版本的所有冲突,请执行以下操作:
git diff --name-only --diff-filter=U | xargs git checkout ${branchName}
因此,如果您已经处于合并状态,并且希望保留冲突文件的主版本:
git diff --name-only --diff-filter=U | xargs git checkout master