Git rebase 分支到 master 失败,如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8780257/
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 12:40:43  来源:igfitidea点击:

Git rebase a branch onto master failed, how to resolve?

gitgit-rebase

提问by milesmeow

I've been working on a local copy of a remote git repo. I created a branch on my local copy, let's call it 'my_branch'. I've committed a few times on my_branch.

我一直在研究远程 git 存储库的本地副本。我在本地副本上创建了一个分支,我们称之为“my_branch”。我已经在 my_branch 上提交了几次。

I recently pushed 'my_branch' to remote. However I didn't know that someone else added a version to the remote master. So, I fetched it to my local master.

我最近将“my_branch”推到了远程。但是我不知道其他人向远程主服务器添加了一个版本。所以,我把它拿给了我当地的主人。

So...long story short, my local repo looks like this (I'm trying to use the diagraming convention here) .

所以......长话短说,我的本地仓库看起来像这样(我正在尝试使用图表约定here)。

 --C0--------------C7--  (local master)
      \
       --C1-C2-C3--     (local my_branch)
             \
              --C4-C5-C6--     (local sandbox_branch)

I want it to look like:

我希望它看起来像:

 --C0--------------C7--  (local master)
                      \
                       --C1'-C2'-C3'--     (local my_branch)
                             \
                              --C4'-C5'-C6'--     (local sandbox_branch)

I tried to rebase my_branch ONTO local master but I got this error message (I'm using a visual tool for git called GitX):

我试图将 my_branch 重新设置为 ONTO 本地主机,但收到此错误消息(我正在使用名为 GitX 的 git 可视化工具):

Rebase Failed!
There was an error rebasing HEAD with branch 'master'.

command: git rebase refs/heads/master

It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
rebase.  If that is not the case, please
    rm -fr /my_project_directory/.git/rebase-apply
and run me again.  I am stopping in case you still have something
valuable there.

What am I doing wrong? How should I handle this? If I were to do this on the command line what is the command to get me to the state in the diagram above?

我究竟做错了什么?我该如何处理?如果我要在命令行上执行此操作,让我进入上图中状态的命令是什么?

UPDATE 1

更新 1

BTW, I'm not in the middle of an application patch or another rebase...at least not intentional. After I found out that remote was updated AFTER I pushed, I did a fetch. Could that have done anything to make GitX think that I'm in the middle of an application patch or another rebase?

顺便说一句,我不是在应用程序补丁或另一个 rebase 中间......至少不是故意的。在我发现遥控器在我推送后更新后,我做了一个提取。这能做些什么让 GitX 认为我正处于应用程序补丁或另一个 rebase 中?

I've also updated the diagram to be more accurate. There is a branch off of my_branch. I didn't include it in the original question b/c I didn't think that it would matter. I'm including just in case...

我还更新了图表以使其更准确。my_branch 有一个分支。我没有将它包含在原始问题 b/c 中,我认为这无关紧要。我包括以防万一...

UPDATE 2

更新 2

FYI...The master tree for 'local' and for 'remote' looks like the diagram that I drew, except it doesn't have the sandbox_branch.

仅供参考......“本地”和“远程”的主树看起来像我画的图,除了它没有sandbox_branch。

回答by Siddhartha Reddy

git rebase has found a .git/rebase-applydirectory and so presumes that you might be in the middle of a rebase. This would have happened if there was a conflict during a previous rebase and the rebase was not finished; i.e. you did not run one of git rebase --abort, git rebase --skipor git rebase --continue(the last one after resolving the conflict).

git rebase 已找到一个.git/rebase-apply目录,因此假定您可能处于 rebase 的中间。如果在之前的 rebase 期间发生冲突并且 rebase 没有完成,就会发生这种情况;即您没有运行其中之一git rebase --abortgit rebase --skipgit rebase --continue(解决冲突后的最后一个)。

Anyway, it does not matter how you ended up in this state if you don't think you ran git rebase at all. Simply rm -fr /my_project_directory/.git/rebase-applyas the help suggests and you should be able to do the rebase now.

无论如何,如果您认为自己根本没有运行 git rebase,那么您最终如何处于这种状态并不重要。简单rm -fr /my_project_directory/.git/rebase-apply的帮助提示,你现在应该可以做底垫。

But wait. Since you say that you have already published your branch to the remote repository, you should not try to rebase master onto it. In fact, if your remote is set to deny non-fast-forward commits (which seems to be a generally recommended best practice), you'll not even be able to push the rebase'd changes to your remote. In general, it is a bad practice to try to modify a commit (which is what git rebasedoes) after you have published it to a remote.

可是等等。既然你说你已经将你的分支发布到远程存储库,你不应该尝试将 master 变基到它上面。事实上,如果您的遥控器设置为拒绝非快进提交(这似乎是普遍推荐的最佳实践),您甚至无法将 rebase 的更改推送到您的遥控器。通常,在将提交git rebase发布到远程后尝试修改提交(正是这样做的)是一种不好的做法。