Recover files in git after reset --hard on added files(not committed!)

折月煮酒 提交于 2021-02-11 18:19:18

问题


I have few files which are never committed. I have added those files and by mistake i have executed "git reset --hard"

i want to recover those files.

please help me anyone.


回答1:


This is not possible. These files are lost.




回答2:


You can still save them if you've done nothing since. The git garbage collector is not run immediately. Try following this.




回答3:


I have found the answer from stackoverflow only.

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

( :) this answer has got second last votes from all the answers mentioned in that page... but i must say.. this is the only answer which saves me.... )

I did this:

Step 1:

manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (94312/94312), done.
dangling blob 34122c30800b0fd91bb8418efbbb230e75fe3dbe
dangling blob 0e25ccea407f3c19abc1433e5732a5f2a20191f6
dangling blob 90afe07d111640d86f02e09e4939d02bbf3f5d56
dangling blob e8b4dcb611257ddb13c29c09d39cc660a6bace02
dangling blob 3ad660983baa3608cd333daa8c6d9d47420d5e0b
dangling commit 8932f1f0c8df1917f38a6847a1f21fa54c6d6355
dangling blob 86548593d0f3337ecb7bc5d665a9f48148d9d157
dangling commit 6cf531cad6822132fd183e7e336a13f38d8fd8bd
dangling commit 0c025ebf3fe8d1632ec11b4734fb504deb06aa0a
dangling blob 6260329668086042177aab9f9a1e2a3720204bd8
dangling blob 887732bd14017d1d4558160e12a3666b131a9604
dangling blob f08e12e5724f7976f1596f963ac60e2ebd11b44c
dangling blob 079077a183e8450ab35670b92fd2ce6a8a4b7a9a
dangling commit 6dd8f3cddfc45340c6d0e0dc5fbaa2bd39b11fdb
dangling blob 1ee3ffc72b23bfcfc08dcc0fee8eac10cd4ea6e2

Step 2:

git show 34122c30800b0fd91bb8418efbbb230e75fe3dbe

this will show commit/changes, have made in that file. (like normal 'git show commit_no' works)

But this will show first few lines only on terminal...

Once we have executed "git reset--hard" then there is no way to restore automatically this file...

so i did trick

Step 3:

manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 1ee3ffc72b23bfcfc08dcc0fee8eac10cd4ea6e2 > /home/manan/Desktop/sample.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 079077a183e8450ab35670b92fd2ce6a8a4b7a9a > /home/manan/Desktop/sample1.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show f08e12e5724f7976f1596f963ac60e2ebd11b44c > /home/manan/Desktop/sample2.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 887732bd14017d1d4558160e12a3666b131a9604 > /home/manan/Desktop/sample3.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 6260329668086042177aab9f9a1e2a3720204bd8 > /home/manan/Desktop/sample4.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 0c025ebf3fe8d1632ec11b4734fb504deb06aa0a > /home/manan/Desktop/sample5.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 6cf531cad6822132fd183e7e336a13f38d8fd8bd > /home/manan/Desktop/sample6.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 86548593d0f3337ecb7bc5d665a9f48148d9d157 > /home/manan/Desktop/sample7.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 8932f1f0c8df1917f38a6847a1f21fa54c6d6355 > /home/manan/Desktop/sample8.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 3ad660983baa3608cd333daa8c6d9d47420d5e0b > /home/manan/Desktop/sample9.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show e8b4dcb611257ddb13c29c09d39cc660a6bace02 > /home/manan/Desktop/sample10.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 90afe07d111640d86f02e09e4939d02bbf3f5d56 > /home/manan/Desktop/sample11.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 0e25ccea407f3c19abc1433e5732a5f2a20191f6 > /home/manan/Desktop/sample12.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 34122c30800b0fd91bb8418efbbb230e75fe3dbe > /home/manan/Desktop/sample13.txt
manan@manan-pc:~/new_cms/abc/.git$ GIT_DIR! $ git show 6dd8f3cddfc45340c6d0e0dc5fbaa2bd39b11fdb > /home/manan/Desktop/sample14.txt

(thanks to my college prof ... who intelligently taught us how to use input/output redirect for *NIX operating system.)

Step 4: Replace these files with original files.

In this way, you can restore previous changes 'manually' after 'git reset --hard' ....

Hope, you will never get in such kind of situations...

:)



来源:https://stackoverflow.com/questions/22303293/recover-files-in-git-after-reset-hard-on-added-filesnot-committed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!