被困在git rebase中...如何重置

44

我正在进行 git rebase 的过程中遇到了困难。我不太记得具体发生了什么,但我正在使用 UI 并删除已检出的分支,然后一切似乎都消失了。我重启了,成功地创建和提交了其他分支的工作,但后来我注意到状态显示我仍处于 rebase 的中间阶段。

如果我尝试

git rebase --skip
git rebase --continue
git rebase --abort

每次都失败

error: could not read '.git/rebase-merge/head-name': No such file or directory

有没有办法让我回到一个稳定的状态?我真的不关心rebase相关的内容,所以不想回到仍处于rebase过程中的那个点。

编辑:

$ git status
On branch fix/SJW-01225
Your branch is up to date with 'core-v3/fix/SJW-01225'.

You are currently rebasing.
(all conflicts fixed: run "git rebase --continue")

Untracked files:
(use "git add <file>..." to include in what will be committed)

     [long list of untracked files]

nothing added to commit but untracked files present (use "git add" to track)

编辑-1:

$ touch .git/rebase-merge/head-name

$ git rebase --abort
error: could not read '.git/rebase-merge/onto': No such file or directory

$ touch .git/rebase-merge/onto

$ git rebase --abort
error: could not get 'onto': ''

谢谢


当你输入 git status 命令后,能否将 git 返回的内容粘贴在这里? - dunajski
尝试创建文件:touch .git/rebase-merge/head-name - Maroun
TVM。根据您的建议进行了编辑。 - Simon Woods
1
这是一个学习实验吗?也许现在是git clone的时候了? - Karol Dowbecki
很遗憾,不行! - Simon Woods
8
好的,以下是对 https://xkcd.com/1597/ 的翻译:面向专家的编程 (黑板上写着:面向对象的编程)如果你想让世界变得更好, 学会如何与计算机沟通。如果你想让生活变得更好, 学会如何让计算机与其他人沟通。 - user330315
5个回答

60
为了摆脱被破坏的git rebase,你可以采取以下措施:
1. 回滚到已知状态。你可以通过git reflog找出你开始rebase的提交。
例如,reflog会给你以下信息。rebase的起点是最后一个rebase (start)或者rebase -i (start),如果你进行了交互式rebase,那么它就是HEAD@{1}。
$ git reflog
f10ccfed (HEAD) HEAD@{0}: rebase : fast-forward
383aa038 (origin/master, origin/HEAD) HEAD@{1}: rebase (start): checkout HEAD~10
0600cf7e (origin/Files, master, Files) HEAD@{4}: checkout: moving from master to Files
0600cf7e (origin/Files, master, Files) HEAD@{5}: commit: fixes
f10ccfed (HEAD) HEAD@{6}: commit: refactoring

所以你需要的是:
git checkout master # assuming you were on master
git reset --hard HEAD@{1}

删除rebase-merge文件夹
rm -rf .git/rebase-merge

3
在 Windows 中,我需要引用提交。 git reset --hard "HEAD@{1}" - PackBjamin
在我的情况下,它找不到rebase-merge文件夹。在重置Head之后,我运行了rebase --abort,一切都正常。 - KYLO

21

我也不知道怎么把我的仓库变成了同样的状态。在我的情况中,没有 .git/rebase-merge 目录,但我找到了一个 rebase-apply 目录。除了正在进行的 rebase 外,git status 是干净的,所以唯一需要做的就是运行以下命令以退出奇怪的错误 rebase 状态:

rm -rf .git/rebase-apply/

16

git rebase --quit 对我很有用。


请注意 --abort--quit 之间的区别:https://dev59.com/3bLma4cB1Zd3GeqPiOdK。使用 --quit 不会将 HEAD 移回,而原始问题是:“有没有办法回到稳定状态? - Gino Mempin

1

我曾经遇到过类似的问题:

$ git status
On branch master
You are currently rebasing.
  (all conflicts fixed: run "git rebase --continue")
nothing to commit, working tree clean
$ git rebase --abort
error: could not read '.git/rebase-merge/head-name': No such file or directory
$ git rebase --skip
error: could not read '.git/rebase-merge/head-name': No such file or directory
$ git rebase --continue
error: could not read '.git/rebase-merge/head-name': No such file or directory

然后我尝试启动新的变基,我得到了这个提示(顺便说一下,这是我第一次看到Git用第一人称来引用自己):

$ git rebase -i HEAD~1
fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
    git rebase (--continue | --abort | --skip)
If that is not the case, please
    rm -fr ".git/rebase-merge"
and run me again.  I am stopping in case you still have something
valuable there.
$ rm -fr ".git/rebase-merge"
$ git status
On branch master
nothing to commit, working tree clean
$ git push origin master
Everything up-to-date

因此,在我的情况下,问题是通过强制递归地删除Git的rebase-merge目录,即rm -fr ".git/rebase-merge" 解决的。

1
遇到了一个类似的问题,其中--abort无法工作。
$ git rebase master
error: could not write index
fatal: Could not detach HEAD
First, rewinding head to replay your work on top of it...

使用时无法找到任何有关该存储库的问题

$ git status
On branch XXX
nothing to commit, working tree clean
$ ls .git/r*
#Nothing of interest

我通过清理解决了这个问题,即执行以下命令(如果您有未提交的更改,则非常具有破坏性)

git clean -xfd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接