问题
Why is that git diff thinks there are no changes
..even if git status reports them as modified?
$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: file-added
modified: file-with-changes << it knows there are changes
but in order to see the difference, I need to explicitly add the last reversion hash..
$ git diff
(nothing)
$ git diff rev-hash
diff --git a/file-with-changes b/file-with-changes
index d251979..a5fff1c 100644
--- a/file-with-changes
+++ b/file-with-changes
.
..
回答1:
Please try git diff --staged command.
More options you can use.
git diff
shows changes between index/staging and working files. In your case, git add put the file-with-changes to staging area. Hence, no difference between staging and working files.
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
回答2:
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 HEADto see the differences between your tree state and theHEADrevision, orgit diff --cachedto see the differences between your index and theHEADrevision.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/26126876/why-git-diff-reports-no-file-change-after-git-add