git - 解决 git 冲突 : git mergetool doesn't work

标签 git conflict git-merge atlassian-sourcetree

我需要解决git中的冲突,但是作为一个新手,不知道什么是最有效的方法。

这是我的情况:

enter image description here

我在 master 中,需要提交并推送我的所有更改。

$ git pull:

error: Your local changes to 'foo.cpp' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.

有几个冲突的文件。当然,我在网上搜索了一下:

How to resolve merge conflicts in Git?

不幸的是,这两种解决方案都效果不佳。我可能会使用 stash(获取 diff 并使其干净)并进行手动解析,但我想使用 git mergetool 或 SourceTree。

这是我试过的:

$ git config merge.conflictstyle diff3
$ git mergetool
merge tool candidates: tortoisemerge emerge vimdiff
No files need merging
$ git mergetool -t vimdiff
No files need merging
$ git mergetool -t vimdiff foo
foo.cpp: file does not need merging
$ git diff foo.cpp
diff --git a/foo.cpp b/foo.cpp
index xxxxxx yyyyy
--- a/foo.cpp
+++ b/foo.cpp
@@ .....
..... <omitted>
$

我不确定哪里出了问题。 git mergetool 无法正常工作。它说不需要 merge 。但是,它显示差异并且 git pull 报告冲突。

Q1) 如何使用 mergetool 进行交互式冲突解决或 merge ?

Q2)是否可以使用SourceTree来解决冲突?我试过了,但也不直观。

谢谢!

最佳答案

您看到的错误:

error: Your local changes to 'foo.cpp' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.

是因为您有未提交的更改(它在 SourceTree 显示的最顶部显示)。如果您有未提交的更改,任何 merge 尝试都将失败。您有三个选择:

  1. 取消所有更改(使用:git reset --hard)
  2. 存储所有更改(使用:git stash)
  3. 提交所有更改(使用:git add ...; git commit ...)

完成后,您将被允许 merge 。如果您选择上面的选项#3,那么可能会出现冲突;选项#1 或#2 不会有冲突。 [注意:对于选项 #2,您将稍后执行 git stash pop,然后可能会出现冲突。]

对于您的 Q1:执行 git config merge.tool diff3(或 vimdiff,或其他)。 merge 可能会造成混淆(它通常是 3 向 merge ,因此该工具显示三个版本和一个组合版本)。

对于您的问题 2:SourceTree 不包含它自己的 merge 工具;你需要依赖另一个(我使用 p4merge 和 SourceTree)。使用这些配置选项:

difftool.sourcetree.cmd=/Applications/p4merge.app/Contents/MacOS/p4merge "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/p4merge.app/Contents/MacOS/p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
mergetool.sourcetree.trustexitcode=true

关于git - 解决 git 冲突 : git mergetool doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798063/

相关文章:

git - 由于 gitattributes eol 设置覆盖未暂存的提交

java - 使用 OPCFoundation/UA-Java 项目的 OPC UA 模拟器工作示例

version-control - 如何解决 TFS 中的冲突?

svn - KDiff3如何与TortoiseSVN正确使用来解决冲突?

.net - 服务器端冲突解决,Azure 移动服务(.NET 后端)

java - Maven:在 pom.xml 中编译外部依赖项

git - gitpython-检查分支是否未与 repo 对象 merge ?

Git rebase 分支,以便提交和未暂存的更改不会被删除或丢失

Git merge repo 之外的文件内容(--no-index)

git - 显示修改过的文件,删除(或重命名)的文件除外