What is the simplest solution to fix committed white space issues like trailing white spaces to an existing repo?
remove trailing whitespace from all lines. collapse multiple consecutive empty lines into one empty line. remove empty lines from the beginning and end of the input. add a missing \n to the last line if necessary.
Use your editor to find the end of the line and backspace. Many modern text editors can also automatically remove trailing whitespace from the end of the line, for example every time you save a file. In emacs: C-M-% <space> + $ then press return twice.
Method One: sed A simple command line approach to remove unwanted whitespaces is via sed . The following command deletes all spaces and tabs at the end of each line in input. java . If there are multiple files that need trailing whitespaces removed, you can use a combination of find and sed commands.
Whitespace is not always insignificant. In some cases, trailing whitespace can significantly change the meaning of a line of code or data. In most cases whitespace is there to format the code for human readers.
Just discovered in the git-docs (to be tested) :
git-rebase --whitespace=fix
could also be an option.
See :
If you have existing commits in your repo with trailing whitespaces, then you would need to change those commit (rewite history), which can be painful if that repo is already push and cloned by others.
But if that is an option, then you can use a script like the one in gitolite:
#!/bin/bash
# from doener (who else!)
# to be called as an index-filter
if git rev-parse --quiet --verify $GIT_COMMIT^ >/dev/null
then
against=$(map $(git rev-parse $GIT_COMMIT^))
git reset -q $against -- .
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
git rm --cached -rfq --ignore-unmatch '*'
fi
git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix
run this like:
git filter-branch --index-filter '. ~/git-scripts/ws-fix.sh'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With