Search code examples
linuxgitcommand-line-interfacemergetool

How do I get BASE / REMOTE / LOCAL files without launching a mergetool GUI?


I'm not quite sure even how to ask this question... but perhaps:

I am in a conflict state. I want to use null-op or /bin/bash as my merge tool.

That is, I want git to create the (REMOTE, LOCAL, BASE, etc) files, and then drop me back to a prompt for manual diff / merging. And then I want to tell git when I'm done.

What is the mergetool I want, and how do I specify it?

Note: This is a related question, but 1) it conflates rebasing (I'm not rebasing), and 2) there is no satisfying answer, e.g. "choose any mergetool and force-quit it." :P


Solution

  • git mergetool is a shell script (/bin/sh; /bin/bash can run it, if you have only bash and not plain old sh). It:

    1. Extracts the three files from the index (.BASE = stage 1 entry, .LOCAL = stage 2 entry, .REMOTE = stage 3 entry). The fourth file is used as a check on the merge tool—there's no need for this if you are going to do your merge manually.
    2. Runs your selected merge tool, using a second shell script.
    3. Depending on the tool and its exit status, asks you if the merge succeeded, or uses the exit status to decide if the merge succeeded. If you say it did, it runs git add on the merged file.
    4. Removes the files it created in step 1.

    It does this in a loop, for all unmerged files.

    Your job is to modify that shell script so that it extracts the three files and leaves them, i.e., to do (most of) step 1 and then not do the remaining steps at all. Simply find the script—it's in $(git --exec-path)/git-mergetool—and copy it somewhere else under another name, e.g., git-extract-unmerged, modify it to behave the way you want, and then run git extract-unmerged and you will have those .BASE, .LOCAL, and .REMOTE files that you want.

    (Or you could set merge.conflictStyle to diff3 and not bother with all of the above, now that the work-tree file has all the information you need. That's what I do instead.)