git reset --hard HEAD to restore the temporary area file
1. Reference materials
- Restore the temporary area file after git reset--hard
- Restore temporary area (uncommitted) files after git reset--hard
- 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 HEAD
command, 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 add
documents submitted, but the document did not add too, sorry, Shiguwucun ~
git cat-file -p ID > recover.txt
command
The command to read the file ID shown out redirected to save recover.txt
the 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/755585613dbd77013b8c86abab558097794314
a file ID of37755585613dbd77013b8c86abab558097794314
Restore files
Execute git cat-file -p ID > recover.txt
the command, you can find the restored files in your home directory git repository
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
Wow, it's getting better
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
Note :
- Repeat the above operations to restore the recently deleted files,
- The .pack file is a file that comes with git and does not need to be restored.
.git/objects/pack/.pack
As a compressed file, the.git/objects/
next file is compressed loosely,.git/objects/pack/.idx
the index compressed, easy to quickly find a file in a compressed file
4. Summary
git reset --hard
To be used with caution, because this action clears the work area and staging area, if we just execute the git add
command to submit files to the staging area, has not been executed git commit
command 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
!