本文转载自:
http://gitbook.liuhui998.com/5_9.html
译者注: 原书这里只有两个链接: Recovering Lost Commits Blog Post,
Recovering Corrupted Blobs by Linus
我根据第一个链接,整理了一篇博文,并把它做为原书补充。
一、前言
在玩git的过程中,常有失误的时候,有时把需要的东东给删了。 不过没有关系,git给了我们一层安全网,让们能有机会把失去的东东给找回来。
二、准备
我们先创建一个用以实验的仓库,在里面创建了若干个提交和分支。 BTW:你可以直接把下面的命令复制到shell里执行。
mkdir recovery ; cd recoverygit inittouch filegit add filegit commit - m "First commit"echo "Hello World" > filegit add .git commit - m "Greetings"git branch cool_branchgit checkout cool_branchecho "What up world?" > cool_filegit add .git commit - m "Now that was cool"git checkout masterecho "What does that mean?" >> file
三、恢复已删除分支提交
现在repo里有两个branch
$ git branchcool_branch* master
存储当前仓库未提交的改动
$ git stash save "temp save"Saved working directory and index state On master : temp saveHEAD is now at e3c9b6b Greetings
删除一个分支
$ git branch - D cool_branchDeleted branch cool_branch ( was 2e43cd5 ).$ git branch* master
用
git fsck --lost-found
命令找出刚才删除的分支里面的提交对象。
$git fsck -- lost - founddangling commit 2e43cd56ee4fb08664cd843cd32836b54fbf594a
用
git show
命令查看一个找到的对象的内容,看是否为我们所找的。
git show 2e43cd56ee4fb08664cd843cd32836b54fbf594acommit 2e43cd56ee4fb08664cd843cd32836b54fbf594aAuthor : liuhui < liuhui998 [ #]gmail.com>Date: Sat Oct 23 12:53:50 2010 +0800Now that was cooldiff -- git a / cool_file b / cool_filenew file mode 100644index 0000000. . 79c2b89--- / dev / null+++ b / cool_file@@ - 0 , 0 + 1 @@+ What up world ?
这个提交对象确实是我们在前面删除的分支的内容;下面我们将考虑一下要如何来恢复它了。
使用
git rebase
命令来恢复
$git rebase 2e43cd56ee4fb08664cd843cd32836b54fbf594aFirst , rewinding head to replay your work on top of it ...Fast - forwarded master to 2e43cd56ee4fb08664cd843cd32836b54fbf594a .
现在我们用git log命令看一下,看看它有没有恢复:
$ git logcommit 2e43cd56ee4fb08664cd843cd32836b54fbf594aAuthor : liuhui < liuhui998 [ #]gmail.com>Date: Sat Oct 23 12:53:50 2010 +0800Now that was coolcommit e3c9b6b967e6e8c762b500202b146f514af2cb05Author : liuhui < liuhui998 [ #]gmail.com>Date: Sat Oct 23 12:53:50 2010 +0800Greetingscommit 5e90516a4a369be01b54323eb8b2660545051764Author : liuhui < liuhui998 [ #]gmail.com>Date: Sat Oct 23 12:53:50 2010 +0800First commit
提交是找回来,但是分支没有办法找回来:
liuhui@liuhui :~/ work / test / git / recovery$ git branch* master
我们也可以使用
以下
命令来把刚才的恢复的提交删除
$ git reset -- hard HEAD ^HEAD is now at e3c9b6b Greetings
再把刚删的提交给找回来:
git fsck -- lost - founddangling commit 2e43cd56ee4fb08664cd843cd32836b54fbf594a
不过这回我们用是合并命令
git merge
来进行恢复:
$ git merge 2e43cd56ee4fb08664cd843cd32836b54fbf594aUpdating e3c9b6b .. 2e43cd5Fast - forwardcool_file | 1 +1 files changed , 1 insertions (+), 0 deletions (-)create mode 100644 cool_file
六、git stash的恢复
前面我们用
git stash
把没有提交的内容进行了存储,如果这个存储不小心删了怎么办呢?
假如当前repo里有的存储:
$ git stash liststash@ { 0 }: On master : temp save
把它们清空:
$git stash clearliuhui@liuhui :~/ work / test / git / recovery$ git stash list
再用
git fsck --lost-found
找回来:
$git fsck -- lost - founddangling commit 674c0618ca7d0c251902f0953987ff71860cb067
用git show看一下回来的内容对不对:
$git show 674c0618ca7d0c251902f0953987ff71860cb067commit 674c0618ca7d0c251902f0953987ff71860cb067Merge : e3c9b6b 2b2b41eAuthor : liuhui < liuhui998 [ #]gmail.com>Date: Sat Oct 23 13:44:49 2010 +0800On master : temp savediff -- cc fileindex 557db03 , 557db03. . f2a8bf3--- a / file+++ b / file@@@ - 1 , 1 - 1 , 1 + 1 , 2 @@@Hello World++ What does that mean ?
看起来没有问题,好的,那么我就把它恢复了吧:
$ git merge 674c0618ca7d0c251902f0953987ff71860cb067
Merge made by recursive.
file | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
七、其他
其实git中找回丢失的对象,这里最重要的一个命令就是:
git fsck --lost-found
,因为git中把
commit
删了后,并不是真正的删除,而是变成了悬空对象(dangling commit)。我们只要把把这悬空对象(dangling commit)找出来,用
git rebase
也好,用
git merge
也行就能把它们给恢复。