解决Git合并冲突以利于拉动期间的更改

本文翻译自:Resolve Git merge conflicts in favor of their changes during a pull

How do I resolve a git merge conflict in favor of pulled changes? 我该如何解决git merge冲突以利于更改?

Basically I need to remove all conflicting changes from a working tree without having to go through all of the conflicts with a git mergetool while keeping all conflict-free changes. 基本上,我需要从工作树中删除所有冲突的更改,而不必使用git mergetool经历所有冲突,同时保留所有无冲突的更改。 Preferably doing this while pulling, not afterwards. 最好在拉动时这样做,而不是在之后。


#1楼

参考:https://stackoom.com/question/istj/解决Git合并冲突以利于拉动期间的更改


#2楼

You can use the recursive "theirs" strategy option : 您可以使用递归“他们的”策略选项

git merge --strategy-option theirs

From the man : 男人

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.

Note: as the man page says, the "ours" merge strategy option is very different from the "theirs" merge strategy . 注意:正如手册页所述,“我们的”合并策略选项与“他们的”合并策略非常不同。


#3楼

git pull -s recursive -X theirs <remoterepo or other repo>

Or, simply, for the default repository: 或者,简单地,对于默认存储库:

git pull -X theirs

If you're already in conflicted state... 如果您已经处于冲突状态...

git checkout --theirs path/to/file

#4楼

OK so, picture the scenario I was just in: 好了,想象一下我刚刚遇到的情况:

You attempt a merge , or maybe a cherry-pick , and you're stopped with 您尝试进行merge ,或者cherry-pick ,但是您停止了

$ git cherry-pick 1023e24
error: could not apply 1023e24... [Commit Message]
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

Now, you view the conflicted file and you really don't want to keep your changes. 现在,您查看冲突的文件,而您确实不想保留所做的更改。 In my case above, the file was conflicted on just a newline my IDE had auto-added. 在上面的例子中,文件在我的IDE自动添加的换行符上发生冲突。 To undo your changes and accept their's, the easiest way is: 要撤消更改并接受更改,最简单的方法是:

git checkout --theirs path/to/the/conflicted_file.php
git add path/to/the/conflicted_file.php

The converse of this (to overwrite the incoming version with your version) is 相反(用您的版本覆盖传入的版本)是

git checkout --ours path/to/the/conflicted_file.php
git add path/to/the/conflicted_file.php

Surprisingly, I couldn't find this answer very easily on the Net. 令人惊讶的是,我在网络上找不到这个答案。


#5楼

To resolve all conflicts with the version in a particular branch: 要解决与特定分支中的版本的所有冲突:

git diff --name-only --diff-filter=U | xargs git checkout ${branchName}

So, if you are already in the merging state, and you want to keep the master version of the conflicting files: 因此,如果您已经处于合并状态,并且想要保留冲突文件的主版本:

git diff --name-only --diff-filter=U | xargs git checkout master

#6楼

If you're already in conflicted state, and you want to just accept all of theirs: 如果您已经处于冲突状态,并且只想接受他们所有的状态:

git checkout --theirs .
git add .

If you want to do the opposite: 如果要执行相反的操作:

git checkout --ours .
git add .

This is pretty drastic, so make sure you really want to wipe everything out like this before doing it. 这太剧烈了,因此请确保您确实要在执行此操作之前先将所有内容清除掉。

发布了0 篇原创文章 · 获赞 73 · 访问量 55万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105292910
今日推荐