1

I just switched to sublime merge away from github desktop (what an improvement), however I just performed the menu option Edit commit\drop selected commits thinking it was the equivalent of the undo commit button in github desktop.

That has not undone the commit I had selected (which was the most recent in the tree) as I thought, it has dropped (erased) it completely.

I know there are many posts discussing the reflog and of course I'll study that but would love a direct (command line if necessary) solution which fixes this exact problem first, then I can learn more later.

mwal
  • 2,803
  • 26
  • 34
  • 2
    From Sublime Merge you should be able to get your commit back by using `Repository > Undo rebase -i (finish)` from the menu (assuming you have taken no other actions), which will undo the last step of the rebase that dropped the commit. That will make the dropped commit reappear in the tree; you can then unstage all of the changes that are left or use a reset to get back to a known state. Otherwise you indeed need to use the command line to do this. – OdatNurd Aug 21 '19 at 17:20
  • Great - that worked. I did try regular system "undo" which didn't appear to do anything, but `Repository > Undo` has restored the dropped commit. ... So, now I know `Repository > Undo` is there specifically to **undo the last git command** - if I'm correct. Great to know.. thanks. – mwal Aug 21 '19 at 17:28
  • @OdatNurd When I dropped the commit, I had no current changes (I had just done a commit amend to put all current changes back into the commit which I then subsequently dropped). So, after running `Repository > Undo`, the dropped commit is back, and also, I'm left with with unstaged changes which appear to be a (perfect?) revert commit. This is interesting (can I check if my understanding is correct?) -- because that most recent commit was brought back, that made head (which we changed back in time when we dropped) now appear modified in relation to it. Is that correct? thanks! – mwal Aug 21 '19 at 17:42
  • So in `sublime-merge` terms, to complete the fix I think I just need to hit `Discard All`, on the working directory, to get rid of the remaining state which otherwise would form the perfect 'revert commit' for the dropped commit which we restored with `Repository > Undo`. – mwal Aug 21 '19 at 17:48
  • 1
    I don't think you can use any of the `Edit Commit` options with a dirty working copy, so indeed any changes you see are a result of the rebase operation that was being done. Discarding all of the changes should get you back to where you were; you can also use something like a hard reset on the commit that you tried to remove to do the same thing, I would think. – OdatNurd Aug 21 '19 at 17:52
  • So, to complete the solution -- the way to do the equivalent of github desktop's `Undo commit` button, if that is wanted, is obviously not to drop the commit, but to select the commit *before* the one you want remove, then do `Reset to this commit > Mixed`. That appears to be the equivalent. – mwal Aug 21 '19 at 18:24

1 Answers1

1

Assume the "drop last commit" actually did something like git reset --hard HEAD~, you may have amsome chance retrieving the dropped commit via ORIG_HEAD. That says, try giving this command a try and check your luck:

git log -3 ORIG_HEAD

If you can recall the commit message, applying git log on every output of git fsck --dangling will 100% find that commit out for you, provided you haven't done a git gc.

iBug
  • 35,554
  • 7
  • 89
  • 134