Make git undo any whitespace-only changes?

后端 未结 5 1422
清歌不尽
清歌不尽 2021-01-31 09:28

Is there an easy way to automatically do a git checkout on any file that only has whitespace changes? I\'m dealing with both Windows and some code generation that\'s run from Ec

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 09:57

    If your changes are commited

    (there’s probably a smarter way but this works for me)

    mybranch=master
    git checkout -b tmp origin/master
    
    # compute the non-ws diff to mybranch and apply it
    git diff -U0 -w --no-color $mybranch | git apply -R --cached --ignore-whitespace --unidiff-zero -
    
    git commit -m "non ws changes"
    git reset --hard  # discard all non-staged data
    

    Your now on a new "tmp" branch. You may want to clean up (NOTE: this loses history of $mybranch)

    git checkout $mybranch
    git reset --hard tmp
    git branch -D tmp
    

提交回复
热议问题