Opening all the files that have been modified in a Git branch
Today a colleague asked for a way to open all the files that have changed in a particular Git branch. They were reviewing a large pull request, and sometimes it’s easier to review files in your local editor than in GitHub’s code review interface. You can see the whole file, run tests or local builds, and get more context than the GitHub diffs.
This is the snippet I suggested:
git diff --name-only "$BRANCH_NAME" $(git merge-base origin/main "$BRANCH_NAME") \
| xargs open -a "Visual Studio Code"
It uses a couple of nifty Git features, so let’s break it down.
How this works
There are three parts to this command:
Work out where the dev branch diverges from main. We can use
git-merge-base
:$ git merge-base origin/main "$BRANCH_NAME" 9ac371754d220fd4f8340dc0398d5448332676c3
This command gives us the common ancestor of our main branch and our dev branch – this is the tip of main when the developer created their branch.
In a small codebase, main might not have changed since the dev branch was created. But in a large codebase where lots of people are making changes, the main branch might have moved on since the dev branch was created.
Here’s a quick picture:
This tells us which commits we’re reviewing – what are the changes in this branch?
Get a list of files which have changed in the dev branch. We can use
git-diff
to see the difference between two commits. If we add the--name-only
flag, it only prints a list of filenames with changes, not the full diffs.$ git diff --name-only "$BRANCH_NAME" $(git merge-base …) assets/2025/exif_orientation.py src/_drafts/create-thumbnail-is-exif-aware.md src/_images/2025/exif_orientation.svg
Because we're diffing between the tip of our dev branch, and the point where our dev branch diverged from main, this prints a list of files that have changed in the dev branch.
(I originally suggested using
git diff --name-only "$BRANCH_NAME" origin/main
, but that's wrong. That prints all the files that differ between the two branches, which includes changes merged to main after the dev branch was created.)Open the files in our text editor. I suggested piping to
xargs
andopen
, but there are many ways to do this:$ git diff … | xargs open -a "Visual Studio Code"
The
xargs
command is super useful for doing the same thing repeatedly – in this case, opening a bunch of files in VS Code. You feed it a space-delimited string, it splits the string into different pieces, and runs the same command on each of them, one-by-one. It’s equivalent to running:open -a "Visual Studio Code" "assets/2025/exif_orientation.py" open -a "Visual Studio Code" "src/_drafts/create-thumbnail-is-exif-aware.md" open -a "Visual Studio Code" "src/_images/2025/exif_orientation.svg"
The
open
command opens files, and the-a
flag tells it which application to use. We mostly use VS Code at work, but you could pass any text editor here.Reading the manpage for
open
, I'm reminded that you can open multiple files at once, so I could have done this without usingxargs
. I instinctively reached forxargs
because I’m very familiar with it, and it’s a reliable way to take a command that takes a single input, and run it with many inputs.