我使用了git pull并发生了合并冲突:

unmerged:   some_file.txt

You are in the middle of a conflicted merge.

如何放弃对文件所做的更改,只保留已提取的更改?


当前回答

自从Git 1.6.1.3以来,Git checkout已经可以从合并的任何一侧进行签出:

git checkout --theirs _widget.html.erb

其他回答

在这个特定的用例中,您实际上并不想中止合并,只需以特定的方式解决冲突。

也没有特别需要重置并使用不同的策略执行合并。git已经正确地强调了冲突,接受其他方更改的要求仅适用于这一个文件。

对于冲突中的未合并文件,git会在索引中提供文件的公共基础、本地和远程版本。(这是git mergetool在三向diff工具中读取它们的地方。)您可以使用git show查看它们。

# common base:
git show :1:_widget.html.erb

# 'ours'
git show :2:_widget.html.erb

# 'theirs'
git show :3:_widget.html.erb

逐字使用远程版本解决冲突的最简单方法是:

git show :3:_widget.html.erb >_widget.html.erb
git add _widget.html.erb

或者,如果git>=1.6.1:

git checkout --theirs _widget.html.erb

为了避免陷入这种麻烦,可以扩展git合并-中止方法,并在合并之前创建一个单独的测试分支。

案例:你有一个主题分支,它没有被合并,因为你分心了/有些事情发生了/你知道,但它已经准备好了。

现在可以将其合并到master中吗?

在测试分支中工作以估计/找到解决方案,然后放弃测试分支并在主题分支中应用解决方案。

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# If it fails you can abandon the merge
git merge --abort
git checkout -
git branch -D test  # we don't care about this branch really...

努力解决冲突。

# Checkout the topic branch
git checkout topic-branch-1

# Create a _test_ branch on top of this
git checkout -b test

# Attempt to merge master
git merge master

# resolve conflicts, run it through tests, etc
# then
git commit <conflict-resolving>

# You *could* now even create a separate test branch on top of master
# and see if you are able to merge
git checkout master
git checkout -b master-test
git merge test

最后再次签出主题分支,从测试分支应用修复程序并继续执行PR。最后删除测试和主测试。

卷入的是的,但在我做好准备之前,它不会干扰我的主题或主分支。

由于您的拉动不成功,那么HEAD(而不是HEAD^)是分支上最后一个“有效”提交:

git reset --hard HEAD

你想要的另一件事是让他们的改变凌驾于你的改变之上。

旧版本的git允许您使用“他们的”合并策略:

git pull --strategy=theirs remote_branch

但正如Junio Hamano(Git维护者)在这条消息中所解释的那样,这条消息已经被删除。如链接中所述,您可以这样做:

git fetch origin
git reset --hard origin

可能不是OP想要的,但对我来说,我试图将一个稳定分支合并到一个功能分支,但冲突太多了。由于HEAD被多次提交更改,所以我没有成功重置更改,所以简单的解决方案是强制签出到一个稳定的分支。然后您可以签出到另一个分支,它将与合并前一样。

git checkout-f master

git结帐侧分支

如果最终出现合并冲突,并且没有任何要提交的内容,但仍显示合并错误。在应用所有下面提到的命令之后,

git reset --hard HEAD
git pull --strategy=theirs remote_branch
git fetch origin
git reset --hard origin

请删除

.git\index.lock

文件[恢复时剪切粘贴到其他位置],然后根据需要的版本输入以下任意命令。

git reset --hard HEAD
git reset --hard origin

希望这有帮助!!!