【问题标题】:Resolving git conflicts: git mergetool doesn't work解决 git 冲突:git mergetool 不起作用
【发布时间】:2013-04-03 21:02:17
【问题描述】:

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

这是我的情况:

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

$ git pull:

错误:您对“foo.cpp”的本地更改将被合并覆盖。 中止。请在合并之前提交您的更改或存储它们。

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

How to resolve merge conflicts in Git?

不幸的是,这两种解决方案都无法正常工作。我可能会使用 stash(获取差异并使其干净)并进行手动解析,但我想使用 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 无法正常工作。它说不需要合并。但是,它显示了差异并且git pull 报告了冲突。

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

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

谢谢!

【问题讨论】:

  • 你需要的config命令是git config merge.tool diff3

标签: git conflict git-merge atlassian-sourcetree


【解决方案1】:

您看到的错误:

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

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

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

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

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

对于您的 Q2:SourceTree 不包含它自己的合并工具;你需要依赖另一个(我使用 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

【讨论】:

  • 这些 sourcetree 配置选项去哪儿了?在 bash 脚本中?
  • 使用git config --global difftool.sourcetree.cmd "/Applications/..."
  • git config --global difftool.sourcetree.cmd=/Applications/p4merge.app/Contents/MacOS/p4merge "$LOCAL" "$REMOTE" 错误:无效键:difftool.sourcetree.cmd= /Applications/p4merge.app/Contents/MacOS/p4merge
  • 抱歉,您需要引用 ...cmd='/Applications/... REMOTE"'
【解决方案2】:

您收到冲突消息是因为您对远程中已更改的文件进行了修改,该文件将被拉入。因此该消息将存储或提交您的更改。

如果您执行上述任何一项操作然后拉动,则可能会或可能不会有任何冲突需要解决。例如,如果您引入的更改与您的修改不在同一位置,则不会发生冲突。如果有合并工具应该可以处理它。

【讨论】:

  • 对不起。错误地编辑了你的;撤消了!
猜你喜欢
  • 2015-08-30
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
相关资源
最近更新 更多