Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to Redo commits removed by GitHub Desktop's Undo on a Mac?

The "GitHub Desktop" Mac app has an "Undo" button in the commit area of the main view.

GitHub Desktop for Mac Screenshot

Clicking it appears to remove the most recent commit from the repository without asking for confirmation. While local changes are still available, undoing past anything that's not currently in local appears to completely erase it from the repository history. Here's the log of a demonstration repository. After a few commits and before pressing "Undo", it looks like this:

  • f6e4436 (master) Initial commit.
  • a3857ad Added example file.
  • cf17c3b Added critical content.
  • 35f1c76 (HEAD -> dev) Accidentally delete critical line.

The critical line is no longer in local or the last commit at this point. It is still available via the "cf17c3b" hash.

Clicking undo once produces this:

  • f6e4436 (master) Initial commit.
  • a3857ad Added example file.
  • cf17c3b (HEAD -> dev) Added critical content.

After two:

  • f6e4436 (master) Initial commit.
  • a3857ad (HEAD -> dev) Added example file.

And finally a third press of the "Undo" button brings the state of the repo back to this:

  • f6e4436 (HEAD -> dev, master) Initial commit.

After the last two "Undo" clicks, the "cf17c3b" commit is no longer accessible (i.e. the critical line I accidentally erased seems to have been completely removed).

I'm not seeing anything in the app to "Redo" the changes done by "Undo". That's very surprising since the button doesn't require a confirmation that it's potentially going to permanently delete code. My questions are:

  1. Is it possible to "Redo" from inside the app to restore the commits removed by "Undo"?

  2. Is it possible via the command line if it's not possible via the app?

like image 899
Alan W. Smith Avatar asked Dec 25 '22 09:12

Alan W. Smith


1 Answers

I don't know about via the app, but using git reflog you can find the history of where HEAD has been, then merge the current branch with that commit to recover it (and all previous commits). My guess is that the app uses git reset to undo the commit, so your reflog might look like this:

0ba8580 HEAD@{0}: reset: moving to head~
1451518 HEAD@{1}: reset: moving to head~
79b5909 HEAD@{2}: reset: moving to head~
a28c206 HEAD@{3}: checkout: moving from 4de17de8c1a89f17db894150144e58f8b0b50f6c to master
4de17de HEAD@{4}: checkout: moving from master to head~4
a28c206 HEAD@{5}: checkout: moving from 5005e7d017b15cf6d725ca01ac586e566338fa86 to master

The most recent action is first on the list. What you want to do is merge with the line after the reset you want to undo. So for example in this case, you could try git merge HEAD@{3}. If you want to examine the commit before merging with it, do git show HEAD@{3}.

like image 200
David Deutsch Avatar answered Jan 17 '23 18:01

David Deutsch