Sam Sam - 1 year ago 104
Git Question

How can you combine git add patch -p mode with diff's ignore-all-space

How can I do git add with patch mode but ignoring whitespace changes.

The use case is for when you've reformatted a file and also made changes to it. I want to commit the real code changes separately first (as shown by git diff -w path) and then commit the reformatting as a separate commit.

Answer Source

I suggest simply roundtripping a diff


git diff --ignore-all-space | (git reset --hard && git apply)

Warning: this is fraught with danger because of the git reset there (it will not preserve changes to binary files as written). Perhaps you'd want a bash function similar to

function cleanup_patch()
    if [ $# -lt 1 ]; then 
        echo 'Must provide explicit paths (wildcards allowed)'; 
        git diff --ignore-all-space -- "$@" |
            (git checkout HEAD -- "$@" &&
             git apply)

Afaict the seemingly useful --binary option to diff doesn't honour the whitespace ignore flags

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download