Reputation: 48829
I've accidentally put some whitespace in my initial commit. It shows up red in git diff --color
. How do I get rid of the existing whitespace and how can I avoid this from happening again?
Upvotes: 20
Views: 16872
Reputation: 1450
git rebase --whitespace=fix --root
This was already linked to in https://stackoverflow.com/a/26299760/2716218 but worth putting as it's own clean answer.
Upvotes: 0
Reputation: 135
And to trim the white spaces from all files recursively from all sub directories this can be used.
find ./* -type f -exec sed -i 's/[[:space:]]*$//' {} \;
Upvotes: 2
Reputation: 48829
To trim trailing whitespace on all files in the current directory, use:
sed -i 's/[[:space:]]*$//' *
To warn about future whitespace errors (both trailing spaces and spaces before tabs), and to fix whitespace errors in patches, add the following code to your gitconfig
file:
[core]
whitespace = trailing-space,space-before-tab
[apply]
whitespace = fix
Upvotes: 31
Reputation: 933
See this thread git remove trailing whitespace in new files before commit on using git rebase
to strip whitespace from files that you've already committed.
Upvotes: 2
Reputation: 411122
core.whitespace
instructs git to flag certain whitespace problems:
trailing-space
warns about whitespace at the end of a line or at the end of a filespace-before-tab
warns when there is a space before a tab used for indentationapply.whitespace
is used when applying a patch. It checks for whitespace errors (the ones listed above, in core.whitespace
) and applies the patch after attempting to fix them (i.e., remove them).
These options go in ~/.gitconfig
-- that is, a .gitconfig
file at the root of your user's home directory (typically /home/user/.gitconfig
on Linux, /Users/user/.gitconfig
on Mac OS X, and I don't know where on Windows but I suppose somewhere in C:\Documents and Settings\user
).
Upvotes: 15