Search code examples
gitconflictgit-mergeatlassian-sourcetree

Resolving git conflicts: git mergetool doesn't work


I need to resolve conflicts in git, but as a novice, not sure what is the most effective way.

This is my situation:

enter image description here

I'm in master and need to commit and push all my changes.

$ 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.

There are several conflicted files. Of course, I searched the web:

How to resolve merge conflicts in Git?

Unfortunately, neither solutions work well. I may use stash (taking diff and make it clean) and do manual resolving, but want to use git mergetool or SourceTree.

This is what I've tried:

$ 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>
$

I'm not sure what is wrong. git mergetool doesn't work correctly. It says no need to merge. However, it shows the differences and git pull reports conflict.

Q1) How can I use mergetool for interactive conflict resolving or merge?

Q2) Is it possible to use SourceTree to resolve conflicts? I tried, but it's also not intuitive.

Thank you!


Solution

  • The error that you are seeing:

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

    is because you have uncommitted changes (it says this at the very top of the SourceTree display). Any attempt to merge will fail if you have uncommited changes. You have three choices:

    1. Axe all the changes (Use: git reset --hard)
    2. Stash all the changes (Use: git stash)
    3. Commit all the changes (use: git add ...; git commit ...)

    After you've done that you will be allowed to merge. If you choose option #3 above then conflicts might arise; there will be no conflicts for options #1 or #2. [Note: for option #2, you will do git stash pop later and conflicts might arise then.]

    For your Q1: do git config merge.tool diff3 (or vimdiff, or whatever). Merging can be confusing (it is typically a 3-way merge so the tool shows three versions and a combined version).

    For your Q2: SourceTree does not include it's own merge tool; you'll need to rely on another (I use p4merge and SourceTree). With these config options:

    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