258

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

19 Answers 19

329

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.

Sign up to request clarification or add additional context in comments.

11 Comments

For completeness, since not everyone has the icons showing, look under menu View ->SCM (Show source control) ctrl-shift-G
Your answer does not work for any diff. This only works to show diffs for current changes against the latest commit.
Important Tip: If you do this and the diff is shown to you as in-line instead of side-by-side, then click on the button w/ 3 dots in the top right and click 'Inline-View'. You should now see the diff shown as side-by-side. (You can also instead toggle the diff view by following these instructions).
This should not be the accepted answer since it is only showing the changes not committed yet
|
115

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 Comments

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 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
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/…
Piping to code - works for one time use. git diff some_tag_or_branch | code -
|
67

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.

7 Comments

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.
If you have a pending update - try restarting visual code after changing this setting. I had one and changing only worked after the update
@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.
@PeterMortensen CTRL+,
This should be the top answer IMO. Its exactly what I was looking for anyways.
|
48

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 Comments

How to open all changed files in one tab 😭😭😭
You can also select "Git: Open Changes" from the command palette
Yes, thank you!! I knew there had to be a way.
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.
31

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 Comments

GitLens works great!
Going back to multiple previous commits is what I wanted. Thanks.
14

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 

Comments

14

I have answered a similar question here.

But basically you can use the following command:

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

1 Comment

It is referring to a (deleted) answer to this question.
13

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

Comments

11

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

Comments

11

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 Comment

For me the diff is always the same (most recent) file. But this would be a really nice workflow.
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 Comments

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: For the second point: It seems to be installed by default (Visual Studio Code 1.41.1 (on Ubuntu 19.10)). Can you confirm?
@PeterMortensen Yes it's there, no need for an extension: code.visualstudio.com/docs/editor/versioncontrol#_viewing-diffs
4

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 

1 Comment

git 2.49.0 supports vscode as dif.guitool (see git-scm.com/docs/git-difftool/2.49.0). Set it with git config --global diff.guitool vscode and run git difftool with the -g flag
4

For more completeness, unenter image description herecheck the box

1 Comment

You are the savior.
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.

Comments

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

Comments

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.

Comments

0

I think OP question is about opening diff in side-by-side vs inline view mode. Not how to open the diff. Someone mentioned in comment that if you are getting inline view and want to change it to side-by-side you need to:

  1. Open the diff(inline view)
  2. click on three dots in top right corner
  3. Disable "inline view" and Also disable "Use inline view when space is limited" if you want to

Comments

0

You can compare two different branches by choosing "GitLensInspect" > "Search & Compare"

If you can't see it on sidebar do the below:

  1. Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac).

  2. Type Git: Compare Branches.

  3. Select the two branches you want to compare.

enter image description here

Comments

0

Once viewing a (any) diff, to view files side-by-side or inline, there is the Compare: Toggle Inline View command that does just that, as @rio was on to above. I wanted to clarify what the command is called (and a comment does not allow images, nor snippets, hence this answer).

So, if you get a diff in inline view and want to view it side-by-side, just hit that command, here in the vs code command palette (ctrl-shift-p):

vs code command palette opened showing Compare: Toggle Inline Viewenter image description here

Command name for keybindings.json is toggle.diff.renderSideBySide:

{ "key": "ctrl+alt+f12", "command": "toggle.diff.renderSideBySide", "when": "textCompareEditorVisible" } 

Hope this helps someone that came looking for this!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.