Open In App

Using Refs And Reflogs In Git

Last Updated : 19 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git, a popular version control system, helps developers track changes in their codebase. Understanding how to use refs and reflogs in Git can significantly enhance your ability to manage and troubleshoot your repositories. This article will learn about these concepts, providing you with the knowledge to utilize them effectively.

What Are Git Refs?

Refs, short for references, are pointers to commits in a Git repository. They serve as labels for specific points in your project’s history. Common refs include branches, tags, and the HEAD.

Types of Refs

  • Branches: A branch is a movable pointer to a commit. When you create a new branch, Git creates a new pointer for you to move around.
  • Tags: Tags are static pointers, often used to mark releases. Unlike branches, they do not move.
  • HEAD: The HEAD is a special ref that points to the current commit your working directory is based on.

Refs are stored as a normal file text in .git/refs directory. To explore refs in one of the project’s repositories navigate to .git/refs or type the following command in Git bash in the root directory of your project.

$ ls -F1 .git/refs

or type the command in Git bash in the root directory of your project

find .git/refs

You 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/

All local branches of repository are defined in refs/heads/ directory. Each file name matches names of the corresponding branch, and inside file, you will find a commit hash. This commit hash is the location of the tip of the branch.

What Are Git Reflogs?

Reflogs (reference logs) record when the tips of branches and other refs were updated in the local repository. They are invaluable for recovering lost commits and understanding the history of your repository.

$ git reflog HEAD@{2}

$ git reflog

This command manages information recorded in reflogs. The command takes various subcommands, and different options depending on the subcommands. Below are some and most used subcommands in reflogs.

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.  

  • Git Reflog Show: The “show” subcommand (which is also default, in absence of any subcommand) shows logs of the reference provided in command(or HEAD, by default). The reflog covers all recent actions, and in addition HEAD reflog records branch switching.
  • Git Reflog Expire: The “expire” subcommand prunes older reflog entries. Entries older than “expire” time, or entries older than “expire-unreachable” time and not  reachable from the current tip, are removed from the reflog. This is typically not used directly by end users
  • Git Reflog Delete: The “delete” subcommand deletes single entries from the reflog. Its argument must be an exact entry (e.g. “git reflog delete master@{2}“). This subcommand is also typically not used directly by end users.
  • Git Reflog Exists: The “exists” subcommand checks whether a ref has a reflog. It exits with zero status if the reflog exists, and non-zero status if it does not.

Next Article

Similar Reads