How to use Visual Studio Code as Default Editor for Git MergeTool

时光总嘲笑我的痴心妄想 提交于 2019-11-28 13:12:03

问题


Today I was trying to use the git mergetool on Windows Command Prompt and realized that it was defaulting to use VIM, which is cool, but I'd prefer VSCode.

How can I have Visual Studio Code function as my GUI for handling merge conflicts (or even as a diffing tool) for Git?


回答1:


As of VSCode 1.13 Better Merge was integrated into the core of VSCode.

The way to wire them together is to modify your .gitconfig and you have two options.

  1. To do this with command line entries, enter each of these: (Note: replace " with ' on Windows Git Bash as clarified by Iztok Delfin and e4rache)

    1. git config --global merge.tool vscode
    2. git config --global mergetool.vscode.cmd "code --wait $MERGED"
    3. git config --global diff.tool vscode
    4. git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
  2. To do this by pasting some line in the .gitconfig with VS Code.

    • Run git config --global core.editor "code --wait" from the command line.
    • From here you can enter the command git config --global -e. You will want to paste in the code in the "Extra Block" below.

      [user]
          name = EricDJohnson
          email = cool-email@neat.org
      [gui]
          recentrepo = E:/src/gitlab/App-Custom/Some-App
      # Comment: You just added this via 'git config --global core.editor "code --wait"'
      [core]
          editor = code --wait
      # Comment: Start of "Extra Block"
      # Comment: This is to unlock VSCode as your git diff and git merge tool    
      [merge]
          tool = vscode
      [mergetool "vscode"]
          cmd = code --wait $MERGED
      [diff]
          tool = vscode
      [difftool "vscode"]
          cmd = code --wait --diff $LOCAL $REMOTE
      # Comment: End of "Extra Block"
      

Now from with in your git directory with a conflict run git mergetool and, tada, you have VSCode helping you handle the merge conflict! (Just make sure to save your file before closing VSCode).

For further reading on launching code from the command line look in these docs.

For more info in git mergetool check out these docs.




回答2:


I had to replace the double quotes with simple quotes:

  git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

for it to work properly.
(with double quotes, $LOCAL and $REMOTE are replaced by their values)

This is needed if you are using Git Bash for Windows instead of Windows Command Prompt.




回答3:


Using the manual you can find an interesting argument:

git difftool --help 
-x <command>, --extcmd=<command>
       Specify a custom command for viewing diffs.  git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.
       Additionally, $BASE is set in the environment.

With this information you can easily use the following command without touching the git configuration:

git difftool -x "code --wait --diff" 

Similar question here



来源:https://stackoverflow.com/questions/44549733/how-to-use-visual-studio-code-as-default-editor-for-git-mergetool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!