Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

add - unstaged files gone after git reset --hard

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 last git add I did, before then I tried git reset to the last git commit.

And all my files gone, I can't go back to the git add before last commit :'(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...