git提交记录被冲掉是很恼火的事情,在用source tree的时候遇到过好几次。一直是坚信git是安全的,只要是commit过的,一定在本地有记录。
被冲掉的提交记录确实没有消失,可以通过 git fsck --lost-found
找回来。只是这些提交成了悬挂提交(dangling commit),没有上下文信息。
运行git fsck命令会输出以下内容
Checking object directories: 100% (256/256), done.
Checking objects: 100% (61788/61788), done.
dangling blob df01008849bd95a9a5900301991b5df7e3fd84ac
dangling blob af0260d54f656151b68e3ecd6ac80df84a28f5b6
dangling commit 50048caf77cf5bbee1fd6ea4db3e6b6963e146c5
dangling commit ea0598d6b3f5be47e209d0a76d9871c82c97619c
dangling blob ec0a903510e4db44c83c4b4f6ebace6898913a23
dangling blob e40f48affb1512aa28b7161d987f1f4a394eb1f8
dangling commit 53161c5d8df3b89c64766ae2a7087da81e7cbfce
dangling blob a916140e3f7278873b93d07e63855dc898665298
dangling tree ae167493d8a3cb824bdeeec73a0f35b04922c223
dangling commit 17179c0b19cd1ae8aefdab99958363c2543f03fa
dangling blob 591c08d8a2ba39a331d45d940caf9aacb64c022e
dangling blob 461d38d2b75da7d068b4349cb45e0135462a2502
dangling blob f01e58ba5057ee1dfa7167e8e8f2bf1895186a16
dangling blob 261f8cbc2b9387a59b384e0290221093a2b32ff3
dangling blob e62544a823c035a76821bbdaae5e91295af16146
dangling commit fd270441ec929e4848a5a32b639f9680804c686d
dangling tree 7828f4f88bfff50d1da5aa23a77c086e9743e94b
...
可以通过 git show [commit] 查看改动,如果很幸运找到了丢失的commit,便可以通过 git merge [commit] 把这条修改合并回来。