How to list files changed in current branch (and run prettier on them)

Published on February 12, 2021

I needed to run prettier on only the files changed in one branch of my git repo. This solution is a bit hacky but it did the trick!

Use git to get a list of the files

Get the files that were added or modified between this branch and master

git diff --diff-filter=MA --name-status master...

Edit output to run prettier for each file

Once you have this list you can edit it in an editor to run prettier on each file. You might need to edit the path if your pretteir config sets a different root path than the git diff.

e.g.

Output from git diff

M       /projectroot/subproject/src/mypath/file1.ts
M       /projectroot/subproject/src/mypath/file2.ts
A       /projectroot/subproject/src/mypath/file3.ts

Edit this to be

npx prettier --write src/mypath/file1.ts
npx prettier --write src/mypath/file2.ts
npx prettier --write src/mypath/file3.ts

and copy paste into terminal. Done!

There is probably a neat one-liner bash command that could modify and pipe git output directly into prettier but I don’t know how to do that. This works great if not doing this too often!

You could also try replacing the new lines from git diff with a space in your editor and supplying the list to prettier that way. I haven’t tested this though!

npx prettier --write src/mypath/file1.ts src/mypath/file2.ts src/mypath/file3.ts

Note: if you just need to prettify files in a commit you can use pretty quick

Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia building and supporting happy teams that create high quality software for the web.

I also make tools for busy developers! Do you have a new M1 Mac to setup? Have you ever spent a week getting your dev environment just right?

My Universal DevShell tooling will save you 30+ hours of configuring your Windows or Mac dev environment with all the best, modern shell and dev tools.

Get DevShell here: ✨ https://devshell.darraghoriordan.com


Read more articles like this one...

List of article summaries

#developer-experience

Start tracking DORA metrics for your team in just 15 minutes with Apache Dev Lake

DORA (DevOps Research and Assessment) metrics are an excellent way for engineering organisations to measure and improve their performance.

Up until now, monitoring the DORA metrics across Github, Jira, Azure Devops etc required custom tooling or a tedious manual process.

With Apache Dev Lake you can get beautiful reporting for DORA metrics on your local machine in as little as 15 minutes (honestly!).

From Google Sheets to Grafana
From Google Sheets to Grafana

#developer-experience

How to use SSH with Git and ssh-agent on Windows

I needed to run git natively in windows (no wsl) for a recent project. I use ssh certificates with passphrases to authenticate with my git provider.

Ssh requires the certificate passphrase every time you use a connection. It’s annoying typing this passphrase in to terminal when using a git command.

#developer-experience

How to fix custom type definitions not being type checked

If you have a custom types file (e.g. myTypes.d.ts) but you get no errors from the type checker and compiler in typescript even though you know there are issues in the file you might have skipLibCheck turned on.

#developer-experience

Consistent modern shell tooling on MacOS and Windows WSL for developers

I regularly code on both MacOS and Windows machines and I was always annoyed how different the default experiences are on each. I need to use the same tools and the same experience on both.

Windows “WSL” (Windows Subsystem for Linux) is a great tool for this you can use on Windows 10 and newer. The latest version lets you run a full Ubuntu instance that integrates seamlessly with the underlying windows instance.

By using WSL2 you can have a (mostly) identical developer experience jumping between MacOS and Windows.

Better tooling for Developers

Many of the terminal tools that come with unix environments are functionally similar to how they were 20 years ago. But other developer tooling has advanced quite a bit since then.

You can replace tools like ls or cat with modern equivalents that support full colour, unicode icons, git state and more. Terminal prompts can be made git aware and use colour to indicate state so you don’t have to query git so often.

Keeping developer experience consistent across machines

Keeping any shell changes you make on one machine up to date on all the machines you code on is a nightmare without the right tooling.

This article also explains all the tools I use and how I keep the same terminal setup consistent on MacOS and Windows!

Let’s go!

Comments