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

前端 未结 4 2127
别跟我提以往
别跟我提以往 2021-01-31 08:36

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 t

4条回答
  •  悲&欢浪女
    2021-01-31 09:11

    Note: This answer is old. 6 years down the road, the other answer by Justin is much better. Prefer to use git apply --cached

    I suggest simply roundtripping a diff

    Idea:

    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)'; 
        else
            git diff --ignore-all-space -- "$@" |
                (git checkout HEAD -- "$@" &&
                 git apply)
        fi
    }
    

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

提交回复
热议问题