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

16

221

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

Piglet answered 13/7, 2018 at 0:39 Comment(0)
B
288

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.

Bleat answered 13/7, 2018 at 4:7 Comment(11)
For completeness, since not everyone has the icons showing, look under menu View ->SCM (Show source control) ctrl-shift-GMunsey
You can also open the preview using the keyboard shortcut ⌘K V as documented in VSCode keybindings.Uniocular
I don't see this icon on my vscode menu is this a plugin?Digestant
icon was updatedViipuri
How to know what are we seeing on left and right panels? (working tree, index, some specific branch, ...)?Burglarize
Your answer does not work for any diff. This only works to show diffs for current changes against the latest commit.Endodontics
@Endodontics yaa my answer only for showing the difference in Vscode and actually question was also for it.Bleat
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).Bibliopegy
This should not be the accepted answer since it is only showing the changes not committed yetEnduring
@MichaelBehrens what is the name of the command to bind to?Clementinaclementine
Important: Building on what @Bibliopegy said, if clicking on view-in-line doesn't work for you, make sure to uncheck: "Use inline View When Space is Limited"Coolant
D
98

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.

Dourine answered 21/5, 2019 at 11:38 Comment(6)
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)?Lentic
Related VS Code docs: code.visualstudio.com/docs/editor/…Popularize
@Lentic 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 fileMetagalaxy
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/…Rabi
as a one-off command, vscode itself can be used to compare two files: code --diff file1.txt file2.txtCha
Piping to code - works for one time use. git diff some_tag_or_branch | code -Ensor
H
49

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.
Hatteras answered 19/12, 2019 at 19:56 Comment(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.Hashish
If you have a pending update - try restarting visual code after changing this setting. I had one and changing only worked after the updateMethylene
@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.Gassman
@PeterMortensen CTRL+,Sikorski
This should be the top answer IMO. Its exactly what I was looking for anyways.Lineal
It seems that the accepted answer assumes that this parameter is on.Weightless
N
37

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

Nuptial answered 3/4, 2020 at 14:41 Comment(2)
How to open all changed files in one tab 😭😭😭Baxie
You can also select "Git: Open Changes" from the command paletteWiesbaden
U
27

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

Uppish answered 10/8, 2021 at 11:16 Comment(1)
GitLens works great!Lough
G
13

I have answered a similar question here.

But basically you can use the following command:

git difftool -x "code --wait --diff"
Guy answered 21/11, 2019 at 20:21 Comment(1)
It is referring to a (deleted) answer to this question.Hashish
C
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
Corsica answered 5/11, 2019 at 13:50 Comment(0)
H
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

Haggle answered 6/2, 2020 at 12:40 Comment(0)
S
8

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

Sagacious answered 9/8, 2020 at 15:22 Comment(0)
G
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

Glomeration answered 13/7, 2018 at 4:8 Comment(3)
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".Capote
@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?Hashish
@PeterMortensen Yes it's there, no need for an extension: code.visualstudio.com/docs/editor/versioncontrol#_viewing-diffsAudiometer
O
4

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

Osborn answered 4/11, 2022 at 20:56 Comment(1)
For me the diff is always the same (most recent) file. But this would be a really nice workflow.Graphy
C
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.

Cha answered 2/11, 2021 at 2:47 Comment(0)
S
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 ..

Steenbok answered 15/11, 2020 at 12:16 Comment(0)
V
1

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
Verified answered 4/6, 2022 at 17:15 Comment(0)
F
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.

Feigin answered 9/8, 2020 at 16:21 Comment(0)
L
0

For more completeness, unenter image description herecheck the box

Lazar answered 6/12, 2023 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.