How can I undo git reset --hard HEAD~1?

后端 未结 18 3101
逝去的感伤
逝去的感伤 2020-11-22 00:21

Is it possible to undo the changes caused by the following command? If so, how?

git reset --hard HEAD~1
相关标签:
18条回答
  • 2020-11-22 00:36

    I've just did a hard reset on wrong project. What saved my life was Eclipse's local history. IntelliJ Idea is said to have one, too, and so may your editor, it's worth checking:

    1. Eclipse help topic on Local History
    2. http://wiki.eclipse.org/FAQ_Where_is_the_workspace_local_history_stored%3F
    0 讨论(0)
  • 2020-11-22 00:38

    Made a tiny script to make it slightly easier to find the commit one is looking for:

    git fsck --lost-found | grep commit | cut -d ' ' -f 3 | xargs -i git show \{\} | egrep '^commit |Date:'

    Yes, it can be made considerably prettier with awk or something like it, but it's simple and I just needed it. Might save someone else 30 seconds.

    0 讨论(0)
  • 2020-11-22 00:40

    What you want to do is to specify the sha1 of the commit you want to restore to. You can get the sha1 by examining the reflog (git reflog) and then doing

    git reset --hard <sha1 of desired commit>

    But don't wait too long... after a few weeks git will eventually see that commit as unreferenced and delete all the blobs.

    0 讨论(0)
  • 2020-11-22 00:41

    If you are using a JetBrains IDE (anything IntelliJ based), you can recover even your uncommited changes via their "Local History" feature.

    Right-click on your top-level directory in your file tree, find "Local History" in the context menu, and choose "Show History". This will open up a view where your recent edits can be found, and once you have found the revision you want to go back to, right click on it and click "Revert".

    0 讨论(0)
  • 2020-11-22 00:42

    git reflog and back to the last HEAD 6a56624 (HEAD -> master) HEAD@{0}: reset: moving to HEAD~3 1a9bf73 HEAD@{1}: commit: add changes in model generate binary

    0 讨论(0)
  • 2020-11-22 00:43

    In most cases, yes.

    Depending on the state your repository was in when you ran the command, the effects of git reset --hard can range from trivial to undo, to basically impossible.

    Below I have listed a range of different possible scenarios, and how you might recover from them.

    All my changes were committed, but now the commits are gone!

    This situation usually occurs when you run git reset with an argument, as in git reset --hard HEAD~. Don't worry, this is easy to recover from!

    If you just ran git reset and haven't done anything else since, you can get back to where you were with this one-liner:

    git reset --hard @{1}
    

    This resets your current branch whatever state it was in before the last time it was modified (in your case, the most recent modification to the branch would be the hard reset you are trying to undo).

    If, however, you have made other modifications to your branch since the reset, the one-liner above won't work. Instead, you should run git reflog <branchname> to see a list of all recent changes made to your branch (including resets). That list will look something like this:

    7c169bd master@{0}: reset: moving to HEAD~
    3ae5027 master@{1}: commit: Changed file2
    7c169bd master@{2}: commit: Some change
    5eb37ca master@{3}: commit (initial): Initial commit
    

    Find the operation in this list that you want to "undo". In the example above, it would be the first line, the one that says "reset: moving to HEAD~". Then copy the representation of the commit before (below) that operation. In our case, that would be master@{1} (or 3ae5027, they both represent the same commit), and run git reset --hard <commit> to reset your current branch back to that commit.

    I staged my changes with git add, but never committed. Now my changes are gone!

    This is a bit trickier to recover from. git does have copies of the files you added, but since these copies were never tied to any particular commit you can't restore the changes all at once. Instead, you have to locate the individual files in git's database and restore them manually. You can do this using git fsck.

    For details on this, see Undo git reset --hard with uncommitted files in the staging area.

    I had changes to files in my working directory that I never staged with git add, and never committed. Now my changes are gone!

    Uh oh. I hate to tell you this, but you're probably out of luck. git doesn't store changes that you don't add or commit to it, and according to the documentation for git reset:

    --hard

    Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.

    It's possible that you might be able to recover your changes with some sort of disk recovery utility or a professional data recovery service, but at this point that's probably more trouble than it's worth.

    0 讨论(0)
提交回复
热议问题