Git rebase:冲突不断阻碍进度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4033009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:21:34  来源:igfitidea点击:

Git rebase: conflicts keep blocking progress

gitrebase

提问by Max Williams

I have a git branch (called v4), that was made from master just yesterday. There were a couple of changes to master, that I want to get into v4. So, in v4, I tried to do a rebase from master, and one file keeps screwing things up: a one-line text file, that contains the version number. This file is app/views/common/version.txt, which before rebasing contains this text:

我有一个 git 分支(称为 v4),它是昨天由 master 制作的。掌握了一些更改,我想进入 v4。因此,在 v4 中,我尝试从 master 进行 rebase,并且有一个文件不断搞砸:一个包含版本号的单行文本文件。这个文件是app/views/common/version.txt,它在变基之前包含以下文本:

v1.4-alpha-02

Here's what I'm doing:

这是我在做什么:

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

The version.txtnow looks like this:

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

So, I tidy it up and it looks like this now:

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

v1.4-alpha-02

and then I tried to carry on: at first I try a commit:

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

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

No luck there. So, I was trying to add the file:

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

git add app/views/common/version.txt

No response. No news is good news, I guess. So, I try to continue:

没有反应。没有消息就是好消息,我猜。所以,我尝试继续:

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

It's at this point, after going round and round with this, that I'm banging my head off the desk.

就在这一点上,在绕来绕去之后,我把头从桌子上撞了下来。

What's going on here? What am I doing wrong? Can anyone set me straight?

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

EDIT - for unutbu

编辑 - 对于 unutbu

I changed the file as you suggested and get the same error:

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

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

采纳答案by Sylvain Defresne

I encountered a similar problem with a rebase. My problem was caused because one of my commit only changed a file, and when resolving, I discarded the change introduced in this commit. I was able to solve my problem by skipping the corresponding commit (git rebase --skip).

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

You can reproduce this problem in a test repository. First create the repository.

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

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

Then commit the original content of version.txtin master.

然后提交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

Create the v4branch and change the content of 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(-)

Go back to masterand change the content of version.txtso that there will be a conflit during the rebase.

返回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(-)

Switch back to v4branch and try to rebase. It fails with a conflit in version.txtas planned.

切换回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

We resolve the conflict by selecting the mastercontent of version.txt. We add the file and try to continue our rebase.

我们通过选择 的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".

It fails ! Let's see what changes gitthink there is in our repository.

它失败 !让我们看看git我们的存储库中有哪些变化。

$ git status
# Not currently on any branch.
nothing to commit (working directory clean)

Ah ah, there is no change. If you read in detail the previous error message, gitinformed us of this and recommended to use git rebase --skip. He told us "If there is nothing left to stage, chances are that something else already introduced the same changes; you might want to skip this patch." So we just skip the commit and the rebase succeed.

啊啊,没有变化。如果您详细阅读了之前的错误消息,请git告知我们这一点并建议使用git rebase --skip. 他告诉我们“如果没有什么可以发布的,很可能其他东西已经引入了相同的变化;你可能想跳过这个补丁。” 所以我们只是跳过提交并且 rebase 成功。

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


Word of caution: Please note that git rebase --skipwill completely drop the commit that gittried to rebase. In our case, this should be okay since gitis complaining this is an empty commit. If you think you've lost changes once the rebase is complete, you can use git reflogto get the commit id of your repository before the rebase, and use git reset --hardto get your depot back in that state (this is another destructive operation).

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

回答by Bijou Trouvaille

Quoting from here: http://wholemeal.co.nz/node/9

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

Huh?!? No, I didn't forget to use git add, I did it ... like ... 2 seconds ago!

Turns out that because there is no change from the patch git suspects something has gone wrong. Git expects a patch to have been applied, but the file has remained unchanged.

The error message is not very intuitive, but it does contain the answer. We just need to tell rebase to skip this patch. It's also not necessary to fix the conflict markers in the file. You will end up with the file version from the branch you are rebasing on.

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

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

错误消息不是很直观,但它确实包含了答案。我们只需要告诉 rebase 跳过这个补丁。也没有必要修复文件中的冲突标记。您最终将获得来自您所基于的分支的文件版本。

$ git rebase --skip

回答by twalberg

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 --continueis trying to do a commit, but finding that there are no pending changes to commit (because you committed them already).

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

回答by unutbu

Change app/views/common/version.txt to

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

v1.4-alpha-01

At this point in the rebase, remember that you are resolving merge conflicts to show the progression of the non-masterbranch.

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

So, in rebasing from

所以,在从

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

to

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

the conflict you are resolving is in how to create A* on the topic branch.

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

So after doing git rebase --abort, the commands should be

所以做完之后git rebase --abort,命令应该是

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

回答by Ben Taitelbaum

The behavior you're seeing is not what I would expect from a typical rebase with just this conflict. Consider using a separate branch to do this rebase (especially if you've already pushed the commits remotely that you're fast-forwarding). Also, git mergetoolcan be helpful for resolving conflicts and remembering to issue a git add.

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

In this minimal example, the rebase works as expected. Can you provide an example that shows the behavior you're seeing?

在这个最小的例子中,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

回答by Adam Monsen

Here are some ideas:

这里有一些想法: