![]() |
VOOZH | about |
Refs and reflogs in Git help track branch pointers and record updates to references, making it possible to recover lost commits.
Refs are stored as plain text files inside the .git/refs directory. You can explore them by navigating to .git/refs or by running a Git command from the projectβs root directory.
$ ls -F1 .git/refsor type the command in Git bash in the root directory of your project
find .git/refsYou should see the following structure, but it will contain different files depending on what branches, tags and remotes you have in your repo.
$ ls -F1 .git/refs
βββ heads/
β βββ master
βββ remotes/
βββ tags/
π ImageA reflog (reference log) records when the tips of branches and other refs were updated in your local repository. Every time you:
Git logs the movement of that reference into the reflog.
For example, Head@{2} points to where HEAD was two updates ago.
git reflog HEAD@{2}π ImageIf you want to inspect the commit itself, use:
git show HEAD@{2}Command that manages information recorded in reflogs:
git reflogπ ImageGit reflog commands come with various subcommands for advanced usage:
git reflog [show] [log-options] [<ref>]
git reflog expire [--expire=<time>] [--expire-unreachable=<time>]
[--rewrite] [--updateref] [--stale-fix]
[--dry-run | -n] [--verbose] [--all [--single-worktree] | <refs>β¦β]
git reflog delete [--rewrite] [--updateref]
[--dry-run | -n] [--verbose] ref@{specifier}β¦β
git reflog exists <ref>Words in square brackets such as "show", "log-options" are qualifiers, or we can say arguments to git reflog command.
Whether you are fixing mistakes or just understanding what happened, mastering refs and reflogs can make you much more confident using Git.