Git reflog
This page privides a detailed discussion of the git reflog command. Git keeps track of updates to the tip of branches using a mechanism call reference logs, or "reflogs". Many git commands accept a parameter for specifying a reference or "ref, which is a pointer to a commit.Common examples include :
Git checkout
Git reset
git Merge
Reflogs track when git refs were updated in the local repo. In addition to branch tip reflogs, a special reflog is maintained for the git statsh .Reflogs are stored in directories under the local repository's .git directory. git reflog directories can be found at .git/logs/refs/heads/., .git/logs/HEAD, and also .git/logs/refs/stash if the git stash has been used on the repo.
We discussed git reflog at a high level on the Rewriting History Page. This document will cover: extended configuration options of git reflog, common use-cases and pitfalls of git reflog, how to undo changes with git reflog, and more.
Basic usage
The most basic Reflog use case is invoking:
git reflog
This is essentially a short cut that's equivalent to:
git reflog show HEAD
This will output the HEAD reflog. You should see output similar to:
eff544f HEAD@{0}: commit: migrate existing content
bf871fd HEAD@{1}: commit: Add Git Reflog outline
9a4491f HEAD@{2}: checkout: moving from main to git_reflog
9a4491f HEAD@{3}: checkout: moving from Git_Config to main
39b159a HEAD@{4}: commit: expand on git context
9b3aa71 HEAD@{5}: commit: more color clarification
f34388b HEAD@{6}: commit: expand on color support
9962aed HEAD@{7}: commit: a git editor -> the Git editor
Reflog references
By default, git reflog will output the reflog of the HEAD ref. HEAD is a symbolic reference to the currently active branch. Reflogs are available for other refs as well. The syntax to access a git ref is name@{qualifier}. In addition to HEAD refs, other branches, tags, remotes, and the Git stash can be referenced as well.
You can get a complete reflog of all refs by executing: git reflog show --all
To see the reflog for a specific branch pass that branch name to git reflog show
git reflog show otherbranch 9a4491f otherbranch@{0}: commit: seperate articles into branch PRs 35aee4a otherbranch{1}: commit (initial): initial commit add git-init and setting-up-a-repo docs
Executing this example will show a reflog for the otherbranch branch. The following example assumes you have previously stashed some changes using the git stash command. git reflog stash 0d44de3 stash@{0}: WIP on git_reflog: c492574 flesh out intro
This will output a reflog for the Git stash. The returned ref pointers can be passed to other Git commands: git diff stash@{0} otherbranch@{0}
When executed, this example code will display Git diff output comparing the stash@{0} changes against the otherbranch@{0} ref.
Timed reflogs
Every reflog entry has a timestamp attached to it. These timestamps can be leveraged as the qualifier token of Git ref pointer syntax. This enables filtering Git reflogs by time. The following are some examples of available time qualifiers:
1.minute.ago
1.hour.ago
1.day.ago
yesterday
1.week.ago
1.month.ago
1.year.ago
2011-05-17.09:00:00
Time qualifiers can be combined (e.g. 1.day.2.hours.ago), Additionally plural forms are accepted (e.g. 5.minutes.ago).
Time qualifier refs can be passed to other git commands.
git diff main@{0} main@{1.day.ago}
This example will diff the current main branch against main 1 day ago. This example is very useful if you want to know changes that have occurred within a time frame.
git reflog accepts few addition arguments which are considered subcommands
Executing this reset command will move head to the commit where "some WIP changes" was added.essentially restoring the other squashed commits
Summary Git Reflog
How to view reflog for specific branches
How to undo git rebase using the reflog
How specify and view time based reflog entries
We briefly mentioned that git reflog can be used with other git commands like git checkout, git reset, and git merge. Learn more at their respective pages.
Last updated