Last Updated: February 25, 2016
·
12.12K
· ChrisMissal

Remove Whitespace From Staged Files in Git

The following alias lets you remove any whitespace from files that have been added to your index (staged):

wsf = !git commit -mTemp && git stash && git rebase HEAD~ --whitespace=fix && git reset --soft HEAD~ && git stash pop

To explain what is going on here:

  1. Commit your currently staged files
  2. Stash any uncommitted changes
  3. Rebase your last commit (from step 1) and remove any whitespace changes.
  4. Reset back to your previous commit, but keep changes in the index.
  5. Pop your previously uncommitted changes back off the stash.

Source: http://stackoverflow.com/questions/19151940/git-remove-trailing-whitespace-in-new-files-before-commit#comment28450261_19156679

3 Responses
Add your response

Just don't put whitespaces in your code :D

over 1 year ago ·

Use good IDE it will clean it for you every time you save a file

over 1 year ago ·

Use "git commit --no-verify -mTemp" if you have a pre-commit whitespace enforcement policy.

over 1 year ago ·