Reputation: 5032
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 Eclipse, so I get newline changes and whitespace changes that are clogging up my workflow with noise, and making it difficult to track actual changes.
Even just a nice way to report which files have real changes and which don't would be a start, rather than having to do a diff -w for each one
Upvotes: 77
Views: 48756
Reputation: 6918
To stage changes that are not just whitespace changes, you can do:
git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero -
Afterwards, to remove all unstaged changes (those changes that differ only in whitespace), you can do:
git checkout .
(or, in newer gits, git restore .
)
Unstage your changes by doing a git reset --mixed
and continue from the top of this answer. Note that mixed
is the default mode and can be omitted.
Upvotes: 173
Reputation: 13114
I ended up doing it like this in Powershell 5.1:
# Unstage all pending changes
git reset
# Create a hashtable to store the files to undo
# By inserting all the files with changes (including whitespace only)
# And then removing the files with real changes
# Note -w flag indicates to not include whitespace only changes
$files = @{}
git diff --numstat | % { $files.Add($_.Split("`t")[2], $_) }
git diff -w --numstat | % { $files.Remove($_.Split("`t")[2]) }
# Undo the changes
$files.keys | % { git checkout -- $_ }
Upvotes: 0
Reputation: 26424
If your changes are commited and pushed...
...and you are not comfortable with advanced git command line use, but have access to JetBrains Rider IDE or JB family of tools in general, doing mixed git reset via GUI automatically removes whitespace changes. Then just commit. From 200+ files, it only missed one, which I can revert manually.
This procedure can be done 99% via GUI. Right click on base branch in the Git tab, and do reset. It will undo the commit and stage non-whitespaces changes for commit. Now commit and push -f (I always force push via command line). That's it, you are done!
Upvotes: 2
Reputation: 29
rumpel's answer was the right fix for me. I would just like to complete a bit :
If your changes have been pushed
Once you followed rumpel's procedure, do not push again on your branch. Github will ask you to pull before and you will get every whitespace diff again. Instead create a new branch from your current branch and push the new one :
# From $mybranch
git checkout -b $mybranch-v2
git push --set-upstream origin $mybranch-v2
Then on GitHub close and delete $mybranch
Upvotes: -1
Reputation: 1282
Another approach is to use dos2unix command on a unix or (windows pc as a special install) to convert the line endings and commit and push
Upvotes: 0
Reputation: 8300
(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
Upvotes: 7
Reputation: 410662
This is most likely a line ending issue; Windows uses CRLF (carriage return + line feed, or \r\n
) line endings, whereas most other systems use LF (line feed, or \n
). Luckily, Git can normalize files such that they are always stored as LF in the repo, but will be checked out as CRLF on Windows. You can do this by setting core.autocrlf
to true
on Windows:
$ git config --global core.autocrlf true
and setting core.autocrlf
to input
on Mac and Linux:
$ git config --global core.autocrlf input
More info is available here (scroll down to the section titled core.autocrlf).
Upvotes: 11