[git] How do I show the changes which have been staged?

I staged a few changes to be committed; how can I see the diff of all files which are staged for the next commit? I'm aware of git status, but I'd like to see the actual diffs - not just the names of files which are staged.

I saw that the git-diff(1) man page says

git diff [--options] [--] […]

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell git to further add to the index but you still haven't. You can stage these changes by using git-add(1).

Unfortunately, I can't quite make sense of this. There must be some handy one-liner which I could create an alias for, right?

This question is related to git diff dvcs git-diff git-stage

The answer is


git gui and git-cola are graphical utilities that let you view and manipulate the index. Both include simple visual diffs for staged files, and git-cola can also launch a more sophisticated side-by-side visual diff tool.

See my closely related answer at How to remove a file from the index in git?, and also this official catalog of Git - GUI Clients.


If you have more than one file with staged changes, it may more practical to use git add -i, then select 6: diff, and finally pick the file(s) you are interested in.


From version 1.7 and later it should be:

git diff --staged

Think about the gitk tool also , provided with git and very useful to see the changes


If your intentions are to push-target a remote repo branch and your first pass at a commit change log were incomplete, you can correct the commit statement before pushing like this.

Locally

... make some changes ...

git diff # look at unstaged changes

git commit -am"partial description of changes"

... recall more changes unmentioned in commit ...

git diff origin/master # look at staged but not pushed changes

... amend staged commit statement ...

git commit --amend -m"i missed mentioning these changes ...."

git push

By default git diff is used to show the changes which is not added to the list of git updated files. But if you want to show the changes which is added or stagged then you need to provide extra options that will let git know that you are interested in stagged or added files diff .

$ git diff          # Default Use
$ git diff --cached # Can be used to show difference after adding the files 
$ git diff --staged # Same as 'git diff --cached' mostly used with latest version of git 

Example

$ git diff 
diff --git a/x/y/z.js  b/x/y/z.js index 98fc22b..0359d84 100644
--- a/x/y/z.js 
+++ b/x/y/z.js @@ -43,7 +43,7 @@ var a = function (tooltip) {

-        if (a)
+        if (typeof a !== 'undefined')
             res = 1;
         else
             res = 2;

$ git add x/y/z.js
$ git diff
$

Once you added the files , you can't use default of 'git diff' .You have to do like this:-

$ git diff --cached
diff --git a/x/y/z.js  b/x/y/z.js index 98fc22b..0359d84 100644
    --- a/x/y/z.js 
    +++ b/x/y/z.js @@ -43,7 +43,7 @@ var a = function (tooltip) {

    -        if (a)
    +        if (typeof a !== 'undefined')
                 res = 1;
             else
                 res = 2;

If you'd be interested in a visual side-by-side view, the diffuse visual diff tool can do that. It will even show three panes if some but not all changes are staged. In the case of conflicts, there will even be four panes.

Screenshot of diffuse with staged and unstaged edits

Invoke it with

diffuse -m

in your Git working copy.

If you ask me, the best visual differ I've seen for a decade. Also, it is not specific to Git: It interoperates with a plethora of other VCS, including SVN, Mercurial, Bazaar, ...

See also: Show both staged & working tree in git diff?


USING A VISUAL DIFF TOOL

The Default Answer (at the command line)

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.



Launching the Visual Diff Tool Instead

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.



You Can Always Use difftool in place of diff in git commands

For 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


Setting up Visual Git Tool

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.



Example .gitconfig entries for vscode as diff/merge tool

Part 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

Note that git status -v also shows the staged changes! (meaning you need to have staged -- git add -- some changes. No staged changes, no diff with git status -v.
It does that since Git 1.2.0, February 2006)

In its long form (default), git status has an undocumented "verbose" option which actually display the diff between HEAD and index.

And it is about to become even more complete: see "Show both staged & working tree in git diff?" (git 2.3.4+, Q2 2015):

git status -v -v

The --cached didn't work for me, ... where, inspired by git log

git diff origin/<branch>..<branch> did.


You can use this command.

git diff --cached --name-only

The --cached option of git diff means to get staged files, and the --name-only option means to get only names of the files.


A simple graphic makes this clearer:

Simple Git diffs

git diff

Shows the changes between the working directory and the index. This shows what has been changed, but is not staged for a commit.

git diff --cached

Shows the changes between the index and the HEAD (which is the last commit on this branch). This shows what has been added to the index and staged for a commit.

git diff HEAD

Shows all the changes between the working directory and HEAD (which includes changes in the index). This shows all the changes since the last commit, whether or not they have been staged for commit or not.

Also:

There is a bit more detail on 365Git.


For Staging Area vs Repository(last commit) comparison use

 $git diff --staged

The command compares your staged($ git add fileName) changes to your last commit. If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged. This command compares your staged changes to your last commit.

For Working vs Staging comparison use

$ git diff 

The command compares what is in your working directory with what is in your staging area. It’s important to note that git diff by itself doesn’t show all changes made since your last commit — only changes that are still unstaged. If you’ve staged all of your changes($ git add fileName), git diff will give you no output.

Also, if you stage a file($ git add fileName) and then edit it, you can use git diff to see the changes in the file that are staged and the changes that are unstaged.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to diff

Create patch or diff file from git repository and apply it to another different git repository Comparing the contents of two files in Sublime Text Git diff between current branch and master but not including unmerged master commits Fast way of finding lines in one file that are not in another? Python - difference between two strings How to see the changes in a Git commit? unix diff side-to-side results? Find the files existing in one directory but not in the other git diff between two different files How to get the difference (only additions) between two files in linux

Examples related to dvcs

Pushing to Git returning Error Code 403 fatal: HTTP request failed Git push won't do anything (everything up-to-date) Mercurial — revert back to old version and continue from there How do I show the changes which have been staged? How can I stop .gitignore from appearing in the list of untracked files? Undo working copy modifications of one file in Git? What's the best three-way merge tool? What is the Difference Between Mercurial and Git?

Examples related to git-diff

How to show uncommitted changes in Git and some Git diffs in detail Git list of staged files Create patch or diff file from git repository and apply it to another different git repository There isn't anything to compare. Nothing to compare, branches are entirely different commit histories git: diff between file in local repo and origin Git diff between current branch and master but not including unmerged master commits How to Diff between local uncommitted changes and origin How to see the changes in a Git commit? How do you take a git diff file, and apply it to a local branch that is a copy of the same repository? git diff file against its last change

Examples related to git-stage

Git list of staged files Staging Deleted files Git: list only "untracked" files (also, custom commands) Show git diff on file in staging area How do I show the changes which have been staged?