卡在 git 变基中...如何重置

Stuck in a git rebase ... how to reset

我正在通过 git 变基,但我被卡住了。我不记得到底发生了什么,但我正在使用 UI 并删除了一个已签出的分支,一切似乎都变得空白了。我重新启动并设法做了一些其他工作,创建并提交给其他分支等,但后来我注意到一个状态说我仍在重新设置基础

如果我尝试

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 rebase,您可以执行以下操作

  1. 重置为已知状态。你可以找出你从哪个提交开始你的 rebasegit reflog

例如,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}
  1. 删除 rebase-merge 文件夹
    rm -rf .git/rebase-merge

我以某种方式让我的存储库进入了相同的状态。在我的例子中,没有 .git/rebase-merge 目录,但我确实找到了一个 rebase-apply 目录。除了正在进行的 rebase 之外,git status 是干净的,所以我唯一需要做的就是摆脱奇怪的 bad rebase 状态 运行:

rm -rf .git/rebase-apply/

我遇到了类似的问题:

$ 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

然后我尝试启动一个新的 rebase,我得到了这个线索(顺便说一下,我第一次看到 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

所以在我的例子中,通过 rm -fr ".git/rebase-merge". [=13] 强制递归删除 Git 的 rebase-merge 目录解决了问题=]

遇到了类似的问题,其中 --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