Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

 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 . .. 
like image 640
eridal Avatar asked Sep 30 '14 17:09

eridal


People also ask

Why git diff does not show changes?

Your file is already staged to be committed. You can show it's diff using the --cached option of git. To unstage it, just do what git status suggests in it's output ;) You can check The Git Index For more info.

Does git diff show added files?

The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file.

Can I make changes after git add?

In git, files (per se) aren't staged, changes are. So when you do git add <filename> , it is the current state of that file that is staged. If you change the file and want to add those changes as well, you just do another git add .


1 Answers

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

like image 93
Bhaskar Avatar answered Oct 08 '22 03:10

Bhaskar