122

我有一个 git 分支(称为 v4),它是昨天由 master 制作的。对 master 进行了一些更改,我想进入 v4。所以,在 v4 中,我尝试从 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".

正是在这一点上,在绕着这个转圈之后,我把头从桌子上撞了下来。

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

编辑 - 为 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".
4

6 回答 6

105

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

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

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

然后提交version.txtin master 的原始内容。

$ 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我们通过选择 的内容来解决冲突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告知我们并建议使用git rebase --skip. 他告诉我们“如果没有任何东西可以上演,很可能其他东西已经引入了相同的变化;你可能想跳过这个补丁。” 所以我们只是跳过提交并且变基成功。

$ git rebase --skip
HEAD is now at 7313eb3 master

注意事项:请注意,这将完全放弃尝试变基git rebase --skip的提交。git在我们的例子中,这应该没问题,因为git抱怨这是一个空提交。如果您认为 rebase 完成后您丢失了更改,您可以使用git reflog在 rebase 之前获取存储库的提交 id,并使用git reset --hard将您的 depot 恢复到该状态(这是另一个破坏性操作)。

于 2011-01-31T14:06:02.477 回答
23

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

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

事实证明,因为补丁没有变化,git怀疑出了点问题。Git 期望已应用补丁,但文件保持不变。

错误信息不是很直观,但它确实包含了答案。我们只需要告诉 rebase 跳过这个补丁。也没有必要修复文件中的冲突标记。你最终会得到你正在变基的分支的文件版本。

$ git rebase --skip
于 2011-01-31T10:35:11.000 回答
6

That error message is a result of your git commit -a -m "merged". If you just fix up the file, then run git add <file>, and git rebase --continue, it should work fine. git rebase --continue is trying to do a commit, but finding that there are no pending changes to commit (because you committed them already).

于 2012-05-22T15:18:24.750 回答
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
于 2010-10-27T12:19:26.630 回答
4

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

在这个最小的例子中,变基按预期工作。您能否提供一个示例来显示您所看到的行为?

#!/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
于 2010-10-27T13:25:30.313 回答
4

这里有一些想法:

于 2011-01-24T23:55:52.177 回答