Reference logs, or "reflogs" are a mechanism Git uses to record updates applied to tips of branches and other commit references. Reflog allows you to go back to commits even though they are not referenced by any branch or tag. After rewriting history, the reflog contains information about the old state of branches and allows you to go back to that state if necessary. Every time your branch tip is updated for any reason (by switching branches, pulling in new changes, rewriting history or simply by adding new commits), a new entry will be added to the reflog. In this section we will take a high level look at the
git reflog command and explore some common uses.
This displays the reflog for the local repository.
This shows the reflog with relative date information (e.g. 2 weeks ago).
git reflog, let's run through an example.
The reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled
If it turns out that you accidentally moved back, the reflog will contain the commit main pointed to
(0254ea7) before you accidentally dropped 2 commits.
git reset --hard 0254ea7
Using Git reset, it is now possible to change main back to the commit it was before. This provides a safety net in case the history was accidentally changed.
It's important to note that the reflog only provides a safety net if changes have been committed to your local repository and that it only tracks movements of the repositories branch tip. Additionally reflog entries have an expiration date. The default expiration time for reflog entries is 90 days.
For additional information, see our git reflog page.