如何使用 Visual Studio Code 作为 Git MergeTool 的默认编辑器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44549733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Visual Studio Code as the default editor for Git MergeTool
提问by Eric D. Johnson
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.
今天我试图git mergetool
在 Windows 命令提示符下使用 ,并意识到它默认使用Vim,这很酷,但我更喜欢VS Code。
How can I have Visual Studio Codefunction as my GUI for handling merge conflicts (or even as a diffing tool) for Git?
我如何将Visual Studio Code功能作为我的 GUI 来处理 Git 的合并冲突(甚至作为差异工具)?
回答by Eric D. Johnson
As of Visual Studio Code 1.13Better Mergewas integrated into the core of Visual Studio Code.
从Visual Studio Code 1.13 开始,Better Merge已集成到 Visual Studio Code 的核心中。
The way to wire them together is to modify your .gitconfig
and you have two options.
将它们连接在一起的方法是修改您的.gitconfig
和您有两个选项。
To do this with command line entries, enter each of these: (Note: replace
"
with'
on Windows Git Bash, macOS and 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 = [email protected] [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"
- Run
要使用命令行输入为此,输入每个这些:(注:替换
"
与'
Windows上的Git猛砸,MacOS和Linux的通过Iztok海豚和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"
为此,请在
.gitconfig
with Visual Studio Code 中粘贴一些行。- 运行
git config --global core.editor "code --wait"
命令行。 从这里您可以输入命令
git config --global -e
。您需要将代码粘贴到下面的“额外块”中。[user] name = EricDJohnson email = [email protected] [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 filebefore closing Visual Studio Code.)
现在从您的 Git 目录中运行一个冲突,git mergetool
并且,您有 Visual Studio Code 帮助您处理合并冲突!(请确保在关闭 Visual Studio Code 之前保存您的文件。)
For further reading on launching code
from the command line, look in this documentation.
有关code
从命令行启动的进一步阅读,请查看此文档。
For more information in git mergetool
check out this documentation.
有关更多信息,git mergetool
请查看此文档。
回答by e4rache
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).
使其正常工作(使用双引号,$LOCAL 和 $REMOTE 被它们的值替换)。
This is needed if you are using Git Bashfor Windows instead of Windows Command Prompt.
如果您使用的是适用于 Windows 的Git Bash而不是 Windows 命令提示符,则需要这样做。
回答by 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
如果有人想在 Visual Studio 中解决它,另一种选择是通过 Visual Studio:团队资源管理器 -> 单击主页图标 => 设置按钮 => 展开 Git 部分 => 单击全局设置
回答by mvd
On top of the excellent existing answer, you should open VS Code in a new window by adding -n
to the command line.
除了现有的优秀答案之外,您应该通过添加-n
到命令行在新窗口中打开 VS Code 。
So your git config --global --edit
looks something like this.
所以你git config --global --edit
看起来像这样。
[merge]
tool = vscode
[mergetool "vscode"]
cmd = code -n --wait $MERGED
[diff]
tool = vscode
[difftool "vscode"]
cmd = code -n --wait --diff $LOCAL $REMOTE
回答by 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 配置:
git difftool -x "code --wait --diff"
Similar question here
类似的问题在这里