The top answers here correctly show how to view the cached/staged changes in the Index
:
$ git diff --cached
or $ git diff --staged
which is an alias.
The default answer will spit out the diff changes at the git bash (i.e. on the command line or in the console). For those who prefer a visual representation of the staged file differences, there is a script available within git which launches a visual diff tool for each file viewed rather than showing them on the command line, called difftool
:
$ git difftool --staged
This will do the same this as git diff --staged
, except any time the diff tool is run (i.e. every time a file is processed by diff), it will launch the default visual diff tool (in my environment, this is kdiff3).
After the tool launches, the git diff script will pause until your visual diff tool is closed. Therefore, you will need to close each file in order to see the next one.
difftool
in place of diff
in git commandsFor all your visual diff needs, git difftool
will work in place of any git diff
command, including all options.
For example, to have the visual diff tool launch without asking whether to do it for each file, add the -y
option (I think usually you'll want this!!):
$ git difftool -y --staged
In this case it will pull up each file in the visual diff tool, one at a time, bringing up the next one after the tool is closed.
Or to look at the diff of a particular file that is staged in the Index
:
$ git difftool -y --staged <<relative path/filename>>
For all the options, see the man page:
$ git difftool --help
To use a visual git tool other than the default, use the -t <tool>
option:
$ git difftool -t <tool> <<other args>>
Or, see the difftool man page for how to configure git to use a different default visual diff tool.
.gitconfig
entries for vscode as diff/merge toolPart of setting up a difftool involves changing the .gitconfig
file, either through git commands that change it behind the scenes, or editing it directly.
You can find your .gitconfig
in your home directory,such as ~
in Unix or normally c:\users\<username>
on Windows).
Or, you can open the user .gitconfig
in your default Git editor with git config -e --global
.
Here are example entries in my global user .gitconfig
for VS Code as both diff tool and merge tool:
[diff]
tool = vscode
guitool = vscode
[merge]
tool = vscode
guitool = vscode
[mergetool]
prompt = true
[difftool "vscode"]
cmd = code --wait --diff \"$LOCAL\" \"$REMOTE\"
path = c:/apps/vscode/code.exe
[mergetool "vscode"]
cmd = code --wait \"$MERGED\"
path = c:/apps/vscode/code.exe