The other day, I was trying to make the CI workflow on a project run only tests that related to the changes made on the topic branch. To that end, I wanted to get the list of file paths that changed on a topic branch. Let’s say we want to show the changes between the head of a-branch
(5) and its branch point (2) like below.
--- 1 --- 2 --- 4 (main)
\
3 --- 5 (a-branch)
To show changes between the HEAD
of a topic branch and the branch point from the base branch, specify the base branch as the --merge-base
option for git diff
. This will show diffs between 2 and 5 in the example above.
git diff --merge-base origin/main
Note that the option --merge-base
was added to git diff
in version 2.30.0
. This is a short-hand option that is equivalent to the following command.
git diff $(git merge-base origin/main HEAD)
For my original purpose, I added the --name-only
option as well to show only the file paths of the changes.
git diff --name-only --merge-base origin/main