12

I want to create a batch file that initializes all the key/values in my .gitconfig file.

I have troubles trying to set the following section from cmd:

[mergetool "p4merge"]
    cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"

I tried:

git config --global mergetool.p4merge.cmd "p4merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""

But the result is:

[mergetool "p4merge"]
    cmd = p4merge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"

How should I escape that double quotes from cmd?

lante
  • 7,192
  • 4
  • 37
  • 57

1 Answers1

6

The answer to "Git on Windows: How do you set up a mergetool?" proposes:

  • From a git bash session:
git config --global mergetool.p4merge.cmd 'p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"'
  • or, from a windows cmd.exe shell, using 3 double-quotes """ to escape one ":
git config --global mergetool.p4merge.cmd "p4merge.exe """$BASE""" """$LOCAL""" """$REMOTE""" """$MERGED""""

So it depends if you launched git-bash.bat or git-cmd.bat

Other examples in CMD:

git config --global core.editor """"C:\Users\vonc\AppData\Local\Programs\Microsoft VS Code\Code.exe""" --wait"
# or
git config --global core.editor """"C:/Program Files/Notepad++/notepad++.exe""""
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for your reply. Thats what i tried from `cmd`, but im getting `cmd = p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"` into `.gitconfig` file, literally (I mean, with the "\"). Is that ok? – lante Aug 27 '14 at 20:20
  • 1
    @lante yes, the outer quotes are supposed to disappear once in the `.gitconfig`. The `\"` should work, but I would still test it first. – VonC Aug 27 '14 at 20:22
  • `$LOCAL` and `$REMOTE` should be in the reverse order: `\"$BASE\" \"$REMOTE\" \"$LOCAL\" \"$MERGED\"` – ivan_pozdeev Dec 17 '17 at 10:17
  • 1
    @ivan_pozdeev Interesting: do you have any link for that? I have always seens the BASE LOCAL REMOTE order myself when it comes to p4merge (https://danlimerick.wordpress.com/2011/06/19/git-for-window-tip-use-p4merge-as-mergetool/, https://gist.github.com/daredude/1350855, https://gist.github.com/robcthegeek/306395/cb22940c0651b1499a24281680d6de7f823b7422, https://gist.github.com/tony4d/3454372#gistcomment-1191983, etc...) – VonC Dec 17 '17 at 13:45
  • I simply tried that with `p4merge` 2012.1. Normally, REMOTE is blue and on the left, and LOCAL is green and on the right. Your way, they were swapped. – ivan_pozdeev Dec 17 '17 at 14:09
  • 2
    @ivan_pozdeev Was it during a rebase (where remote and local are reversed: https://stackoverflow.com/a/2960751/6309)? – VonC Dec 17 '17 at 14:10
  • @VonC, exactly. – ivan_pozdeev Dec 17 '17 at 14:13
  • @ivan_pozdeev Oh, that would makes sense, then. – VonC Dec 17 '17 at 14:14
  • The backslash does not work in Windows cmd console. The command `git config --global core.editor "\"C:/Program Files/Notepad++/notepad++.exe\""` produces `editor = \"C:/Program Files STANDALONE/Notepad++/notepad++.exe\"` I ended up editing .gitconfig directly. – Alex Fainshtein Jun 15 '21 at 08:24
  • @AlexFainshtein 7 years later, I agree: I have edited the answer to fix the double-quotes escape in a CMD on Windows. – VonC Jun 15 '21 at 08:33
  • Good Lord - cmd.exe/Windows command line quoting and escaping is a mess. – Michael Burr Jun 24 '22 at 23:52