(Update 2020-03-01: there’s a better way to do this, see below)
Situation
You’ve been working on a (Git) branch and you need to generate the list of files modified on that branch.
Why?
GitHub shows it: it’s useful to see in a PR. (maybe looking for surprises)
Maybe you need to run tests or a linter but it takes forever to run it for the whole codebase. Running
it on selected files makes it a bit faster. (completely hypothetical! 😄)
How Not to Do It..
First, I found all the commits on a branch, manually.
Then I tried to git log --name-only COMMIT1 COMMIT2 … which was kind of close.
The list of files was there … now I needed to grep it out of the output. I also needed to remove duplicates (with sort | uniq or awk).
Bottom line: long, error-prone, and messy.
A Better Way
First, I found all the commits on a branch, manually.
Let’s skip that step, I’ll come back to it in a moment.
I didn’t know that git diff also has the --name-only flag!
Yes! Just the files, without duplicates.
What about “finding all the commits?” I found it at the usual place.
Let’s double-check git log --graph:
(click to enlarge)
Looks right: man git-merge-base says:
Find as good common ancestors as possible for a merge