how to undo all whitespace changes with git

后端 未结 4 921
挽巷
挽巷 2021-01-31 14:54

I have a git repo, where I replaced a lot of files locally.

git status now shows many many modified files.

Some are \"really modified\", others only differ by li

4条回答
  •  醉酒成梦
    2021-01-31 15:25

    If you're allergic to bash and prefer powershell (formatting for clarity)...

    git diff  --numstat --ignore-space-change --ignore-all-space 
              --ignore-blank-lines --ignore-cr-at-eol 
              --ignore-space-at-eol 
    | % {($added,$deleted,$path)= $_.Split("`t"); 
        if ("$added$deleted" -eq "00") {$path} 
    | % { git checkout HEAD $_}
    

    Note that this is a pretty aggressive approach - the git options I've used here will also ignore added/deleted blank lines. Also remember that whitespace and even line-feeds can be semantically meaningful when in the middle of a string so use with that in mind.

    It's possible that some of the git options are redundant but I've included them for paranoia's sake. You may wish to peruse the documentation to form your own interpretation.

    Here's a slightly terser implementation for cutting and pasting...

    git diff --numstat -b -w --ignore-blank-lines --ignore-cr-at-eol --ignore-space-at-eol | % {($a,$d,$p)= $_.Split("`t");if ("$a$d" -eq "00") {$p}| % { git checkout HEAD -- $_}
    

    As was noted elsewhere, it may be advisable to take a copy of your changes before reverting by running git stash or copying to a backup folder first.

提交回复
热议问题