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

unmerged:   some_file.txt

You are in the middle of a conflicted merge.

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


当前回答

另一种保留工作副本状态的方法是:

git stash
git merge --abort
git stash pop

我通常建议不要这样做,因为它实际上就像Subversion中的合并,因为它会在接下来的提交中丢弃分支关系。

其他回答

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

也没有特别需要重置并使用不同的策略执行合并。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 reset *currentBranchIntoWhichYouMerged* -- *fileToBeReset*

评论建议git reset-merge是git merge-ort的别名。值得注意的是,如果存在merge_HEAD,gitmerge--abort仅等同于gitreset-merge。这可以在git help for merge命令中读取。

当存在merge_HEAD时,gitmerge--abort等同于gitreset-merge。

合并失败后,如果没有merge_HEAD,则可以使用git-reset-mmerge撤消失败的合并,但不必使用gitmerge-art。它们不仅是同一事物的新旧语法。

就个人而言,我发现git reset-merge对于与所描述的场景类似的场景更为强大,并且通常合并失败。

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

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

希望这有帮助!!!

为了避免陷入这种麻烦,可以扩展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。最后删除测试和主测试。

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