Skip to content

Recovering a lost commit

Abhik B Pramanik edited this page Aug 11, 2011 · 1 revision

Git keeps a log of every commit you've made in a datastore. This datastore only gets purged if you every run git gc (which is never). So if you ever lose a commit by accident, its pretty easy to find it and put it back in the branch.

  1. Let's make a commit and purposefully lose it:

    % echo "Losing this" >> README
    % git add README
    ~/workspace/git-test
    % git status
    # On branch master
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #   modified:   README
    #
    ~/workspace/git-test
    % git commit -m "Lost commit"
    [master 1a3dc8f] Lost commit
    1 files changed, 1 insertions(+), 0 deletions(-)
    % git log
    commit 1a3dc8fa315dd08ae7ea6e6620179d9481c06b3f
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Thu Aug 11 14:45:09 2011 -0700
    
        Lost commit
    
    commit 288741dc5ef74ba119eb75daa962da29b0c686a8
    Author: Abhik Pramanik <abhik@alum.berkeley.edu>
    Date:   Tue Aug 2 21:50:56 2011 -0700
    
        Adding clean instructions to INSTALL
    % git reset 288741dc5ef74ba119eb75daa962da29b0c686a8
    % git checkout .
    % git log
    commit 288741dc5ef74ba119eb75daa962da29b0c686a8
    Author: Abhik Pramanik <abhik@alum.berkeley.edu>
    Date:   Tue Aug 2 21:50:56 2011 -0700
    
        Adding clean instructions to INSTALL
    
  2. For all intents and purposes this commit has been lost from the branch, but we can find it again via a few methods. I like to use git reflog since it prints the commit message. git reflog shows tip of branches are updated.

    % git reflog
    288741d HEAD@{0}: 288741dc5ef74ba119eb75daa962da29b0c686a8: updating HEAD
    1a3dc8f HEAD@{1}: commit: Lost commit
    ...
    

If you lose a commit through a rebase then it may not be in the reflog, and you'll need to use git fsck --lost-found. Unfortunately, it doesn't describe the commits very well.

```
% git fsck --lost-found          
dangling commit 86cc1c2fc46f632a0ee4b4b0b97adf7f7f4d7c72
dangling blob 18c41127c42cc33e3d80914063a7483653fc65b3
dangling commit 1a3dc8fa315dd08ae7ea6e6620179d9481c06b3f
...
```
  1. Now that you have a list of commits you can view their details via git show <commit sha1>. You only have to give enough characters to uniquely identify the sha1:

    % git show 1a3dc8f               
    commit 1a3dc8fa315dd08ae7ea6e6620179d9481c06b3f
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Thu Aug 11 14:45:09 2011 -0700
    
        Lost commit
    
    diff --git a/README b/README
    index eae4da1..c3e0df9 100644
    --- a/README
    +++ b/README
    @@ -1 +1,2 @@
     Hey this a test for git. Welcome, to git.
    +Losing this
    
  2. Once the right commmit is found you can put it at the beginning via git cherry-pick <commit sha1>.

    % git cherry-pick 1a3dc8fa315dd08ae7ea6e6620179d9481c06b3f
    [master 183f0cb] Lost commit
    1 files changed, 1 insertions(+), 0 deletions(-)
    % git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    % git log
    commit 183f0cba98a6ad3f3f94894200ddc86d7665e09a
    Author: Abhik B Pramanik <abhik@alum.berkeley.edu>
    Date:   Thu Aug 11 14:45:09 2011 -0700
    
        Lost commit
    
    commit 288741dc5ef74ba119eb75daa962da29b0c686a8
    Author: Abhik Pramanik <abhik@alum.berkeley.edu>
    Date:   Tue Aug 2 21:50:56 2011 -0700
    
        Adding clean instructions to INSTALL
    

Clone this wiki locally