why `git diff` reports no file change after `git add`

前端 未结 4 1920
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-01 16:16

 Why is that git diff thinks there are no changes

..even if git status reports them as modified?

$ git status
On bra         


        
相关标签:
4条回答
  • 2021-02-01 16:50

    Because git diff by default checks differences between the staging area and your working copy. When you git add, your staging area matches your working copy and therefore diff reports no changes.

    Adding the --cached flag tells diff to diff against HEAD.

    0 讨论(0)
  • 2021-02-01 17:05

    Please try git diff --staged command.

    Alternative options available are listed below.

    git diff

    shows changes between index/staging and working files. Since, in your case, git add moved your files-with-changes to staging area, there were no modifications shown/seen.

    git diff --staged

    shows changes between HEAD and index/staging. git diff --cached also does the same thing. staged and cached can be used interchangeably.

    git diff HEAD

    shows changes between HEAD and working files

    git diff $commit $commit

    shows changes between 2 commits

    git diff origin

    shows diff between HEAD & remote/origin

    0 讨论(0)
  • 2021-02-01 17:13

    git diff diffs against the index, not against your HEAD revision. By running git add, you've put the changes in your index, so of course there are no differences! Use

    • git diff HEAD to see the differences between your tree state and the HEAD revision, or
    • git diff --cached to see the differences between your index and the HEAD revision.
    0 讨论(0)
  • 2021-02-01 17:15

    Ran into the exact same problem.

    • Add the new file that you created using git add filename1.c
    • Make another change in some other filename2.c that was already a part of the repository tracking system.
    • Do a git diff and you will only see the change to filename2.c show up. Changes to filename1.c will not show up.
    • However if you do a git status you will see the changes in both filename1.c and filename2.c show up.
    • Do a git commit -a -m "Changes to filename1.c and filename2.c blah blah"
    • Do a git push

    You will see that filename1.c got committed.

    0 讨论(0)
提交回复
热议问题