6

When using git rebase --autostash, git automatically creates an "autostash" commit, and will re-apply it after a successful rebase.

But in case the rebasing is aborted (e.g. by using :cq in Vim when it's an interactive rebase), the autostash-commit might end up as a dangling commit.

Git 2.9.0

4
  • If you interrupt an interactive rebase, the interactive rebase should still be in progress, and git rebase --continue will resume it, while git rebase --abort should terminate and re-apply the autostash. If it does not re-apply the autostash, that's a bug (I'm pretty sure there was such a bug for a while, so your Q+A is actually useful, but if you have the bug, you should upgrade your Git).
    – torek
    Jun 15, 2016 at 23:46
  • After all it's probably a but with git in the first place - it should not leave this autostash behind: thread.gmane.org/gmane.comp.version-control.git/297404.
    – blueyed
    Jun 15, 2016 at 23:50
  • @torek the rebase is not in progress afterwards anymore, so it cannot be aborted or resumed. git rebase --continue says No rebase in progress?.
    – blueyed
    Jun 15, 2016 at 23:58
  • 1
    Ah, definitely a bug then. I take it you're making the sequence editor fail, during the rebase startup. Aha, yes, that's in your bug report. You can simplify this by replacing your steps 2-and-3 with just GIT_SEQUENCE_EDITOR=false git rebase -i --autostash.
    – torek
    Jun 16, 2016 at 0:36

2 Answers 2

22

I have found the following to list all "autostash" commits:

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

You can then use the commit hash to get the commit back, e.g. using git show or git cherry-pick.

The output looks like this:

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
1
  • 2
    Rebase stuffs the hash of the autostash into a "state directory" in .git ($state_dir in the rebase script; see $(git --exec-path)/git-rebase, apply_autostash function, around line 160). Assuming you're dealing with the Git bug and this state dir is gone: Once you have the ID, use git stash store to turn it into a regular stash (see same code, around line 167).
    – torek
    Jun 15, 2016 at 23:52
4

Git 2.10 (Q3 2016) should avoid that issue entirely.

See commit 33ba9c6 (29 Jun 2016) by Patrick Steinhardt (pks-t).
(Merged by Junio C Hamano -- gitster -- in commit 5eb1e9f, 13 Jul 2016)

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.