What does git diff –name-only do?

Git is an essential tool for version control and collaboration in the world of software development. Among its many commands, git diff is particularly useful for examining changes between commits, branches, and more. But, what does git diff --name-only do?


The git diff --name-only command is a variation of the standard git diff command, which is used to show changes between commits, commit and working tree, etc. The --name-only option modifies the output of this command to show only the names of files that have changed.

Here is a more detailed explanation:

  • git diff: This command is used to display the differences between two git objects (like commits). By default, it shows the actual changes made to content, line by line.
  • git diff --name-only: The --name-only option changes the output to show just the file names that have changed. This command doesn’t display what changes were made, just the list of files affected.

This command can be very useful in a variety of situations. For example:

  • When you have made changes to a large number of files and want to quickly see which files have been modified without scrolling through all the line-by-line changes.
  • When you want to use the output of this command as an input for another command. Since it only outputs the file names, it’s useful in scripting and automation.

Here’s an example of usage:

$ git diff --name-only

In this case, git diff --name-only is showing us that three files (README.md, src/index.js, and src/components/Button.js) have been modified. But remember, it doesn’t show what changes were made.

To see the actual changes, you would use git diff without the --name-only option.

Remember, the git diff --name-only command operates on your working tree by default. If you want to compare other commits, you can provide them as arguments, like git diff --name-only <commit1>..<commit2>.

In conclusion, git diff --name-only is a helpful command that can save time when you’re only interested in what files have been changed, not how they were changed.