Open In App

Recovering Lost Commits in Git

Last Updated : 17 Nov, 2025
Comments
Improve
Suggest changes
1 Likes
Like
Report

Git is a very powerful tool, and with great power comes great responsibilities. If not used and handled properly, it might cause you to lose your commits. There might be situations when you may find all of your work missing at once. If you have regularly committed your work, there is a way to recover these

lost commits

. This tutorial will aim at using the

 git reflog

and the

git cherry-pick

commands to recover your lost commits in Git.

Note: Using the reflog will only work for a certain amount of time after the commits are lost. Git cleans the reflog periodically, so don’t wait too long!

Procedure

The first step to recovering your lost commits is to recover the list of all your previous commits and actions done on the repository.

Note: Keep in mind that the given commit hashes and signatures may differ from that of your local repository. Replace all relevant information with the info corresponding to your log.

Run this command

git reflog

After running the command this is what you will see as the output.

$ git reflog
c9f9669 HEAD@{0}: commit: Fixed test cases to run on Unix
b3ca8a4 HEAD@{1}: pull: Fast-forward
54ba188 HEAD@{2}: pull origin master: Fast-forward
e659a21 HEAD@{3}: reset: moving to HEAD~1
12944d8 HEAD@{4}: reset: moving to HEAD~1
6f40152 HEAD@{5}: reset: moving to HEAD~1
3de61ba HEAD@{6}: pull: Fast-forward
e659a21 HEAD@{7}: reset: moving to HEAD^1
12944d8 HEAD@{8}: reset: moving to HEAD^1
6f40152 HEAD@{9}: reset: moving to HEAD^1
3de61ba HEAD@{10}: commit: Removed Query object
6f40152 HEAD@{11}: pull: Merge made by the 'recursive' strategy.
12944d8 HEAD@{12}: commit: API touchups --- We want to recover this commit.
e659a21 HEAD@{13}: commit: Test enhancements
07419e1 HEAD@{14}: pull: Fast-forward

Find the Hash of the commit you want to recover, For ex-12944d8. Now use the following command to bring your lost commit back.

 git cherry-pick 12944d8 

Recovered Commit

That's it! Your work should be recovered soon with the following success commands:

Finished one cherry-pick.
[master 12944d8] API touchups
3 files changed, 36 insertions(+), 3 deletions(-)

In case of a Merge conflict

If there is a merge conflict, the following message will show up.

error: could not apply 12944d8... API touchups
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'

Using the git status command can help you identify what needs to be done.

Suggested Quiz
5 Questions

Which Git command allows you to view previous HEAD movements and recover lost commits?

  • A

    git log

  • B

    git revert

  • C

    git reflog

  • D

    git reset

Explanation:

git reflog records all movements of HEAD and refs, allowing you to find commits that may not be visible in regular log and recover them.

Which command is used to restore a lost commit once its hash is identified in reflog?

  • A

    git merge <hash>

  • B

    git cherry-pick <hash>

  • C

    git fetch <hash>

  • D

    git blame <hash>

Explanation:

git cherry-pick <commit-hash> re-applies the lost commit to the current branch, recovering it.

What is an important limitation when recovering commits using reflog?

  • A

    reflog works only on remote repositories

  • B

    reflog cleans automatically after a period of time

  • C

    reflog requires staging files first

  • D

    reflog can restore only merge commits

Explanation:

Reflog expires entries over time based on cleanup rules, so recovery must be done before those entries are pruned.

If a merge conflict occurs while cherry-picking, what should you do?

  • A

    Abort cherry-pick immediately

  • B

    Use git revert to fix it

  • C

    Resolve conflicts, git add the files, then commit

  • D

    Delete the repository and clone again

Explanation:

When conflicts happen during cherry-pick, fix them, stage with git add, then commit to complete recovery.

What does the output HEAD@{0}, HEAD@{1}, HEAD@{2} in reflog represent?

  • A

    Versions of remote branches

  • B

    Number of untracked files

  • C

    The chronological history of reference changes

  • D

    Current commit size

Explanation:

HEAD@{n} shows previous states of HEAD, with {0} being the most recent and increasing numbers referring to older states.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Article Tags :

Explore