Why git diff shows nothing?

Short answer? You’re using it wrong or you mis-configured.

It works but not as you think it does

Most common. You expected something different. Perhaps you worked on Mercurial recently? Or perhaps you thought default behaviour should be something else in your case?

This should help:

image showing git diff between commits, branches, branch and working dir, HEAD and working dir, commit and index and index and working dir, along with commands
Mark Lodato’s beautiful explanation in SVG format (available on Github)
  1. By default it compares index and working dir, nothing in index means nothing to show)
  2. If you wish to see what will be in next commit, use `--staged` or `--cached`
  3. for comparing files use either paths or `--no-index`
  4. for branches, make sure they don’t point to same commit


Either (most likely) a pager or mishandled permissions on a file. To correct:

# sets less as default pager
git config --global core.pager 'less'
# tells Git to ignore file modes
git config core.fileMode false

Pager problems

`git diff` uses pager to show results. If you

  1. don’t have any pager or
  2. have removed the default one: `less` (why would you?!) or
  3. have chosen another pager, or
  4. have configured it with auto-quit flags (-F | --quit-if-one-screen or -e | --quit-at-eof)

then `git diff` won’t show results even if they’re there. The last one deserves clarification. Usually you quit `less` only via key q. However, if you run less with extra options (or configure them for your user / system / whatever) then if you have only one screen of output, both flags will cause less to auto-quit. Since less doesn’t print to STDOUT, you lose the output.

For it’s config, check `$LESS` env variable (contains options auto-passed to less with each call) or if you’re not calling it like `less -options` somehow.

To eliminate pager, run `git --no-pager diff [...]`. If this shows output, you know where the problem is.

Permission problem

If someone is using unusual permissions and you are on Windows (why?!) you may have to bite it. Outside of that I’m yet to see this. Follow-up with whoever has unusual permissions or set your Git to ignore them altogether.

Something else

Time for some git debug.

$ GIT_TRACE=2 git diff
trace: built-in: git 'diff'
trace: run_command: 'less'
trace: exec: 'less'

Wrong – not a git repository

Checked on version 1.9.1 and it can work outside of git repo. According to commits, since 1.8.5. Obviously, requires diff to be used with `--no-index` or with two files (otherwise you WILL get error message).

Junio Hamano : git : 286bc123 – explains how it works.

Dale Worley : git : b214eddf – explains it further, when outside of the repo.

$ git diff
Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s