I am trying to format an entire repo using a code formatter tool. In doing so, I want to keep information about who committed which line, so that commands like git blame still show the correct information. By this, I mean it should show the author that previously edited each line (before it was formatted).
There is the git filter-branch command which allows you to run a command against each revision of the repo starting from the beginning of time.
git filter-branch --tree-filter '\
npx prettier --write "src/main/web/app/**/**.{js, jsx}" || \
echo "Error: no JS files found or invalid syntax"' \
-- --all
It will take forever to run this and really I don't care about the past. I just want to format the master branch going forward without changing ownership of each line. How can I do this? I tried playing with the rev-list at the end and other filter types but it still doesn't work. There must be a way to format the codebase while preserving the author information for each line.
You can make git blame ignoring certain commits, which do only mass reformatting etc.:
Create a file .git-blame-ignore-revs like:
# Format commit 1 SHA:
1234af5.....
# Format commit 2 SHA:
2e4ac56.....
Then do
git config blame.ignoreRevsFile .git-blame-ignore-revs
, so that you don't have to use the --ignore-revs-file option every time with git blame.
Upvote https://github.com/github/feedback/discussions/5033 to get that feature into github's web blame viewer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With