Field Note

Getting the list of changed files for a given Git branch

git diff diff-tree
Posted on Tuesday, July the 30th 2024
1 min read

Let’s assume we want to run a couple of static checks on all changed files of a Git branch. Then the canonical answer of how to get the list of changed files is git diff-tree: for example,

git diff-tree --name-only --no-commit-id -r main HEAD

would give us the list of files on the current branch that have changed compared to the main branch.

There is just one problem with this approach: git diff-tree doesn’t have an option to filter by some pattern (e.g. to filter by a certain file suffix). This is possible, however, with git diff. E.g. the output of

git diff --name-only -r main HEAD -- '*.sh'

would only contain the .sh files with changes on the current branch compared to the main branch.

friedrichkurz.me

© 2025 Friedrich Kurz

Privacy Policy