Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git-add only whitespace changes?

Tags:

git

whitespace

The style of my project does say to trim trailing whitespace, but this makes diffs very, very messy. I like to consolidate them to one commit before I commit the actual fix.

This is closely related to Add only non-whitespace changes, but it is asking the exact opposite:

Is there a way to add only the whitespace changes to the staging area?

like image 343
Sean Allred Avatar asked Dec 15 '14 17:12

Sean Allred


People also ask

Does git ignore whitespace changes?

We use the git diff -w command to ignore all whitespace differences. It will ignore spaces at the beginning, middle, and end of lines.

How do I get rid of whitespace changes in git?

Right click on base branch in the Git tab, and do reset. It will undo the commit and stage non-whitespaces changes for commit. Now commit and push -f (I always force push via command line). That's it, you are done!

Is git add mandatory?

The git add command adds new or changed files in your working directory to the Git staging area. git add is an important command - without it, no git commit would ever do anything. Sometimes, git add can have a reputation for being an unnecessary step in development.

What is git whitespace?

in 1.6. 3.2, " whitespace " attribute that is set was meant to detect all errors known to git, but it told git to ignore trailing carriage-returns.


1 Answers

You can try the following "trick":

git add -A git diff --cached -w | git apply --cached -R 

This basically adds everything to the index, then unstages all changes which affect more than whitespace.

like image 134
nneonneo Avatar answered Sep 21 '22 06:09

nneonneo