233

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

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

Example of what I want:

Screenshot of diff viewer in Visual Studio Code

16 Answers 16

304

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

By clicking on this icon, then double-clicking one of the files listed under Changes you can see the Git difference in two sides.

11
  • 42
    For completeness, since not everyone has the icons showing, look under menu View ->SCM (Show source control) ctrl-shift-G Commented Mar 12, 2019 at 15:27
  • 1
    You can also open the preview using the keyboard shortcut ⌘K V as documented in VSCode keybindings. Commented Mar 27, 2020 at 20:43
  • 3
    icon was updated
    – llobet
    Commented Aug 26, 2020 at 9:54
  • 49
    Your answer does not work for any diff. This only works to show diffs for current changes against the latest commit.
    – blindguy
    Commented Oct 8, 2020 at 19:16
  • 22
    This should not be the accepted answer since it is only showing the changes not committed yet
    – O.k
    Commented Feb 22, 2022 at 10:00
105

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

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

    [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.

6
  • 16
    Is there a way we can see the list of all files to diff in visual studio code instead of opening one by one (like how we see by clicking the git icon on the left side panel of visual studio)?
    – Sathya
    Commented Mar 17, 2020 at 16:18
  • 2
    Related VS Code docs: code.visualstudio.com/docs/editor/…
    – ford04
    Commented Aug 11, 2020 at 7:39
  • 3
    @Sathya once you are done with settings shared by Boncho git difftool --dir-diff should do the work or you can use alias [alias] df = difftool --dir-diff in your config file
    – Mr.Ali
    Commented Nov 17, 2020 at 6:43
  • 2
    There is also existing a nice visual code plugin 'git-tree-compare' that shows your diff changes to a specific reference branch. See: marketplace.visualstudio.com/…
    – ruuns
    Commented Jan 2, 2021 at 12:22
  • 1
    as a one-off command, vscode itself can be used to compare two files: code --diff file1.txt file2.txt Commented Nov 2, 2021 at 5:47
51

You can achieve this in Visual Studio Code by

  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:Render Side by Side. Mark the checkbox.
6
  • Can you be more specific? How do you "open up settings"? The "gear" icon in the lower left? Something else? Respond by editing your answer, not here in comments. Commented Jan 21, 2020 at 15:06
  • If you have a pending update - try restarting visual code after changing this setting. I had one and changing only worked after the update
    – StackUnder
    Commented Sep 1, 2020 at 18:44
  • @PeterMortensen, almost a year passed, but maybe someone will find it helpful. Anyways, on Windows you press CTRL+P, then > and then type Settings, menu rolls down, and I would look for UI menu option.
    – Gabrielius
    Commented Oct 13, 2020 at 13:11
  • @PeterMortensen CTRL+,
    – Giulio
    Commented Aug 15, 2021 at 8:07
  • 1
    This should be the top answer IMO. Its exactly what I was looking for anyways.
    – SHINIGAMI
    Commented May 22, 2022 at 23:42
41

After hours of searching, installing and uninstalling extensions, it seems this is already implemented in 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

4
  • 4
    How to open all changed files in one tab 😭😭😭
    – VityaSchel
    Commented Feb 5, 2022 at 20:22
  • 2
    You can also select "Git: Open Changes" from the command palette
    – Ryan
    Commented Jul 22, 2022 at 22:50
  • 2
    Yes, thank you!! I knew there had to be a way.
    – Brain2000
    Commented Jun 4 at 18:39
  • 2
    It's remarkably difficult to figure out how to do this. Your answer is the only one I've found which shows the right way. Commented Jun 12 at 19:25
29

If you want to compare between two arbitrary references - for example comparing between branch and branch, or a commit and another commit - and still view all files in one shot easily just like we see the index changes.

  • Install the GitLens extension
  • Go to the Source control in the left pane. If you don't have the icon then you can look under menu View -> SCM (Show source control) or use the defined shortcut.
  • Expand the last section Search & Compare
  • Click on button Compare References...
  • Pick the references, and then you will see the list of changed files and clicking one file will show its changes side to side.

Image showing the button

2
  • 2
    GitLens works great!
    – Shaohua Li
    Commented Jun 22, 2023 at 6:34
  • 1
    Going back to multiple previous commits is what I wanted. Thanks.
    – drchicken
    Commented May 23 at 22:23
14

I have answered a similar question here.

But basically you can use the following command:

git difftool -x "code --wait --diff"
1
  • 1
    It is referring to a (deleted) answer to this question. Commented Jan 21, 2020 at 15:02
12

Open file ~/.gitconfig in Visual Studio Code:

code  ~/.gitconfig

Copy the following lines in ~/.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:

git difftool master origin/master
0
10

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

Image showing example of VS Code displaying changes

10

toggle inline view now is available (on the 3 dots)

6

Here another way to compare against the previous versions.

  1. On the Explorer panel.
  2. choose a file to compare, then open context menu (right click), then choose Select for Compare.
  3. again, open context menu, then select Open Timeline. Wait for loading previous changes.
  4. On the Timeline tab, you can select a previous version and open the context menu and click Compare with Selected.

Then you will see diff files side by side.

compare with previous

1
  • For me the diff is always the same (most recent) file. But this would be a really nice workflow.
    – ahfx
    Commented Jul 4, 2023 at 22:16
4

You can diff any two files by first right clicking on a file in the EXPLORER or OPEN EDITORS list and selecting Select for Compare and then right-click on the second file to compare with and select Compare with <file_name_you_chose>.

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

Enter image description here

3
  • 6
    First, the question clearly asks about git diff in which case there will not be two files, rather only one. Second, you need to install another extension in VS Code, like "compareit" for enabling the option "Select for Compare" and "Compare with Selected".
    – KatariaA
    Commented Jul 30, 2019 at 13:19
  • @KatariaA: For the second point: It seems to be installed by default (Visual Studio Code 1.41.1 (on Ubuntu 19.10)). Can you confirm? Commented Jan 21, 2020 at 14:48
  • 1
    @PeterMortensen Yes it's there, no need for an extension: code.visualstudio.com/docs/editor/versioncontrol#_viewing-diffs
    – rmcsharry
    Commented Feb 7, 2021 at 20:10
3

Vscode itself is able to show differences between any two files:

code --diff file1.txt file2.txt

i believe this is independent from git diff feature.

3

To make this answer work, we must follow a few steps, which, despite having been already repeated in previous answers, I'll rewrite them for sake of completeness.

Open the file ~/.gitconfig, and add the following lines:

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

In some of the answers, the next suggested step is doing git difftool <local_branch> origin/<remote_branch>.

However, there's also another possibility:

git fetch origin <remote_branch>
git difftool FETCH_HEAD

Also, to prevent those annoying prompts from showing up, we can always do:

git config --global difftool.prompt false
2

For a quick single file diff view in VSCode without further integrated navigation and edit experience you can configure and use the git difftool as explained by other answers - or more safe (and global) like this:

git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
git config --global diff.tool vscode   # optionally as default

For a fully integrated experience for such kind of custom diff in VSCode do like this:

# possibly commit or stash a dirty work tree before switching
git switch origin/master --detach  # new master in worktree
git reset master                   # old master as detached HEAD (diff base)

Now you can see and use this "custom diff" as usual in VSCode - as a diff of worktree vs. HEAD : Use the git SCM icon, double/right-click on file changes, toggle inline diff view, etc. .

Now you can even work directly on that worktree right in the diff view. To make a commit of such changes do like:

git reset origin/master    # base for added changes only
# review the bare added delta again (in VSCode)
git add/commit ...
git branch/tag my_master_fixup   # name it

Then merge the new master as usual, switch back to feature branch, possibly cherry-pick the my_master_fixup, rebase or whatever ..

2

For more completeness, unenter image description herecheck the box

1
  • You are the savior.
    – LulzAsh
    Commented Aug 28 at 9:32
0

From v1.48 release notes:

As you navigate the Source Control view, pressing Space on a change will now open it as a preview editor and keep the focus in the Source Control view, for easier keyboard navigation.

So you could downarrow through your scm file changes and hit Space to open a diff view.. Focus remains in the SCM view so you could keep doing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.