git notes main page | gitolite main page | license

IMPORTANT NOTE: although this page has a "" URL, this is not about gitolite. That's just an artifact of "" being translated to "" 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/


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

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

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


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