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

unmerged:   some_file.txt

You are in the middle of a conflicted merge.

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


当前回答

我想你需要重置一下。

请注意,git revert意味着与svn revert非常不同的东西-在Subversion中,该revert将丢弃您(未提交)的更改,将文件从存储库返回到当前版本,而git rever“撤消”提交。

gitreset应该执行与svnreset等效的操作,即丢弃不需要的更改。

其他回答

对于git>=1.6.1:

git merge --abort

对于旧版本的git,这将完成以下任务:

git reset --merge

or

git reset --hard

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

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 --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。最后删除测试和主测试。

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