Suppose I had a git repository with code from multiple users, but some of them don’t adhere to the indentation guidelines and use spaces instead of tabs. It would be easy to change this, but by cleaning up their code, git blame becomes less useful, since then I’d be blamed for other people’s code.
I’ve seen that one can specify the commit’s author and date using the respective flags, but this solution isn’t ideal to me, since I’d have to iterate over the users and change each user’s indentation in turn. I’d also have to find out the original commit date of the changed line in order to use the --date flag.
I also know that git blame -w ignores such whitespace changes, but I’d like to make the changes transparent. I find it cumbersome to use -w whenever i blame. (And as a sidenote, I doubt that the others will remember to use the flag when extracting information from blame.) This approach will also break down if the changes aren’t related to just whitespace (e.g. exchanging single quotes with double quotes).
Is it possible to make changes to the tracked files in git without being considered the author of the changed lines and, ideally, without changing the original commit’s date?
I haven't tested this core git feature out yet, but regardless, in the second half of this answer, I say a very different strategy (GitLens) that I have tested well...
Git version 2.23 came out with a feature that can address your needs. You can specify a file that has a list of commits to ignore, when doing git blame.
Steps:
npx prettier -w --single-quote **/* to format your files. WARNING: that particular command will likely change/improve most of the files in the folder you run it in, AND it will follow any smlinks you have in that folder (if you use smlinks) to change files in those other, liked folders. (You must install Node to use npx.))Optional bonus step: read the comment someone made on that article that says how to get the GitLens extension of VS Code to pay attention to the file that lists the commits to ignore. Never use git blame on a command line ever again, and just use GitLens's git history tools.
"gitlens.blame.ignoreWhitespace": true, setting/line in your .vscode/settings.json file. (That file is for the vscode settings for everyone working on that project.)
to toggle from showing git history of that whole file to showing the git history for just the line your cursor is on, and to follow file renames reliably. THIS WORKS SO WELL, I DON'T EVEN NEED TO DO THE STUFF IN https://akrabat.com/ignoring-revisions-with-git-blame/ .
. I will tell you the magically efficient steps I follow to use it. I know the steps I have listed below look long, but they only take about 1 minute to do, once you know the steps. Here's the steps:
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