unstaged files gone after git reset --hard

前端 未结 7 1007
清酒与你
清酒与你 2020-12-03 14:31

I tried the git reset --hard HEAD@{n} from git reflog and I lost everything with my current unstaged files :\'(

the unstaged files is the l

相关标签:
7条回答
  • 2020-12-03 15:07

    Similar to the answers from UsamaAmjad and Juank.

    If you used a nice enough IDE/Editor:

    • Hit Ctrl+Z on each file you had modified,
    • If you are lucky (like I was) you will presented with a pop up, "Undo reload from disk"
    • Hit "yes".

    Notes:

    • This worked for my unstaged files
    • I did this in PyCharm, so it will probably work in other JetBrains/Idea-based IDEs
    • At the time of git reset, the IDE probably needed to be open
    • At the time of git reset, the files probably needed to be open in an editor window
    0 讨论(0)
  • 2020-12-03 15:11

    It's not clear if you lost files in your working directory, or files in the index. You say you lost your "unstaged files", but then you mention you might have run "git add". "unstaged files" are lost for good.

    Staged files can be recovered with

    git fsck --full --unreachable --no-reflog
    

    For each file added there will be a lost blob object, and for each directory entry there will be a tree object. You would recover your file changes by doing

    git cat-file -p SHA
    

    For each file that you had modified

    (master)$ vi bar (master)$ vi baz (master)$ vi foo (master)$ git add foo bar baz (master)$ git reset --hard HEAD HEAD is now at ead8fa2 initial (master)$ git fsck --full --unreachable --no-reflog Checking object directories: 100% (256/256), done. unreachable blob 0c29287001b29159f11c4e8a320bce7e9789c00b unreachable blob 1524d3478e3d0b92866a53239b10bcd4b3838c4d unreachable blob 97b724e770249816c61d8a526415986208ed7e15 // take a look at one of the objects (master)git cat-file -p 0c29287001b29159f11c4e8a320bce7e9789c00b changes for bar //Here, based on inspecting the output, I can determine that 0c29287 was the file "bar" (master) git cat-file -p 0c29287 > bar

    (note I didn't get any lost trees when I tested, so this part may not work)

    If you modified a whole bunch of files it is probably easier to recover via the tree object instead of individual files

    git read-tree SHA
    

    Where SHA is the lost tree object for the root tree.

    0 讨论(0)
  • 2020-12-03 15:20

    If anyone made the same blunder like me git reset --hard before adding unstaged files then there are still chances to get those changes back. Although these files are not available in repo anymore but some of the new IDEs maintain their own history. Like in my case I was able to retrieve my unstaged changes from Android Studio's local history feature which is located under VCS.

    Directions:

    0 讨论(0)
  • 2020-12-03 15:20

    If you have staged the files with git add, it can still be found back. But if the files are not staged, I have to say there's no way. :(

    Just remember that git reset is really not safe, especially for the unstaged files.

    But if the files are really really important, what I can think of is that you may stop modifying any data in your disk, and try disk recovery with the tools such as finaldata. It might find something back if lucky, because you have already overwritten some files after git reset.

    Anyway, Git is really a powerful and cool tool if you are familiar with it.

    0 讨论(0)
  • 2020-12-03 15:23

    All unstaged/uncommited files will be deleted with git reset --hard

    Using --hard is not recomended since that option removes ALL unstaged/uncommited files, instead you should stash first and then use normal reset

    Instead of

    git reset --hard HEAD@{n}
    

    You should do

    git stash
    git reset HEAD@{n}
    

    Your code is then saved in the stash stack, you can retrieve it again by doing

    git stash pop
    

    Although this command merge the "stashed" changes with their current HEAD (an stash is implemented like a branch) is recommended to do stash retrievals on the same commits where these stashes was generated

    0 讨论(0)
  • 2020-12-03 15:24

    if anyone searching for a solution using Pycharm IDE,

    • find folder for deleted file,
    • with right click select , "Show History",
    • In left panel you will see history of changes in that folder.

    Then find file you deleted, right click to it and select "revert", then deleted files will appear in folder.

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