'git diff' doesn't give any output

Why do you get no git diff output before adding?

Git does not treat files in the filesystem as automatically included in the version control system. You have to add things explicitly into the Git repository (as you are doing by adding the current directory with git add .).

There is no output to git diff because Git doesn't see any changes inside your repository, only files outside the repository, which it considers 'untracked' and so ignores when generating a diff.

I found this one of the key differences to version control systems like SVN (along with staging and ignoring directories).

If you want the untracked files to be included, git add them.

If you don't want them in your repository, add them to your .gitignore (see git ignore --help). This is good for C object files or Python .pyc files.

Why don't I get git diff output after adding?!

So this is slightly different. If you do git status you will see the file is now in the staging area. This is the area for files that you are about to commit.

When you git add a new file into the Git repository, it skips the working copy and goes straight into the staging area. This make sense in a way, and git add always moves files into staging area whether it is tracked or untracked.

To see the differences between the last check in and the staging area do git diff --cached.

To see the differences between the staging area and your working copy, do git diff. If there is nothing in the staging area then this is the same as doing a diff between the last check in and your working copy.


I have seen situations where there really should be output from git diff, but there isn't; adding --no-pager in between git and diff does work:

git --no-pager diff

...so does explicitly setting the pager to be less with

git config --global core.pager 'less'

Even though less is supposed to be the default pager.

This was in Ubuntu 12.04 LTS (Precise Pangolin).

I found the information about pager settings in the Git documentation.


1.How, in plain English, does git diff work?

How git diff works all depends on what parameters you pass to it.

Depending on the parameters, git diff will show the changes between two commits, or between a commit and the working tree, etc...

For example, git diff, git diff --staged and git diff HEAD are described further below.

2.How can I show a diff of all the changes I've made (unstaged and staged)?

By using both commands git diff HEAD and git diff --staged.

But in the case of a repository just created with git init, the question doesn't really make sense and git diff HEAD cannot be used: the history being empty, there's no commit and no HEAD yet!


  • git diff shows the changes in your working tree relative to the last commit, only for tracked files

  • git diff HEAD shows the changes in your working tree relative to the last commit (includes files that are not tracked)

  • git diff --staged (or its synonym git diff --cached) shows the changes you staged for the next commit relative to the last commit

There are many other ways to invoke git diff, for comparing arbitrary commits or branches, etc. Invoke git help diff to read more about it.

Tags:

Git