有没有办法使用 VisualStudio 代码查看 git 与 source/master 的区别?

使用 Visual Studio Code (版本1.11.2) ,通过单击左侧面板中的 源头控制按钮,我可以非常容易地看到当前更改的并排图形差异。但是,一旦我将这些更改提交到本地存储库,我就无法找到一种方法来查看与 source/master 相同的并行差异。

换句话说,有没有一种方法可以使用 Visual Studio Code (版本1.11.2)的产生比较工具来向我展示我在执行 git diff origin/master时看到了什么,但是在并排的图形差异中也可以看到?

86201 次浏览

You can use an extension for this.

Two good options:

Gitlens: https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens

With this one, you can use the >GitLens: Open Changes with... action to compare with any branch (local or remote).

You also can use Git History: https://marketplace.visualstudio.com/items?itemName=donjayamanne.githistory

You can see the entire file history and compare with the current version with the >Git: View File History action.

It doesn't use Visual Studio Code, but if you just want to see a quick summary of changes... just start a PR from the branch on GitHub.

From Using Version Control in Visual Studio Code:

Add this to the Git configuration file, like ~/.gitconfig:

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

When using git difftool HEAD HEAD^, Git will ask if to use Visual Studio Code.

From MSDN blog

Viewing Diffs

Our Git tooling supports viewing of Diffs within VS Code. Click the file in the Git view to display a side-by-side view. This allows you to compare your current file with a previous version of it:

I use GitLens extension as well. Go to Source Control tab, right click on file you want to compare with origin/master (or other) branch. From the menu choose Open Changes with... and pick a branch.

Compare local file changes to master

The accepted answer is good if you want to compare a single file from HEAD to some commit. On the other hand, if you need to diff all your files with another branch, Git Lens also provide solution for that: Go to source control tab on the side(1) > click on BRANCHES(2) > right click on the desired branch (like dev - 3)

enter image description here

Now, a menu will open, choose compare with HEAD

enter image description here

  • You could also do the same with commits, if in phase (2) you'll choose COMMITS instead.
  • You could also use cmnd+shift_p or ctrl+shift+p and type GitLens: Compare HEAD with, and then choose the specific wanted commit/branch.

I am using vscode with GitLens
The most convenient way to see diff from origin/$branch is to use git merge tool.
For example if I want to compare local and remote develop branch I will use the following command

git merge --no-commit --no-ff origin/develop

And the most exiting thing is that I can see all the changes in all the files from the gitlents extension tab and side by side and in case I don't want to merge there is a command

git merge --abort

Hope it helps someone!