git notes main page | gitolite main page | license

IMPORTANT NOTE: although this page has a "gitolite.com" URL, this is not about gitolite. That's just an artifact of "sitaramc.github.com" being translated to "gitolite.com" and so ALL my git related stuff gets carried over. Gitolite documentation has another /gitolite in the URL, so you can tell. My apologies for this confusion.

fixup whitespace errors

When you have whitespace errors in a repo and want to fix them all together, here’s what you do. Written by doener circa 2009-07-12 19:23

So let’s say the following code is in ~/git-scripts/ws-fix.sh

#!/bin/bash

# from doener (who else!)
# to be called as an index-filter

if git rev-parse --quiet --verify $GIT_COMMIT^ >/dev/null
then
        against=$(map $(git rev-parse $GIT_COMMIT^))
        git reset -q $against -- .
else
        # Initial commit: diff against an empty tree object
        against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
        git rm --cached -rfq --ignore-unmatch '*'
fi

git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix

Notes:

Other possibilities:

If the whitespace problem causing conflicts is something like indentation shift, etc., and is not amenable to git apply --whitespace=fix, the following may work. We assume the “remote” version is the one with the unnecessary indentation changes:

git show :1:filename > filename
git diff -b :1:filename :3:filename | git apply
# (might need munging; the diff header has :1: and :3: in it...)
git merge-one-file :1:filename :2:filename $(git hash-object -w filename) filename