tags:

views:

7304

answers:

5

I'm using git with my team and would like to remove whitespace changes from my diffs, logs, merges, etc. I'm assuming that the easiest way to do this would be for git to automatically remove trailing whitespace (and other whitespace errors) from all commits as they are applied.

I have tried to add the following to by ~/.gitconfig file but it doesn't do anything when I commit. Maybe it's designed for something different. What's the solution?

[core]
    whitespace = trailing-space,space-before-tab
[apply]
    whitespace = fix

I'm using ruby in case anyone has any ruby specific ideas. Automatic code formatting before committing would be the next step, but that's a hard problem and not really causing a big problem.

A: 

This probably won't directly solve your problem, but you might want to set those via git-config in your actual project space, which edits ./.git/config as opposed to ~/.gitconfig. Nice to keep the settings consistent among all project members.

git config core.whitespace "trailing-space,space-before-tab"
git config apply.whitespace "trailing-space,space-before-tab"
bojo
A: 

If the core.whitespace directive doesn't fix your issues, you can also change the pre-commit hook (.git/hooks/pre-commit) to find and fix them for you. See this post for a detailed description.

VolkA
+12  A: 

Those settings (core.whitespace and apply.whitespace) are not there to remove trailing whitespace but to:

  • core.whitespace: detect them, and raise errors
  • apply.whitespace: and strip them, but only during patch, not "always automatically"

I believe the git hook pre-commit would do a better job for that (includes removing trailing whitespace)


Note that at any given time you can choose to not run the pre-commit hook:

  • temporarily: git commit --no-verify .
  • permanently: cd .git/hooks/ ; chmod -x pre-commit

Warning: by default, a pre-commit script (like this one), has not a "remove trailing" feature", but a "warning" feature like:

if (/\s$/) {
    bad_line("trailing whitespace", $_);
}

You could however build a better pre-commit hook, espacially when you consider that:

Committing in git with only some changes added to the staging area still results in an “atomic” revision that may never have existed as a working copy and may not work.

VonC
+2  A: 

I'd rather leave this task to your favorite editor.

Just set a command to remove trailing spaces when saving.

rjack
In vim you can do this with: autocmd BufWritePre *.cpp,*.c,*.h :%/\s\+$//e
Robert Massaioli
Sorry, I upvoted the above comment before testing it. There is a missing "s" after the percent sign, and it will move the cursor around if whitespace is found, and it will remove the last search pattern. See http://vim.wikia.com/wiki/Remove_unwanted_spaces for better alternatives.
Seth Johnson
+2  A: 

I found a git pre-commit hook that removes trailing whitespace.

  #!/bin/sh

  if git-rev-parse --verify HEAD >/dev/null 2>&1 ; then
     against=HEAD
  else
     # Initial commit: diff against an empty tree object
     against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  fi
  # Find files with trailing whitespace
  for FILE in `exec git diff-index --check --cached $against -- | sed '/^[+-]/d' | sed -r 's/:[0-9]+:.*//' | uniq` ; do
     # Fix them!
     sed -i 's/[[:space:]]*$//' "$FILE"
  done
  exit
Casey
Interesting. +1
VonC