To use Git reflog to recover lost commits or branch history, follow these steps:
Start by opening your Git command line or terminal.
Use the git reflog
command. This will display a list of recent actions and reference changes in your Git repository.
Identify the commit or branch that you want to recover. Each entry in the reflog is labeled with a unique hash, along with information about the action that was performed, such as the checkout, commit, or branch creation.
Once you have identified the commit or branch, copy its hash.
Create a new branch at the desired commit by using the git branch <branch-name> <commit-hash>
command. Replace <branch-name>
with the name you want for your new branch and <commit-hash>
with the hash of the commit you want to recover. This will create a new branch pointing to the desired commit, enabling you to access the lost history.
Switch to the newly created branch using the git checkout <branch-name>
command. Replace <branch-name>
with the name of the branch you created in the previous step.
At this point, you should have successfully recovered the lost commits or branch history.
Remember that Git reflog only keeps a limited history of actions, usually around 90 days by default. So it's important to act promptly if you suspect any loss of commits or branch history.