20

I've accidentally put some whitespace in my initial commit - it shows up red in git diff --color. What's the best way to get rid of the existing whitespace and how can I avoid this happening again?

I am not necessarily looking for a built-in git command. Any external program available for free on Ubuntu would also be welcome.

Zaz
  • 46,476
  • 14
  • 84
  • 101
  • awesome I didn't know git had this feature. – xenoterracide Jul 30 '10 at 17:39
  • 1
    See [this question](http://stackoverflow.com/q/591923/470844). – ntc2 Mar 13 '13 at 23:46
  • Does this answer your question? [Make git automatically remove trailing whitespace before committing](https://stackoverflow.com/questions/591923/make-git-automatically-remove-trailing-whitespace-before-committing) – Carlos Marx Mar 14 '21 at 17:44

4 Answers4

31

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
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
Zaz
  • 46,476
  • 14
  • 84
  • 101
  • 1
    this is good for protecting against whitespace commits but the OP asked about removing whitespace they's already commited – stevejpurves Oct 10 '14 at 12:45
  • @stevejpurves: The top line explains how to remove trailing whitespace using *sed*. As far as I know, there is no git command that does the same thing. – Zaz Jan 07 '15 at 16:38
15

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 file
  • space-before-tab warns when there is a space before a tab used for indentation

apply.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).

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 1
    That explains the whitespace settings (+1 for that) but doesn't answer the question "How do I get rid of existing whitespace?". I've updated the original post for clarity. – Zaz Jul 30 '10 at 18:43
2

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.

Community
  • 1
  • 1
stevejpurves
  • 923
  • 1
  • 7
  • 12
1

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:]]*$//' {} \;
Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
rshdzrt
  • 125
  • 7