Git Diff: Only Show Filenames
Sometimes it’s useful to be able to list only the filenames of changed files when using the git diff command.
The way to view just the filenames when using git diff is to add a --name-only flag like this:
git diff --name-only
It takes the same arguments as a regular git diff command, so if you only want the filenames of changed blog posts stored in a content/blog/ directory, you could do something like this:
git diff --name-only content/blog/
The --name-only flag also works with git show.
Using the Technique with Other Terminal Commands
Here’s an example of how the technique can be useful.
You might run into situations where you want to open all the changed files in a code editor to make some quick changes. There is a way to open multiple files in Vim, but it needs a list of filenames, not the wall of text that is normally returned by the git diff command.
You can use vim -p to open multiple files in Vim tabs, and the list of files can be passed in by using the --name-only flag with git diff like this:
vim -p $(git diff --name-only)
The $() syntax (command substitution) replaces the contents with the value returned from the command(s) inside. So the list of files that is output by git diff --name-only is passed into vim -p.