What is git reflog

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the git category.

Last Updated: 2021-05-15

Git reflog

This isn't simply a list of lost commits, like I had once thought of it (though that is a handy use-case!). Rather it's a log of all the actions that modified a particular reference. Usually this is HEAD, making the reflog a record of how the tip of a branch was updated. (To use on a specific branch, for example: git reflog show branchname)

Here is some typical default output (i.e. based on HEAD)

$ git reflog

# Effectively this runs: `git reflog show HEAD`

1745df8 (HEAD) HEAD@{0}: checkout: moving from e5e54cb990b56bc65ff2394e38401dbca0af27c8 to HEAD@{4}
e5e54cb (origin/master, test1, master) HEAD@{1}: checkout: moving from master to HEAD@{2}
e5e54cb (origin/master, test1, master) HEAD@{2}: reset: moving to e5e54cb
1745df8 (HEAD) HEAD@{3}: checkout: moving from e5e54cb990b56bc65ff2394e38401dbca0af27c8 to master
e5e54cb (origin/master, test1, master) HEAD@{4}: reset: moving to e5e54cb
1745df8 (HEAD) HEAD@{5}: reset: moving to master
e5e54cb (origin/master, test1, master) HEAD@{6}: checkout: moving from master to e5e54cb
1745df8 (HEAD) HEAD@{7}: commit: Add second line
e5e54cb (origin/master, test1, master) HEAD@{8}: checkout: moving from e5e54cb990b56bc65ff2394e38401dbca0af27c8 to master
e5e54cb (origin/master, test1, master) HEAD@{9}: checkout: moving from 75ea9be7a3e20fe494b931a1758da0d3bcfcfb7a to HEAD@{4}
75ea9be HEAD@{10}: checkout: moving from master to 75ea9be7a3e20fe494b931a1758da0d3bcfcfb7a
e5e54cb (origin/master, test1, master) HEAD@{11}: reset: moving to origin/master
e5e54cb (origin/master, test1, master) HEAD@{12}: checkout: moving from test1 to master
e5e54cb (origin/master, test1, master) HEAD@{13}: checkout: moving from master to test1
e5e54cb (origin/master, test1, master) HEAD@{14}: checkout: moving from 75ea9be7a3e20fe494b931a1758da0d3bcfcfb7a to master
75ea9be HEAD@{15}: checkout: moving from master to 75ea9be7a3e20fe494b931a1758da0d3bcfcfb7a
e5e54cb (origin/master, test1, master) HEAD@{16}: commit: Add text
75ea9be HEAD@{17}: commit (initial): First commit

Notice how all of them are HEAD@{N} (e.g. HEAD@{3}). That's the hint that they reference some prior state of HEAD.

The reflog reflects checkouts, commits, resets etc. - see the final column

I noticed something useful here: a reference to a commit no longer connected to my master branch. This would be handy for recovery, if I needed it.

1745df8 (HEAD) HEAD@{7}: commit: Add second line

I can get even more detailed info (and even about git's stash) with the --all option

$ git log --all

1d6d2ce HEAD@{19}: pull: Merge made by the 'recursive' strategy.
a095290 refs/heads/rebase@{3}: commit: Deploy from make
a095290 HEAD@{20}: commit: Deploy from make
f91450a HEAD@{21}: checkout: moving from brutto-netto to rebase
3d84fe1 refs/stash@{2}: WIP on brutto-netto: 7454359 Add basic untested code for brutto-rechner page
7454359 HEAD@{22}: reset: moving to HEAD