git diff gives no output

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 repo 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 gives no 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 stackoverflow 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:

  • How, in plain English, does git diff work?
  • 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.


    Why do you get no git diff before adding?

    git does not treat files on 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 sees no 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 vcs 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 you're repo add them to you're .gitignore (see git ignore --help ). This is good for C object files or python .pyc files.

    Why do I get no git diff 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 repo it skips the working copy and does straight into the staging area. This make sense in a way, 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. I'm just adding this in case others with the same problem come looking for a solution and the above doesn't answer the question.

    I found the information about pager settings in the git docs.


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

    Just type

    git add test.txt
    

    or

    git add .
    

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

    链接地址: http://www.djcxy.com/p/36256.html

    上一篇: 如何在jQuery中选择具有其名称属性的元素?

    下一篇: git diff不提供输出