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

Published on February 12, 2021

Tagged: #developer-experience

Follow me on twitter for more posts like this

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 enjoying the mountains and the ocean.

I build and support happy teams that create high quality software for the web.

Contact me on Twitter!


Read more articles like this one...

List of article summaries

#developer-experience

Modern shell tooling for devs on MacOS and Windows WSL

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

You can replace many of these cli tools with equivalents that support full colour, unicode icons, git and more. Changing OS and git defaults will better match your workflow.

Your prompt can be made git aware and use colour to indicate state so you don’t have to query git so often.

I work on MacOS and Windows machines and I’ll show you how to have the same terminal and experience on both.

#developer-experience

How engineers can help deliver software effectively

Delivery managers and team leads have the responsibility to deliver a software system via an engineering team.

Your customer wants every feature to work perfectly and they want it delivered yesterday. Your team wants to learn and grow.

It’s a tough role managing all the stakeholders and creators in a project.

Engineers can help drive great delivery by empathising with and supporting the delivery manager or leads in a project team.

#developer-experience

How to identify a great tech organisation before you join

No one wants to waste time working for an organisation that’s a bad fit. But how do you know a good place from a bad place until you actually work there?

It’s important to spend the same amount of time analysing a potential organisation as they spend analysing you!

During an interview process an organisation has personality testing, resumes, tech testing, behavioural testing, reference checks, bank checks, police checks and more.

If it sounds like a very one-sided process that’s because it is! But there are ways you can bring more balance and here are some important things to research before accepting any engineering job offer.

#developer-experience

Set vscode as the default for text files on mac

How to set the default text editor to be visual studio code on the mac

Update 29 Jan 2022 - I added this and more to comprehensive instructions for configuring a modern developer shell and MAcOS in another article here. You should check that out.