ChatGPT解决这个技术问题 Extra ChatGPT

How to use Visual Studio Code as the default editor for Git MergeTool

Today I was trying to use the git mergetool on the Windows command prompt and realized that it was defaulting to use Vim, which is cool, but I'd prefer VS Code.

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


J
Jeremy Moritz

As of Visual Studio Code 1.13 Better Merge was integrated into the core of Visual Studio Code.

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

To do this with command line entries, enter each of these: (Note: you may need to replace ' with " if you are not using Windows Git Bash, macOS or Linux as clarified by Iztok Delfin and e4rache) git config --global merge.tool vscode git config --global mergetool.vscode.cmd 'code --wait $MERGED' git config --global diff.tool vscode git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE' To do this by pasting some line in the .gitconfig with Visual Studio 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 Visual Studio Code 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 within your Git directory with a conflict run git mergetool and, tada, you have Visual Studio Code helping you handle the merge conflict! (Just make sure to save your file before closing Visual Studio Code.)

https://i.stack.imgur.com/SjhvD.png

For further reading on launching code from the command line, look in this documentation.

For more information in git mergetool check out this documentation.


Does this integration of Better Merge allow you to accept and choose which portion to merge? Or do you still have to remove by hand the extra comments of the conflict like <<<< HEAD and >>>> develop?
There is a line above <<<< Head, that gets inserted that lists the options: "Accept Current Change | Accept Incoming Change | Accept Both Changes | Compare Changes" and I believe it inserts this on each section of detected changes in the file. But within a section if you want to custom merge a bit of this and a bit of that by hand, I believe you would make that change in your local and then go with the "Accept Current Change" option. So the work flow does kind of have you take a step back in order to take a step forward. If others resolve this in a different way please post here to educate us
Oh I see it in your print screen now, very neat. Thanks a lot for the explanation and the how to do it. Will happily use it on the next time conflicts gets in the way :)
I get the error unknown tool: vscode... I'm pretty sure to call VsCode from command line you have to use code instead of vscode
Also, this is not working for me. It just pops open VsCode and no files are ever brought up
P
Peter Mortensen

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.


Not for me. I just did this on Windows using the Command Prompt. Perhaps you're using something different? If so, I suggest adding which environment you're using so others with the same environment will know that they'll need to make this change.
@e4rache and @Iztok-Delfin what you have listed here is helpful content but you've accidentally made it an answer, when it's really a comment. I'm sure you ran into trouble because you didn't have the 50 point on SO to allow you to comment, which is kind of a site work flow problem that should maybe be looked into. Anyway, thanks for contributing, and I added your tip in my answer above. Thanks for helping those who come later and are using Git Bash :^)
@eric-d-johnson I totally meant to make a comment instead of a reply. ( sorry, I'm new to this site ) and btw I was using bash on linux. Is there a way to transorm this reply into a comment ?
@e4rache I don't know a way to make it a comment, but perhaps a moderator will see this and give us some tips (hint, hint). If you had the 50 points, you could make a comment on the accepted answer, and delete this one, so once you become an SO rock-star with tons of points you can come back here and do whatever housekeeping makes you happy and brings back good memories of how it all started.
@mohamedghonemi Good feedback. I updated the explanation at the top for macOS and Linux now to remember to use single quotes ('s).
C
Captain Man

On top of the excellent existing answer, you should open VS Code in a new window by adding -n to the command line.

So your git config --global --edit looks something like this.

[merge]
        tool = vscode
[mergetool "vscode"]
        cmd = code --new-window --wait $MERGED
[diff]
        tool = vscode
[difftool "vscode"]
        cmd = code --new-window --wait --diff $LOCAL $REMOTE                                                    

I used these settings, but when I do git diff commit_id1 commit_id2, it doesn't bring up a VS code editor window. It still defaults to the terminal. Am I using the wrong command?
@user5965026 have you tried git diftool instead of git diff?
G
Gomino

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


D
Daniel B

In case if someone want to resolve it in Visual Studio, another option would be doing it through Visual Studio: Team Explorer -> click on Home icon => Settings button => expand Git section => click on Global Settings

https://i.stack.imgur.com/8ZdKI.png


Visual Studio is not equivalent to Visual Studio Code. The way settings like this are done are quite different. See stackoverflow.com/a/33798601 for the differences
@jwd630 I know VS code is different than VS. I had this issue in VS but couldn't find a solution for that so I though it would be better to post it in case someone is experincing this problem. No need to down-vote.
This is an answer to a completely different question for a different product.
Proper usage of completely: windows medial player is completely different than vs code.