gitvisual-studio-codecommand-linegit-difftool

How can I open VS Code’s side-by-side diff view (staged vs. working copy) from a single terminal command?


Repository State

I have a git repository located in ~/Desktop/ML/Books. In this repo, I've made a series of edits to a file (main.rs) as follows:

  1. Initially, I modified main.rs, staged it, and committed those changes.
  2. Later, I made further modifications to main.rs and staged those changes.
  3. Then, I modified main.rs again (leaving the file unstaged).

Walkthrough of non-automated viewing of diff

When I open VS Code (using, for example, the command code ProgrammingRust/projs/actix-gcd/src/main.rs) and click the "Open Changes" button, I see a visual side-by-side comparison of the file’s staged content versus the latest edits in the working copy.

enter image description here

What I have tried

I've configured my global git settings (in ~/.gitconfig) to use VS Code as my difftool. enter image description here

However, when I run the command:

git difftool ProgrammingRust/projs/actix-gcd/src/main.rs

instead of showing a comparison between the staged and unstaged versions, it compares the current file with an unexpected "unknown" file (as seen in the attached screenshot of paths). enter image description here

Request

What I need is to automate this diff view – that is, have a single terminal command (or a small set of commands) that directly opens the VS Code side-by-side diff view comparing the staged version of main.rs against its working copy without any extra clicks in the GUI.

How can I configure or invoke git difftool (or another command) to achieve this behavior?


Solution

  • Fixed it by adding --wait argument to the command in the .gitconfig file (or ./.git/config file for local change). Like:

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

    Then running the following command:

    git difftool --no-prompt --tool=vscode ProgrammingRust/projs/actix-gcd/src/main.rs