Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "git rebase" give "error: patch does not apply" with whitespace-only commit?

Tags:

git

git-rebase

I get the following error when trying to rebase after making a commit whose only change is adding leading whitespace:

First, rewinding head to replay your work on top of it...
Applying: other-branch: modify myfile-1: add leading whitespace
Using index info to reconstruct a base tree...
error: patch failed: myfile-1:1
error: myfile-1: patch does not apply
error: Did you hand edit your patch?
It does not apply to blobs recorded in its index.
Patch failed at 0001 other-branch: modify myfile-1: add leading whitespace
The copy of the patch that failed is found in: .git/rebase-apply/patch

I can avoid this error with: git rebase -Xignore-space-change master

I would like to understand what is happening.

Q1: What causes this error to happen?

Q2: Why do I see this error with git rebase, but not with git commit?

Commands to reproduce:

export GIT_DIFF_OPTS=-u0
cd /tmp
rm -fr my-repo
git init my-repo
cd my-repo

cat > myfile-1 << EOF
line 1
line 2 (note that 2 lines are necessary to reproduce)
EOF
git add .
git commit -am 'master: no leading whitespace in myfile-1'

git checkout master
git checkout -b other-branch
cat > myfile-1 << EOF
      line 1
line 2 (note that 2 lines are necessary to reproduce)
EOF
git commit -am 'other-branch: modify myfile-1: add leading whitespace'

git checkout master
echo foo > myfile-2
git add .
git commit -am 'master: non-conflicting commit (add myfile-2)'

git checkout other-branch
git rebase master  # this fails -- why?
git rebase --abort
git rebase -Xignore-space-change master  # this succeeds
like image 758
Rob Bednark Avatar asked Jan 19 '26 14:01

Rob Bednark


1 Answers

Solution: GIT_DIFF_OPTS=-U0 (not GIT_DIFF_OPTS=-u0)

I discovered this error was being caused by having:
GIT_DIFF_OPTS=-u0
set in my environment. This was a mistake -- what I really wanted was:
GIT_DIFF_OPTS=-U0
(uppercase U, to set diff lines of context to be 0).

The -u option to git diff tells it to create a patch, which somehow causes the git rebase error shown in the question.

like image 173
Rob Bednark Avatar answered Jan 22 '26 02:01

Rob Bednark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!