我如何恢复错过的提交?

时间:2015-06-03 16:25:35

标签: git

前段时间我在更改远程存储库URL后没有检查其中一个本地git客户端的配置,然后在更改它并继续开发之前,我将一些本地更改推送到远程git存储库。

现在我还没有在实际存储库中提交一些提交,但我在本地有实际文件。

如何恢复错过的提交?

$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (7726/7726), done.
dangling blob 4b811ed6c4c512e400399736869bccf0110b28c9
dangling tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
dangling blob 5f93f15a1d2dbf9fe65429e9124987208cdbe715
dangling commit 9c1434d01260873de6122f5fbd428c5eaec75761
dangling blob e6a2e5c880c410e98fc5803a77c6f16b6ffc3291
dangling tree c3b8e7abfe8455ed916113703890c1e355a123f5
dangling blob 794c7b37a217aa9b32f4f461a7ebc495d8c562f6
dangling tree 4b5224923fa2349a72372fd434e4fdf476680e0e
dangling blob bb5661ad15eebff39ddf7f462133a2e3c7492e48
dangling blob ddd70eadd066bcd63070545cbd2bb46d9e3735f5
dangling blob 2ed95453e41fc338db1db42b81fd19ad8e679ea4
dangling blob a95e284d68c4f01562b6ca9838076400500fd9fa
dangling tree f15f904c1cbdb0e7438466b29a4594aed23fe650
dangling blob 8c7b2a910b75e0fec826a8ce51c692a6be9c661f

git与上面的提交合并后我有:

# On branch recover-prod
# Your branch and 'origin/recover-prod' have diverged,
# and have 12 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean

1 个答案:

答案 0 :(得分:2)

您可以使用git merge将已删除的提交还原到您的工作分支。来自问题的git fsck的输出显示一个提交,其SHA-1哈希值为9c1434d。要将此提交合并到您的工作分支,请键入以下内容:

git checkout yourbranch
git merge 9c1434d

请记住,执行此操作时可能会遇到合并冲突,并且您必须为每次恢复的提交逐个解决它们。