Back to TILs.

Calmcode TIL

Show Changed Files in Git. logoShow Changed Files in Git.

Let's say that you've got a large git project. This project might have lots and lots of files that need checking on every commit. But maybe, some of the automated checks only need to run on a subset of all the files. Maybe they should only run on the files that actually changed.

For these situations there's a lovely command;

git diff --name-status main | cat

This compares the current git state with the main branch that you have locally. You can also make a comparison with the main branch on Github as well via;

git diff --name-status origin/main | cat

Combining it

You can chain this command using xargs such that tools like flake8 can check only the specific files that changed.

git diff --name-status main | xargs flake8

If you're running your own blog, you could even use it to speed up the build times. If you had a script, render.py that reads files from stdin to build and render render then you might build a faster build pipeline via;

git diff --name-status origin/main | grep md | xargs python render.py

In our experience, it makes for a nice addition to a Makefile.


Back to main.