Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I've found tons of sites that explain how to have git warn you when you're changing line endings, or miscellaneous other techniques to prevent you from messing up an entire file. Assume it's too late for that -- the tree already has commits that toggle the line endings of files, so git diff shows the subtraction of the old file followed by the addition of a new file with the same content

I'm looking for a git configuration option or command-line flag that tells diff to just ignore those -- if two lines differ only by whitespace, pretend they're the same. I need this config option/flag to work for anything that relies on file differences -- diff, blame, even merge/rebase ideally -- I want git to completely ignore trailing whitespace, particularly line endings. How can I do that?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

For diff, there's git diff --ignore-space-at-eol, which should be good enough. For diff and blame, you can ignore all whitespace changes with -w: git diff -w, git blame -w.

For git apply and git rebase, the documentation mentions --ignore-whitespace.

For merge, it looks like you need to use an external merge tool. You can use this wrapper script (untested), where favorite-mergetool is your favorite merge tool; run git -c mergetool.nocr.cmd=/path/to/wrapper/script merge. The result of the merge will be in unix format; if you prefer another format, convert everything to that different format, or convert $MERGED after the merge.

#!/bin/sh
set -e
TEMP=$(mktemp)
tr -d '\013' <"$BASE" >"$TEMP"
mv -f "$TEMP" "$BASE"
TEMP=$(mktemp)
tr -d '\013' <"$LOCAL" >"$TEMP"
mv -f "$TEMP" "$LOCAL"
TEMP=$(mktemp)
tr -d '\013' <"$REMOTE" >"$TEMP"
mv -f "$TEMP" "$REMOTE"
favorite-mergetool "$@"

To minimize trouble with mixed line endings, make sure text files are declared as such.

See also Is it possible for git-merge to ignore line-ending differences? on Stack Overflow.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.