Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between git status and git diff --name-only?

Tags:

git

These two commands seem to have options which will cause them to show the same information.

like image 549
Nick Avatar asked Jan 28 '16 19:01

Nick


People also ask

What is git diff -- name only?

To explain further: The -z to with git diff --name-only means to output the list of files separated with NUL bytes instead of newlines, just in case your filenames have unusual characters in them. The -0 to xargs says to interpret standard input as a NUL-separated list of parameters.

What does git status mean?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.

What does git diff mean?

Comparing changes with git diff Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

What is the difference between git diff and git diff -- cached commands?

git diff --cached - Show what has been added to the index via git add but not yet committed. git diff - Show what has changed but hasn't been added to the index yet via git add .


2 Answers

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.

like image 183
AkaiBukai Avatar answered Sep 18 '22 08:09

AkaiBukai


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")
like image 29
Naman Avatar answered Sep 19 '22 08:09

Naman