使用 Visual Studio Code 作为 Git diff 工具

问题描述 投票:0回答:3

Visual Studio Code 有一个很好的内置功能来比较两个文件。

是否可以使用 Visual Studio Code diff 作为 Git 的 diff 工具?

git diff visual-studio-code
3个回答
15
投票

就像 Maciej 所说,

gitconfig
就是要走的路。有了这个,我可以将它设置为不仅仅是一个 difftool,而且还是 git 的合并工具。

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

我使用 VSCode Insiders,提前获取最新(但仍然稳定)的功能

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

编辑:现在有 官方 VSCode 文档,可将 VSCode 用作 difftoolmergetool

在您的

~/.gitconfig
文件中:

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = code
[mergetool "code"]
  cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED

4
投票

是的,有可能 您只需将 Visual Studio Code 设置为默认 difftool,方法是将其添加到 ~/.gitconfig 文件中。

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

完成此操作后,只需运行命令例如:git difftool master,几秒钟后 VS Code 运行 difftool


1
投票

以下是官方文档:

https://code.visualstudio.com/docs/sourcecontrol/overview#_vs-code-as-git-difftool-and-mergetool

截至 22 年 11 月 16 日的内容复制如下,以防移动:

即使从命令行使用 Git,您也可以使用 VS Code 的差异和合并功能。将以下内容添加到您的 Git 配置中,以使用 VS Code 作为差异和合并工具:

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = code
[mergetool "code"]
  cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED

这使用了 --diff 选项,可以传递给 VS Code 进行比较 两个文件并排。下次Git时会用到合并工具 发现合并冲突。

总而言之,以下是一些可以使用 VS Code 的示例 编辑:

  • git rebase HEAD~3 -i
    使用 VS Code 进行交互式变基
  • git commit
    使用 VS Code 作为提交消息
  • git add -p
    后跟 e 用于交互式添加
  • git difftool <commit>^ <commit>
    使用 VS Code 作为更改的差异编辑器
© www.soinside.com 2019 - 2024. All rights reserved.