DEV Community


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

Darragh O'Riordan
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!
Originally published at on ・1 min read

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...
Enter fullscreen mode Exit fullscreen mode

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.


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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Discussion (1)

jackvial profile image
Jack Vial

That's for sharing this Darragh. My PRs are looking prettier!
Here's a neat enough one liner for it. git diff --diff-filter=MA --name-only dev... | xargs npx prettier --write