Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid whitespace being commit with github

Tags:

git

github

I made a 2 lines of change but in git It shows more lines of changes because of whitespace. Its very difficult for us to review the exact code changes.

The below are the command that I used to push the code.

First I will pull the code base from Different repo and merge with my local. And push my changes to my fork, and raise PR.

git add .
git commit -am "changes"
git pull upstream master
git push origin master

but I didn't find any whitespace remover option in my git console as well. Only I can see "UNIFIED", "SPLIT".

Screenshot

Please find the below sample screenshot for reference.

enter image description here

Is there any way that we can ignore all whitespace that being commit.

Any suggestion leads?

like image 701
ArrchanaMohan Avatar asked Aug 08 '18 06:08

ArrchanaMohan


People also ask

How do I ignore whitespace in diff?

Please note that -w option will ignoring all whitespaces before diffing, so a line like this i s a line and this is a line in each file will compare as thisisaline and will not report differences. So you should use sed to remove those whitespaces occurred at start first then do `diff -bB.

How do you ignore white spaces in C++?

The standard solution is to use the std::remove_if algorithm to remove whitespace characters from std::string using the Erase-remove idiom technique.


2 Answers

You can use git diff -w | git apply --cached --ignore-whitespace as mentioned here

EDIT: After you make any change, run
git diff -w | git apply --cached --ignore-whitespace
then
git commit -m "your message"

like image 180
Abhishek Arya Avatar answered Sep 21 '22 08:09

Abhishek Arya


Another way to look at the problem is to be more cautious with the way you stage your changes.

In your above example you're using two very broad sweeping ways to search for changes in your codebase before committing.

git add .
git commit -am "changes"

The . parameter for add is already staging all detected changes (not exactly, see the excellent answers here for more details), but on top of that you're using the -a parameter for commit, which I'd describe as redundant overkill, since you're staging ALL changes AGAIN.

Your context may make my following advice more or less practical, but you might consider adding your changed files "manually" :

# check your changes (at this point, whitespace differences should not show up)
git status

# let's assume the above line showed changes in 3 files
git add <path/to/file1> <path/to/file2> <path/to/file3>

# then commit without staging parameter
git commit -m "changes"

And if the process seems tedious to you, keep in mind that :

  • You can copy and paste paths straight from the right above status output, which makes it quick
  • There is also an interactive mode invoked with add -i, which allows you to iteratively look at changes to decide what's to be actually staged.
like image 20
Romain Valeri Avatar answered Sep 18 '22 08:09

Romain Valeri