Git rebase:冲突不断阻碍进展

时间:2010-10-27 12:13:16

标签: git rebase

我有一个git分支(称为v4),它是昨天由master制作的。掌握了一些变化,我想进入v4。因此,在第4版中,我尝试从master执行rebase,并且一个文件不断搞砸:一行文本文件,其中包含版本号。此文件为app/views/common/version.txt,在重新定位之前包含此文本:

v1.4-alpha-02

这就是我正在做的事情:

> git rebase master
First, rewinding head to replay your work on top of it...
Applying: new version, new branch
error: patch failed: app/views/common/version.txt:1
error: app/views/common/version.txt: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging app/views/common/version.txt
CONFLICT (content): Merge conflict in app/views/common/version.txt
Failed to merge in the changes.
Patch failed at 0001 new version, new branch

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

version.txt现在看起来像这样:

<<<<<<< HEAD:app/views/common/version.txt
v1.4-alpha-02
=======
v1.4-alpha-01
>>>>>>> new version, new branch:app/views/common/version.txt

所以,我整理了它,现在看起来像这样:

v1.4-alpha-02

然后我试着继续:首先我尝试提交:

> git commit -a -m "merged"
# Not currently on any branch.
nothing to commit (working directory clean)

那里没有运气。所以,我试图添加文件:

git add app/views/common/version.txt

没有回应。我想,没有消息是好消息。所以,我试着继续:

> git rebase --continue
Applying: new version, new branch
No changes - did you forget to use 'git add'?

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

正是在这一点上,经过这一轮,我正在敲打桌子。

这里发生了什么?我究竟做错了什么?谁能让我直截了当?

编辑 - for unutbu

我按照您的建议更改了文件并得到了同样的错误:

> git rebase master
First, rewinding head to replay your work on top of it...
Applying: new version, new branch
error: patch failed: app/views/common/version.txt:1
error: app/views/common/version.txt: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging app/views/common/version.txt
CONFLICT (content): Merge conflict in app/views/common/version.txt
Failed to merge in the changes.
Patch failed at 0001 new version, new branch

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

6 个答案:

答案 0 :(得分:97)

我遇到了与rebase类似的问题。我的问题是因为我的一个提交只更改了一个文件,并且在解析时,我放弃了此提交中引入的更改。我通过跳过相应的提交(git rebase --skip)来解决我的问题。

您可以在测试存储库中重现此问题。首先创建存储库。

$ mkdir failing-merge
$ cd failing-merge
$ git init
Initialized empty Git repository in $HOME/failing-merge/.git/

然后在master中提交version.txt的原始内容。

$ echo v1.4-alpha-02 > version.txt
$ git add version.txt
$ git commit -m initial
[master (root-commit) 2eef0a5] initial
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 version.txt

创建v4分支并更改version.txt的内容。

$ git checkout -b v4
Switched to a new branch 'v4'
$ echo v1.4-alpha-03 > version.txt
$ git add version.txt
$ git commit -m v4
[v4 1ef8c9b] v4
 1 files changed, 1 insertions(+), 1 deletions(-)

返回master并更改version.txt的内容,以便在rebase期间出现混乱。

$ git checkout master
Switched to branch 'master'
$ echo v1.4-alpha-04 > version.txt
$ git add version.txt
$ git commit -m master
[master 7313eb3] master
 1 files changed, 1 insertions(+), 1 deletions(-)

切换回v4分支并尝试重新绑定。它按计划在version.txt中失败了。

$ git checkout v4
Switched to branch 'v4'
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: v4
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging version.txt
CONFLICT (content): Merge conflict in version.txt
Recorded preimage for 'version.txt'
Failed to merge in the changes.
Patch failed at 0001 v4

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".
$ cat version.txt
<<<<<<< HEAD
v1.4-alpha-04
=======
v1.4-alpha-03
>>>>>>> v4

我们选择master的{​​{1}}内容来解决冲突。我们添加文件并尝试继续我们的rebase。

version.txt

失败了!让我们看看$ echo v1.4-alpha-04 > version.txt $ git add version.txt $ git rebase --continue Applying: v4 No changes - did you forget to use 'git add'? If there is nothing left to stage, chances are that something else already introduced the same changes; you might want to skip this patch. When you have resolved this problem run "git rebase --continue". If you would prefer to skip this patch, instead run "git rebase --skip". To restore the original branch and stop rebasing run "git rebase --abort". 认为我们的存储库中存在哪些变化。

git
啊啊,没有变化。如果您详细阅读了上一条错误消息,$ git status # Not currently on any branch. nothing to commit (working directory clean) 通知我们并建议您使用git。他告诉我们“如果没有任何东西可以上台,很可能其他东西已经引入了相同的变化;你可能想跳过这个补丁。”所以我们只是跳过提交并且rebase成功。

git rebase --skip

谨慎提示:请注意$ git rebase --skip HEAD is now at 7313eb3 master 将完全删除git rebase --skip尝试重新绑定的提交。在我们的例子中,这应该没问题,因为git抱怨这是一个空提交。如果您认为在rebase完成后丢失了更改,则可以使用git在rebase之前获取存储库的提交ID,并使用git reflog将您的depot恢复到该状态(这是另一种破坏性的行动。)

答案 1 :(得分:20)

从这里引用:http://wholemeal.co.nz/node/9

  

咦?!?不,我没有忘记使用git   加,我做了......就像... 2秒   前!

     

原来,因为没有   从补丁git嫌疑人改变   出了点问题。 Git期待   已经应用的补丁,但是   文件保持不变。

     

错误消息不是很多   直观,但确实包含了   回答。我们只需要告诉rebase   跳过这个补丁。它也不是   必须修复冲突标记   在文件中。你最终会得到   来自分支机构的文件版本   重新定位。

$ git rebase --skip

答案 2 :(得分:6)

将app / views / common / version.txt更改为

v1.4-alpha-01

在rebase的这一点上,请记住您正在解决合并冲突,以显示非主分支的进展。

所以,从

重新定位
      A---B---C topic
     /
D---E---F---G master

              A*--B*--C* topic
             /
D---E---F---G master

您正在解决的冲突是如何在主题分支上创建A *。

执行git rebase --abort后,命令应为

git checkout topic
git rebase master
< make edits to resolve conflicts >
git add .
git rebase --continue

答案 3 :(得分:5)

该错误消息是您git commit -a -m "merged"的结果。如果你只修改了文件,然后运行git add <file>git rebase --continue,它应该可以正常工作。 git rebase --continue正在尝试进行提交,但发现没有挂起的更改要提交(因为您已经提交了它们)。

答案 4 :(得分:4)

你所看到的行为并不是我所期望的只有这种冲突的典型变种。考虑使用一个单独的分支来执行此rebase(特别是如果您已经远程推送了快速转发的提交)。此外,git mergetool可以帮助您解决冲突并记住发布git add

在这个最小的例子中,rebase按预期工作。您能举例说明您所看到的行为吗?

#!/bin/bash

cd /tmp
mkdir rebasetest
cd rebasetest
git init
echo 'v1.0' > version.txt
git add version.txt
git commit -m 'initial commit'
git checkout -b v4
echo 'v1.4-alpha-01' > version.txt
git add version.txt
git commit -m 'created v4'
git checkout master
git merge v4
echo 'v1.4-alpha-01-rc1' > version.txt
git add version.txt
git commit -m 'upped version on master to v1.4-alpha-01-rc1'
git checkout v4
echo 'v1.4-alpha-02' > version.txt
git add version.txt
git commit -m 'starting work on alpha-02'

git rebase master
echo 'v1.4-alpha-02' > version.txt
git add version.txt
git rebase --continue

答案 5 :(得分:4)

以下是一些想法: