7

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?

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84

3 Answers3

10

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"

Abhishek Arya
  • 450
  • 4
  • 16
  • Is it possible to avoid the whitespace while commit? Its creating big issue while review the Pull Request. – ArrchanaMohan Aug 08 '18 at 06:57
  • I think this way, you will be able to commit the non-whitespace changes. What do you mean by `Is it possible to avoid the whitespace being commit?`? – Abhishek Arya Aug 08 '18 at 06:59
  • Like I don't want to commit the whitespace. Only i want to commit the code changes that I intent to commit. do I need to ran the above command before I execute git commit -am "" ? – ArrchanaMohan Aug 08 '18 at 07:01
  • If you see the screenshot that attached. I didn't make any changes in that line. I just did cntr+shift+F. But in git It shows changes because of format. I dn't wanna happen that – ArrchanaMohan Aug 08 '18 at 07:02
  • Edited my answer – Abhishek Arya Aug 08 '18 at 07:04
  • I can't see your screenshot – Abhishek Arya Aug 08 '18 at 07:04
  • It's been attached with question. Let me try yours suggestion, and will accept. If it resolves my issue. – ArrchanaMohan Aug 08 '18 at 07:06
  • When you get patch failed message run `git diff -w | git apply --cached --ignore-whitespace --reject` it will omit things that cannot be easily fixed – Pawel Mar 25 '22 at 13:30
  • Note that no space changes are staged after `git apply` but space changes are left unstaged. `git checkout .` will delete those spaces. `git diff --cached` will show the staged changes. – xvan Oct 04 '22 at 02:08
2

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.
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
0

You now can setup a GitHub Action which will, when getting a new commit or a pull request, will fail if there is any whitespace issue.

Git itself illustrates that automation process with Git 2.30 (Q1 2021).

See commit 32c83af (22 Sep 2020) by Chris. Webster (webstech).
(Merged by Junio C Hamano -- gitster -- in commit 1a42a77, 27 Oct 2020)

ci: github action - add check for whitespace errors

Signed-off-by: Chris. Webster
Reviewed-by: Jeff King

Not all developers are aware of [git diff --check](https://github.com/git/git/blob/32c83afc2c69aa51b82aa223f2099389f1f0be0a/Documentation/diff-options.txt#L415)<sup>([man](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---check))</sup> to warn about whitespace issues.
Running a check when a pull request is opened or updated can save time for reviewers and the submitter.

A GitHub workflow will run when a pull request is created or the contents are updated to check the patch series.
A pull request provides the necessary information (number of commits) to only check the patch series.

To ensure the developer is aware of any issues, a comment will be added to the pull request with the check errors.

You can see here the .github/workflows/check-whitespace.yml script

It starts on pull request:

on:
  pull_request:
    types: [opened, synchronize]

It will use git log --check to detect if a commit includes any conflict markers or whitespace errors.


With Git 2.30 (Q1 2021), the whitespace checker is more robust:

See commit cba2504 (03 Nov 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 15486b6, 11 Nov 2020)

ci: make the whitespace checker more robust

Signed-off-by: Johannes Schindelin

In 32c83afc2c69 ("ci: github action - add check for whitespace errors", 2020-09-22, Git v2.30.0 -- merge listed in batch #1), we introduced a GitHub workflow that automatically checks Pull Requests for whitespace problems.

However, when affected lines contain one or more double quote characters, this workflow failed to attach the informative comment because the Javascript snippet incorrectly interpreted these quotes instead of using the git log(man) output as-is.

Let's fix that.

While at it, let's await the result of the createComment() function.

Finally, we enclose the log in the comment with ... to avoid having the diff marker be misinterpreted as an enumeration bullet.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250