Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run git rebase and ignore whitespace?

Tags:

git

git-merge

I have branch where I have one commit that only modifies whitespace (trailing whitespace and leading whitespace).

I ran git rebase master and git rebase --ignore-whitespace master and in both cases I have a merge conflict with the commit that only changed whitespace.

like image 277
jcubic Avatar asked Jun 25 '14 15:06

jcubic


People also ask

What does git rebase skip do?

You can run git rebase --skip to completely skip the commit. That means that none of the changes introduced by the problematic commit will be included. It is very rare that you would choose this option. You can fix the conflict.

Does git ignore whitespace?

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


2 Answers

I can't guarantee that this will help, but you could try
git rebase -Xignore-space-change master
or
git rebase -Xignore-all-space master.
-X passes options into the merge algorithm, and the default merge algorithm has these options to affect how it handles whitespace.

like image 191
Brian Campbell Avatar answered Oct 10 '22 18:10

Brian Campbell


This is easier with Git 2.29 (Q4 2020)

"git rebase -i"(man)learns a bit more options, including --ignore-whitespace on merge (which was not supported in 2014 when the OP used that option).

So git rebase --ignore-whitespace master will now work!

See commit 6160b2e (26 Aug 2020) by Junio C Hamano (gitster).
See commit 2712669 (17 Aug 2020), and commit ef484ad (13 Jul 2020) by Rohit Ashiwal (r1walz).
See commit a3894aa, commit 7573cec, commit e8cbe21 (17 Aug 2020) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit 9c31b19, 03 Sep 2020)

rebase -i: add --ignore-whitespace flag

Signed-off-by: Rohit Ashiwal
Signed-off-by: Phillip Wood

Rebase is implemented with two different backends - 'apply' and 'merge' each of which support a different set of options.

In particular the apply backend supports a number of options implemented by 'git am(man) ' that are not implemented in the merge backend.

This means that the available options are different depending on which backend is used which is confusing.

This patch adds support for the --ignore-whitespace option to the merge backend.
This option treats lines with only whitespace changes as unchanged and is implemented in the merge backend by translating it to -Xignore-space-change.

git rebase now includes in its man page:

--ignore-whitespace:

Ignore whitespace differences when trying to reconcile differences.
Currently, each backend implements an approximation of this behavior:

apply backend: When applying a patch, ignore changes in whitespace in context lines. Unfortunately, this means that if the "old" lines being replaced by the patch differ only in whitespace from the existing file, you will get a merge conflict instead of a successful patch application.

merge backend: Treat lines with only whitespace changes as unchanged when merging.
Unfortunately, this means that any patch hunks that were intended to modify whitespace and nothing else will be dropped, even if the other side had no changes that conflicted.

This flag is passed to the 'git apply' program

like image 39
VonC Avatar answered Oct 10 '22 18:10

VonC