What is the difference between git status and git diff --name-only?
Besides the --name-flag
that displays only the file name, the git diff
command (without other arguments) will display the difference between your index and your working directory (as explained in the diagaram here).
Whereas the git status
command will display the whole current status of your working tree (files that are staged, modified, deleted, untracked etc.)
However, by considering the -s
flag that shorten the output of the status
command (which typically display only names), we can have some situations where these two commands will give you "almost" the exact same output.. It all depends of what you already did in your working directory.
For example, if you do git status -s
, you will get all the differences from your working directory against your HEAD
and your index
including untracked files.. (But say you don't have untracked files..)
On the other hands, and regardless of what you already staged or not, if you do git diff --name-only HEAD
(see the above-mentioned diagram), there is a chance that you get almost the same output.
Also, the results are almost the same (not exactly the same) because git status -s
shows pretty useful additional information:
- a letter at the beginning of the line that summarize the status (M for modified, D for deleted, A for added, ? for untracked files)
- a color code that shows what are already staged (green for staged, red for unstaged).
So, we can consider that diff
is more suited to view differences in actual file contents. That is why not displaying untracked files by diff
makes sense.. Comparing differences starting from only something we have (the tracked files)..
All that said, if you want some concise output about your actual changes (after all this is the rational for using --name-only
) you should really consider git status -s
.
On one hand, the git diff --name-only
returns the name of the files which are ready to be staged on the next commit. As following :
git diff --name-only
/src/main/java/com/core/First.java
/src/main/java/com/core/Second.java
On the other hand, git status
provides you not only the details of the files to be staged in the current working repository, but also the comparison with the origin of your branch.
git status
On branch myBranch
Your branch is up-to-date with 'origin/myBranch'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: /src/main/java/com/core/First.java
modified: /src/main/java/com/core/Second.java
no changes added to commit (use "git add" and/or "git commit -a")