KKAIROS
Practical guideIllustrative3 min read

Git Reflog: the history of where HEAD pointed

git reflog lists where HEAD has been, including commits that no branch points to any more. It is not shared on push or clone.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-62-A: reflog still lists a commit after reset moved the branch away

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'b\n' >> f.txt; git commit -qam two
git reset -q --hard HEAD~1

Command:

echo log=$(git log --oneline | wc -l); git reflog --format=%gs | head -3

Recorded output:

log=1
reset: moving to HEAD~1
commit: two
commit (initial): one

Exit status: 0. The output matches the expected text written before the run. This is the status of head, the last command in the pipe.

TE-62-B: git reset --hard HEAD@{1} brings back the commit that a reset moved away from

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm one
printf 'b\n' >> f.txt; git commit -qam two
git reset -q --hard HEAD~1

Command:

git reset -q --hard 'HEAD@{1}'; echo log=$(git log --oneline | wc -l); git log -1 --format=%s

Recorded output:

log=2
two

Exit status: 0. This status and the output match what was written before the run.

TE-62-C: expected non-zero exit: a reflog position that does not exist is a fatal error

Setup for this run, inside a throwaway directory (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one

Command:

git reset --hard 'HEAD@{9}'

Recorded output: none, standard output was empty.

Standard error from the same run:

fatal: log for 'HEAD' only has 1 entries

Exit status: 128. This status and the output match what was written before the run.

TE-62-B: by the setup, reset had moved main back to the first commit. git reset --hard HEAD@{1} moved it forward again, the log has 2 lines, and the newest subject is two. TE-62-C: git reset --hard HEAD@{9} exited with status 128. Standard error reads fatal: log for 'HEAD' only has 1 entries.

Limits

The reflog is kept per repository and expires old entries. It is not shared on push or clone. Not tested on this page: the subcommands expire, delete and exists. The usage text printed by git reflog -h in git 2.34.1 lists them next to show, which is the default subcommand the runs here used without naming it.

Ad

Source

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-62-A | reflog still lists a commit after reset moved the branch away | 0 | PASS | ef2be06444c1 | | TE-62-B | git reset --hard HEAD@{1} brings back the commit that a reset moved away from | 0 | PASS | a12956f24f0d | | TE-62-C | expected non-zero exit: a reflog position that does not exist is a fatal error | 128 | PASS | 1eadfc68411e |

Recorded on: bash 5.1.16(1)-release; git version 2.34.1; Python 3.10.12. Locale C.UTF-8, TZ=UTC. Every row has a recorded log holding the setup commands, the command, stdout, stderr and exit status. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides