Last Updated: Reading Time: 1 min
If you are like me, you have probably sometimes committed a change in git but did a hard reset. Or you force pushed after a rebase and lost a commit that you needed.
Have no fear; your work is still here. Let's see how you can restore lost commits so that you don't have to do that work again.
You can use
git reflog to restore lost commits.
git reflog is a logging mechanism that keeps track of all changes made in a repository.
Below is an example of the output of git reflog
In the image above, you can see that each commit has a unique hash-id. You can either use the SHA or HEAD@
Now, depending on what you want, there are several options.
git reset --hard <SHA>: restores the project's history to that commit.
git checkout <SHA> -- <filename>: recreates one or more files in your working directory without altering the history.
git cherry-pick <SHA>: append the commit to the current working head.