git rebase:"错误:补丁不适用"只有空白提交

时间:2018-03-29 15:09:25

标签: git git-rebase

在进行提交后尝试重新定义时出现以下错误,其中唯一的更改是添加前导空格:

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

我可以通过以下方式避免此错误:     git rebase -Xignore-space-change master

我想了解发生了什么。

Q1:导致此错误发生的原因是什么?

Q2:为什么我在git rebase时看到此错误,而git commit却没有?

要重现的命令:

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

1 个答案:

答案 0 :(得分:2)

解决方案:GIT_DIFF_OPTS=-U0(不是GIT_DIFF_OPTS=-u0

我发现这个错误是由于:    GIT_DIFF_OPTS=-u0
在我的环境中设置。这是一个错误 - 我真正想要的是:
GIT_DIFF_OPTS=-U0
(大写U,将上下文的diff行设置为0)。

-u的{​​{1}}选项告诉它创建一个补丁,它会以某种方式导致问题中显示的git diff错误。