如题,git stash了代码,点了pop stash后,revert了改动。是可以找回的。
操作步骤:
使用 git stash pop 其实并没有真正地将文件删掉的,而是删除引用而已,因此我们可以使用 git fsck 命令进行找回,搭配参数--unreachable,让 git-fsck 显示出所有不可访问的对象。
先拿到所有SHA -1编号:如下:
bob.wang@CNDLBobWangVM MINGW64 ~/source/repos/stms20231220/stms (student-tp-20230619-bella)
$ git fsck --unreachable
Checking object directories: 100% (256/256), done.
Checking objects: 100% (1249704/1249704), done.
Checking connectivity: 1241815, done.
unreachable tree 85018045c674dac594d293486bcf98b4b3745610
unreachable tree ab0d00431965d55bd8f0448e4f01c9e2b6cd33af
unreachable tree f21b4065da6043c11b87402acf031130c94285ca
unreachable tree 821dc066016290ff12141bc2eb973fc7458ebab0
unreachable blob 3420c03a276dd387d800f7f9ab430c71240ca746
unreachable blob 702c009f3172a4ceefe94d3de233d6f4a8f08dbe
unreachable tree 1e2d80cf1263701ba802970d1917582503cbb9da
unreachable tree 1c3040b5fca5a553f0ccfc9cbc8500589185c9b8
unreachable tree c831404b50695b002b3497effb263db544cded62
unreachable tree 433b801d7a49fdb00803365466129e66d5116d7c
unreachable tree 6b3e80fdf3f75b0533cd6ae7e1dbddf78f0ed64c
unreachable tree 69424079d5c380075b590ad821fb0d7c40941e62
unreachable tree 1c4ac0c57b6c83f2153a9d816d5903c9e859d4c2
unreachable tree 2d4a40a973075a5f84d8eaf3c4550451c0c2bd84
...
第二步:筛选出所有带有blob字样的行,并编辑成批量的命令行如下:
(此处可以用excel+notepad++筛选替换等)
$ git show 85018045c674dac594d293486bcf98b4b3745610 > bob7.txt
git show 3420c03a276dd387d800f7f9ab430c71240ca746 > bob8.txt
git show 702c009f3172a4ceefe94d3de233d6f4a8f08dbe > bob9.txt
git show a5a300771e063daf7f51b557efc1e051dacab8b8 > bob10.txt
git show 160281e68a49317eafa0c35fa435e016201d08c4 > bob11.txt
git show 634181fc444163f19824792a2fc5c2c89ca79d81 > bob12.txt
git show d25d019101dfeeeeac50a98825e954942aa2d81d > bob13.txt
git show 6ebb0124e649e0a993a98f309bafbde8f2fb5ba6 > bob14.txt
git show 5cc1c165f876d314cdd4711697f0e88739721779 > bob15.txt
git show 25ed0117f1a605336ca73aef3d9413228e95f9ea > bob16.txt
git show cd67024d885e8ce8d99378d6ce0448717b0421a8 > bob17.txt
git show 029bc20baf0c422c6a83442dff4f769b74569880 > bob18.txt
git show 437643f8bbfcd9cc837fedfbcc19379d83707955 > bob19.txt
git show df76830d069485be5a40b553cd73eaf25cc19b5f > bob20.txt
git show 232c041955c37f51bd2def7dab16d11ca2853697 > bob21.txt
git show ca99c4f0d03c072b77356f69f5e803d278d0eebd > bob22.txt
git show e8d58421171876a204c59109ad37c9ca9371e6b9 > bob23.txt
git show c60dc55bd9b710e8ddfbb850faee81475a68fbeb > bob24.txt
git show 9e1cc534ab91ce965d0ee16340dae141c580949f > bob25.txt
git show 9ef4c5dbdb90d9f8879bf58e0940f7050b720713 > bob26.txt
git show 9907c7a55df930406624ed85a604e2d99e5dcba9 > bob27.txt
git show b002c81e1f18f33a186606d09a8a82a35e26bce4 > bob28.txt
git show 5a5ec8e935804bd5103101d54185270ae0abbb10 > bob29.txt
第三步:在git bash下执行如上命令,会得到所有stash过的文件。会以单的文件的存储方式输出到git bash here的目录下。
第四步:用vs code打开文件夹并搜索代码中关键字,定位到丢失的文件。代码就找回来了。
参考链接:记一次误删 git stash 文件恢复操作_git stash后怎么恢复-CSDN博客
仅供学习参考,如有侵权联系我删除。