gitwhitespacecode-cleanupremoving-whitespace

How do I get rid of existing whitespace in my initial commit?


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?


Solution

  • 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