git reset --hard HEAD to restore the temporary area file

git reset --hard HEAD to restore the temporary area file

1. Reference materials

  1. Restore the temporary area file after git reset--hard
  2. Restore temporary area (uncommitted) files after git reset--hard
  3. The painful lesson about the command git reset --hard

2. Foreword

My TM brain was kicked by a donkey. I added some files and executed it without configuring the .gitignore file git add .. As a result , some files that shouldn't be added to the git version management were added to the tracking cache.

Originally use git rm --cached 文件通配符will be able to resolve the matter, I owe plus hand head ventilation, execute the git reset –hard HEADcommand, reset the work area and staging area, a look back yard one day own code no more. . .

Wow, the little fist hits his chest

3. Solution

find .git/objects -type f | xargs ls -lt | sed 60q command

This command can retrieve the most recent 60 git adddocuments submitted, but the document did not add too, sorry, Shiguwucun ~

image-20210205113823351

git cat-file -p ID > recover.txt command

The command to read the file ID shown out redirected to save recover.txtthe paper, that some students would be asked: the old old wet wet, how do you know when certain .txt file? Don't worry, I will talk about it later~


Submitted document ID

ID is a series of objects behind something (removed /), such as -r--r--r-- 1 Heygo 197609 1706 Feb 5 10:52 .git/objects/37/755585613dbd77013b8c86abab558097794314a file ID of37755585613dbd77013b8c86abab558097794314


Restore files

Execute git cat-file -p ID > recover.txtthe command, you can find the restored files in your home directory git repository

image-20210205114323056

Determine file type

Recover pictures or documents

Open the recover.txt file with Sublime Text software and find that it is a binary file. You can try to modify its suffix name. I originally had an .ico file here, so I changed its suffix name to .ico

image-20210205114458713

Wow, it's getting better

image-20210205114804992


Recover text files

Open the recover.txt file with Sublime Text software, and find it is a text file, you can modify the suffix according to its content, here is a batch file of .bat

image-20210205114913464


Note :

  1. Repeat the above operations to restore the recently deleted files,
  2. The .pack file is a file that comes with git and does not need to be restored. .git/objects/pack/.packAs a compressed file, the .git/objects/next file is compressed loosely, .git/objects/pack/.idxthe index compressed, easy to quickly find a file in a compressed file

image-20210205120112512

4. Summary

git reset --hardTo be used with caution, because this action clears the work area and staging area, if we just execute the git addcommand to submit files to the staging area, has not been executed git commitcommand will submit the file to the local library, then oh excluded

The code must be timely commit! The code must be timely commit! The code must be timely commit!

Guess you like

Origin blog.csdn.net/oneby1314/article/details/113706217