Ignore whitespaces changes in all git commands

35,851

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:
35,851
Michael Mrozek
Author by

Michael Mrozek

Updated on September 18, 2022

Comments

  • Michael Mrozek
    Michael Mrozek over 1 year

    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?

  • Smallen
    Smallen about 9 years
    Is there any configuration to always use the -w flag with git blame?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 9 years
    @Thayne Not that I know of. You can define an alias like bl = blame -w but you can't redefine the name of built-in commands and I don't see a configuration option to make it the default. But I am not a git expert.
  • Ejaz
    Ejaz over 5 years
    for git status ?