Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove historical trailing white spaces in Git

Tags:

git

What is the simplest solution to fix committed white space issues like trailing white spaces to an existing repo?

like image 868
Daniel Avatar asked Aug 06 '13 09:08

Daniel


People also ask

How do I remove a trailing space in git?

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.

How do you fix a trailing whitespace error?

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.

How do I remove trailing spaces from a file?

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.

Why is trailing whitespace bad?

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.


2 Answers

Just discovered in the git-docs (to be tested) :

git-rebase --whitespace=fix

could also be an option.

See :

  • http://git-scm.com/book/en/Customizing-Git-Git-Configuration#Formatting-and-Whitespace
  • http://git-scm.com/docs/git-apply
like image 186
Pierre-Olivier Vares Avatar answered Sep 27 '22 22:09

Pierre-Olivier Vares


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'
like image 23
VonC Avatar answered Sep 27 '22 22:09

VonC