你如何从 `git rebase --autostash` 获得丢失的 "autostash" 提交

How do you get a lost "autostash" commit from `git rebase --autostash`

当使用 git rebase --autostash 时,git 会自动创建一个 "autostash" 提交,并会在 rebase 成功后重新应用它。

但是如果变基被中止(例如,当它是一个交互式变基时,通过在 Vim 中使用 :cq),自动存储提交可能最终成为悬空提交。

Git 2.9.0

我发现以下列出了所有 "autostash" 提交:

git log --pretty='%cr: %h %s' $(git fsck --no-reflog \
  | grep '^dangling commit' | cut -f3 -d\ ) | grep ': autostash$'

然后您可以使用提交哈希来取回提交,例如使用 git showgit cherry-pick.

输出如下所示:

Checking object directories: 100% (256/256), done.
2 minutes ago: 7a50bcb On improve-moving-out-of-zoomed-tmux-pane: autostash
22 minutes ago: 9c504af On pr-123: autostash
5 weeks ago: f216b45 On look-for-vim-with-pgrep-ps: autostash
9 weeks ago: f405faa On look-for-vim-with-pgrep-ps: autostash
10 weeks ago: 28ddead On look-for-vim-with-pgrep-ps: autostash

Git 2.10(2016 年第三季度)应该完全避免这个问题。

参见commit 33ba9c6 (29 Jun 2016) by Patrick Steinhardt (pks-t)
(由 Junio C Hamano -- gitster -- in commit 5eb1e9f 合并,2016 年 7 月 13 日)

rebase -i: restore autostash on abort

When we abort an interactive rebase we do so by calling die_abort, which cleans up after us by removing the rebase state directory.
If the user has requested to use the autostash feature, though, the state directory may also contain a reference to the autostash, which will now be deleted.

Fix the issue by trying to re-apply the autostash in die_abort.
This will also handle the case where the autostash does not apply cleanly anymore by recording it in a user-visible stash.