31

I've used a perl script to modify all tab characters in a php git repository and changed them all to 4 spaces.

$ find -iname \*.php -exec perl -pi -e "s/\t/    /g" {} \

I can commit this change with git commit, but it will mark me as the author of all changed lines inside git blame after this commit is made.

Is there any way to commit this massive change that doesn't mark me as the author of the changed lines, but retains the original author? That's a lot of history we don't really want to lose in our project.

Our purpose in replacing tabs with 4 spaces is not to make things appear different in git blame, but to follow proper PEAR coding standards. E.g. no tabs, use 4 spaces for indentation.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
nookni
  • 313
  • 3
  • 6

2 Answers2

40

It isn't the responsibility of the commit command to decide how to treat whitespaces, but the responsibility of the blame command because it is blame which analyzes the differences between versions to get the author of each line. So searching for an option to ignore whitespace in blame:

The option -w is defined as: "Ignore whitespace when comparing the parent's version and the child's to find where the lines came from." http://kernel.org/pub/software/scm/git/docs/git-blame.html

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • 1
    We're trying to follow good PEAR coding standards, which insist on 4 spaces for indentation instead of tab characters. This makes things much nicer for unification across OS/IDE implementations too... – nookni Oct 15 '10 at 20:22
  • 9
    I think you misunderstood my answer. I'm saying that you should tell blame to ignore the changes in whitespace(and thus indirectly that commit), instead of trying to explicitly mark the commit so it doesn't appear in *blame*. – CodesInChaos Oct 15 '10 at 20:32
  • 4
    Yes sadly your answer only works where the `-w` flag is actually used. In IDEs and web based tools (Github, etc.) it isn't. – Timmmm Feb 21 '17 at 12:27
  • 1
    @Timmmm that's a failure of those tools. File an issue in their GitHub issues requesting that they (optionally?) ignore whitespace changes. – iconoclast Jun 20 '18 at 02:22
21

Thanks to wnoise on git: change styling (whitespace) without changing ownership/blame?, I came up with this to run an arbitrary filter on git history, so using this you could rewrite history to make it look like offending whitespace or other issues were never committed, leaving the original authors in tact but your code cleaned up: git filter-branch --tree-filter 'git diff-tree --name-only --diff-filter=AM -r --no-commit-id $GIT_COMMIT | php cleanup.php' HEAD

Community
  • 1
  • 1
Jay Paroline
  • 2,487
  • 1
  • 22
  • 27
  • 2
    Beautiful git filter-branch --tree-filter 'git diff-tree --name-only --diff-filter=AM -r --no-commit-id $GIT_COMMIT | find -iname \*.php -exec perl -pi -e "s/\t/ /g" {} \;' HEAD – nookni Nov 15 '10 at 21:25
  • @nookni: does that mean you finally used this answer? then you should check this one as the accepted answer – Tobias Kienzler Feb 23 '11 at 14:43
  • 2
    Is there not a way in git commit to specify "for every line in the diff, keep the previous author"? e.g. fixing all the suggestions from a style linter then committing the cleaned up file? – SwimBikeRun Feb 23 '19 at 17:35
  • I think some more reading from my point of view is needed. I don't fully understand how to use this. So I make my commits that removes all of my double quotes and replaces them with single quotes then run this code? Or do I run this code, then change them :D – Jamie Hutber Apr 17 '19 at 14:57
  • @JamieHutber in my exmple, the script cleanup.php does the replacing of double quotes with single quotes – Jay Paroline Apr 25 '19 at 20:34
  • Ah interesting Jay Paroline... I've already written the changes. But I like this idea of an external file – Jamie Hutber Jun 18 '19 at 13:47