git reflog
- Version
- 2.51
- Review
- Draft
Summary#
The reflog records where HEAD and other refs pointed in this repository. Use it to find a commit left behind by a reset, rebase or amend, then create a branch at that entry.
Syntax#
git reflog
git reflog show branch-name
git branch rescue 'HEAD@{1}'
Example#
(
set -eu
scratch=$(mktemp -d "${TMPDIR:-/tmp}/devlookup-git.XXXXXX")
trap 'rm -rf -- "$scratch"' EXIT
unset GIT_CONFIG_GLOBAL $(git rev-parse --local-env-vars)
export GIT_CONFIG_NOSYSTEM=1 GIT_TEMPLATE_DIR= LC_ALL=C
export HOME="$scratch" XDG_CONFIG_HOME="$scratch" GIT_ATTR_NOSYSTEM=1
export GIT_PAGER=cat GIT_EDITOR=true
mkdir "$scratch/work"
cd "$scratch/work"
git init -q -b trunk
git config user.name "Reference Example"
git config user.email example@example.invalid
git commit -q --allow-empty -m "Start"
git commit -q --allow-empty -m "Write draft"
# Move back over a commit in a clean, disposable repository.
git reset -q --hard HEAD~1
git log --format=%s
echo "reflog:"
git reflog show --format='%gd %gs' HEAD
git branch rescue 'HEAD@{1}'
echo "rescued:"
git log --format=%s rescue
)
Expected output#
Start
reflog:
HEAD@{0} reset: moving to HEAD~1
HEAD@{1} commit: Write draft
HEAD@{2} commit (initial): Start
rescued:
Write draft
Start
Common mistakes#
HEAD@{1} means the previous position at the moment you run the command, and it shifts after every new movement, so read the log again before using a number. A reflog is local to the repository. It can recover commits after a hard reset. Uncommitted changes discarded by a reset are outside the reflog. Entries expire: the default limits are 90 days, or 30 days for entries no longer reachable from the current tip.
Version applicability#
These forms are available on the Git 2.51 version line covered here. Configuration can change the expiry limits.
The complete example requires Git 2.28 or later for git init -b.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0