How do you undo a hard reset in Git Gui or Gitk on

2019-07-07 04:56发布

问题:

I'm using Git Gui and Gitk on Windows. How do I undo a hard reset from within the past two hours?

(Is it possible to do this from these applications, without using the command line?)

I saw this SO post, which says that undos are possible before git's garbage collection occurs. I might have quit and reopened one or both of these applications.

回答1:

  1. If you had changes in your working tree that were not committed when you did git reset --hard, those changes are gone for ever. You have to use your memory (in your head) to recreate them.

  2. Changes that were committed after the commit to which you switched are not lost. They likely have no reference pointing to them, making them more difficult to locate. The tool to list all low-level changes to the repo is git reflog. After you locate the commit which you want to revert to observe the hash number in the first row and use git reset --hard #hashnumber or git checkout #hashnumber to get the changes. I found this useful line on http://quirkygba.blogspot.com/2008/11/recovering-history-with-git-reflog.html:

gitk --all $(git reflog | cut -c1-7)

This will display all the hidden changes in gitk, where you can comfortably view, point, click and create new branches.

As you mentioned the unreferenced commits are normally being kept in the repository for 30 days.

EDIT: I have to add stuff here so that my edit is at least 6 characters. I know, sometimes code fixes are less than 6 characters, but there might, after all, be something else to improve in this post.



回答2:

See the answers by Brian Riehman and Pat Notz in the link in the question.

One solution is to use the command line.

In Windows, open DOS in the directory containing your .git directory.

Type something like the following to see what commit you want to go to:

"c:\Program Files\Git\bin\git.exe" reflog

To go to a certain commit, type something like the following, where the last expression is the SHA1 code of that commit:

"c:\Program Files\Git\bin\git.exe" reset --hard 5eb4080


回答3:

I don't think you can undo a hard reset to get uncommitted changes back - you can undo a rebase because the blobs are still available, but if you never committed your newest changes to Git ever, anything it overwrote is most likely history. I'd love to find out that I'm wrong though!