'git diff' doesn't give any output

Git

Git Problem Overview


If I run git diff I would expect to see a list of changes of my working directory relative to whatever had been committed before (or a list of the working directory contents if it's a new repository with no commits). Try this example:

$ mkdir temp
$ cd temp
$ git init
$ echo "first line" > test.txt
$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test.txt
nothing added to commit but untracked files present (use "git add" to track)

Let's see a diff of test.txt:

$ git diff

This doesn't give any output!

I would expect to see a diff like + first line, but instead I get nothing. It doesn't tell me what's going on. People on Stack Overflow tell me to git add some files so I do:

$ git add .
$ git diff

Still nothing!

Git GUI shows the changes.

git status -v shows the changes.

But for some reason git diff doesn't show anything.

So my questions are:

  1. How, in plain English, does git diff work?
  2. How can I show a diff of all the changes I've made (unstaged and staged)?

Some people at my company are using Git, but the SVN crowd are going to point at this as a case of where Git is too confusing to be usable.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

> 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.

Solution 4 - Git

Basing on your git status output there is nothing to show for git diff without additional parameters. There is nothing to show for git diff --cached and git diff HEAD as all of these commands rely on changes already known to Git.

You have no staged files and no changed files from those that are under version control now.

After you add test.txt under Git control you will get the desired output.

Just type

git add test.txt

or

git add .

Then this file will be added under version control. And future changes of this file will be shown by git diff.

Solution 5 - Git

I had a LESS=-R environment variable set. It made git diff show a blank screen.

The solution for me:

unset LESS

Solution 6 - Git

This is indeed related to the pager settings.

I found that including '-e' in

export PAGER=less LESS=-cse

helps.

Without the '-e', less will quit on the first page. Somehow git will not even bother to display output in that case.

Solution 7 - Git

GIT DIFF DOESN'T WORK AFTER STASH

Someone may run into this after attempting to apply stashed changes like I did.

  • Stash changes
  • Move to different branch and do some work
  • return to work-in-progress branch, update (git pull) then git stash apply (possibly with merge conflict)
  • git diff no longer works...

At this point simply stash your changes again (git stash), do git pull to ensure everything is up to date (because I'm paranoid about it), double check that all files are up to date in working directory (possibly run rsync if dealing with remote/local), then attempt to git stash apply again and it should work!

In answer to the original question, git diff isn't showing anything because you have a brand new directory, with a newly added file, but there are zero changes in the file for git diff to show. git status is showing that you added a new file, but git diff is for showing changes within files. If you made changes to the file, committed it, then made additional changes to it and ran git diff it would then show you the changes to the file.

Solution 8 - Git

You don't need any additional flags for git diff to work. First you create a file, and add it to the git repo with git add to staging and or execute a git commit to get it into your repository. Now, go to the file and make a change to it and save it. Just put a sentence in there stating "I will run the "git diff " command right after I save it". Save it. It is at that point you will see the difference when you run the git diff command. It will show the recent changes prior to executing a "git commit" or if you had only put the file initially into staging.

\TestGit>git diff File2.txt
diff --git a/File2.txt b/File2.txt
index e2ba019..eff7299 100644
--- a/File2.txt
+++ b/File2.txt
@@ -6,4 +6,6 @@ File two. Which is the second file that I will NOT add with the first file File1

 The goal here is to add the File1 but not the File 2

-using git add File1.txt
\ No newline at end of file
+using git add File1.txt.
+
+So far I have just done a git add on this file.
\ No newline at end of file

The lines with the + signs is the added content. If you use the --cached flag, and get result,s then your git status for the same file should report that the file has not been committed. Once you commit the file, you will not get any output for git diff. even with the --cached flag.

Solution 9 - Git

The first things is that I suggest you to save your file before using "git diff" command to make changes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblokeleyView Question on Stackoverflow
Solution 1 - Gitm0tiveView Answer on Stackoverflow
Solution 2 - GitDan TenenbaumView Answer on Stackoverflow
Solution 3 - GitBludzeeView Answer on Stackoverflow
Solution 4 - GitligView Answer on Stackoverflow
Solution 5 - GitfaflView Answer on Stackoverflow
Solution 6 - GitHenk LangeveldView Answer on Stackoverflow
Solution 7 - GitVallierView Answer on Stackoverflow
Solution 8 - GitH-manView Answer on Stackoverflow
Solution 9 - GitLogesh R.View Answer on Stackoverflow