Search code examples
gitgit-rebase

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


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

Solution

  • 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.