如何在 Visual Studio Code 并排文件上看到“git diff”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51316233/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:19:09  来源:igfitidea点击:

How can I see 'git diff' on the Visual Studio Code side-by-side file?

gitvisual-studio-codegit-diffgit-difftool

提问by Ricardo Jesus Jarquin Perez

I would like to know how to see as a file with the command git diff master origin/masterin the terminal on Visual Studio Code.

我想知道如何git diff master origin/master在 Visual Studio Code 的终端中使用命令查看文件。

I did a git fetchfrom my remote repository, and now I want to see the diff, but with the command just show me in the terminal.

git fetch从我的远程存储库做了一个,现在我想查看差异,但是使用命令只在终端中显示我。

Example of what I want:

我想要的例子:

Screenshot of diff viewer in Visual Studio Code

Visual Studio Code 中差异查看器的屏幕截图

回答by Ratan Uday Kumar

In Visual Studio Code, on the left side, there is a Git icon that looks like this:

在 Visual Studio Code 中,左侧有一个 Git 图标,如下所示:

By clicking on this icon, you can see the Git difference in two sides.

通过点击这个图标,你可以看到两个方面的 Git 差异。

回答by Boncho Valkov

If you want to see the diff changesfrom different branches, there is some extra work. For example you want to see all the changes from last Ncommits in your Feature branch.

如果你想看到的差异变化,从不同的分支,有一些额外的工作。例如,您想查看Feature 分支中最近N 次提交的所有更改。

  1. Set up Visual Studio Code to be your default difftool by adding this in your ~/.gitconfigfile.

    [diff]
        tool = vscode
    [difftool "vscode"]
        cmd = code --wait --diff $LOCAL $REMOTE
    
  2. Go to your Git project. Type in:

    git difftool {{branch you want to check with}}, for example git difftool master

  3. You will be prompted for each file, if you want to open it in Visual Studio Code or not.

  1. 通过在~/.gitconfig文件中添加它,将 Visual Studio Code 设置为默认的 difftool 。

    [diff]
        tool = vscode
    [difftool "vscode"]
        cmd = code --wait --diff $LOCAL $REMOTE
    
  2. 转到您的 Git 项目。输入:

    git difftool {{要检查的分支}},例如git difftool master

  3. 如果您想在 Visual Studio Code 中打开它,系统将提示您输入每个文件。

回答by lboyel

You can achieve this in Visual Studio Code by

您可以通过以下方式在 Visual Studio Code 中实现此目的

  1. Opening up settings (On window/linux File > Preferences > Setting. On macOS Code > Preferences > Settings)
  2. Search for diff
  3. The specific setting is Diff Editor:RenderSide by Side. Mark the checkbox.
  1. 打开设置(在window/linux File > Preferences > Setting。在macOS Code > Preferences > Settings)
  2. 搜索差异
  3. 具体设置为Diff Editor:RenderSide by Side。标记复选框。

回答by billygarrison

Here's a simple way to view your changes since last commit (on current branch):

这是查看自上次提交(在当前分支上)以来所做更改的简单方法:

  1. Click Git icon on left side of VS Code
  2. If you've made changes to the file(s) since last commit, you'll see the file(s) listed under "CHANGES"
  3. Right click the file name (under "CHANGES") and click "Open Changes"
  4. This will open the two versions of the file side by side with the changes highlighted
  1. 单击 VS Code 左侧的 Git 图标
  2. 如果您自上次提交以来对文件进行了更改,您将看到“更改”下列出的文件
  3. 右键单击文件名(在“更改”下),然后单击“打开更改”
  4. 这将并排打开文件的两个版本,并突出显示更改

Image showing example of VS Code displaying changes

显示 VS Code 显示更改示例的图像

回答by Nitin Bisht

You can diffany two files by first right clicking on a file in the EXPLORERor OPEN EDITORSlist and selecting Select for Compareand then right-click on the second file to compare with and select Compare with <file_name_you_chose>.

您可以diff通过首先右键单击EXPLOREROPEN EDITORS列表中的文件并选择Select for Compare,然后右键单击要与之比较的第二个文件并选择Compare with <file_name_you_chose>选择任意两个文件。

Alternatively from the keyboard hit Ctrl+ Shift+ Pand select menu FileCompare Active File With...and you will be presented with a list of recent files. Example:

或者从键盘点击Ctrl+ Shift+P并选择菜单文件将活动文件与...比较,您将看到最近文件的列表。例子:

Enter image description here

在此处输入图片说明

回答by Abdollah

Open file ~/.gitconfigin Visual Studio Code:

~/.gitconfig在 Visual Studio Code 中打开文件:

code  ~/.gitconfig

Copy the following lines in ~/.gitconfig:

复制以下几行~/.gitconfig

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

Save the changes. Open a terminal in Visual Studio Code by running Ctrl+ Shift+ `. Run the following command in the terminal:

保存更改。通过运行Ctrl+ Shift+ `在 Visual Studio Code 中打开一个终端。在终端中运行以下命令:

git difftool master origin/master

回答by Gomino

I have answered a similar question here.

在这里回答了一个类似的问题。

But basically you can use the following command:

但基本上你可以使用以下命令:

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

回答by adrianvintu

After hours of searching, installing and uninstalling extensions, it seems this is already implemented in VSC.

经过数小时的搜索、安装和卸载扩展,这似乎已经在 VSC 中实现了。

Just click on the top right icon - "Open changes" enter image description here

只需单击右上角的图标 - “打开更改” 在此处输入图片说明

And go back to seeing only the file, not the changes, by clicking on the... top right icon - "Open file"

然后通过单击...右上角的图标 - “打开文件”返回仅查看文件,而不查看更改

enter image description here

在此处输入图片说明