Back to Git & Dev Tools
2026-02-197 min read

REFLOGS (Git & Dev Tools)

Learn REFLOGS (Git & Dev Tools) step by step with clear examples and exercises.

Title: Git Reflogs - A full guide to Git's Log of Changes

Why This Matters

Mastering Git reflogs is crucial for developers as they enable the recovery of lost commits, access to older versions of project history, and efficient troubleshooting. Whether you are working on a personal project or collaborating with others in a professional setting, understanding Git reflogs can save valuable time and effort when dealing with unexpected issues.

In addition to providing a safety net for your project's history, Git reflogs can also help developers understand the sequence of events leading up to an issue, making it easier to identify and resolve problems quickly.

Prerequisites

Before diving into Git reflogs, it's essential to have a solid foundation in the following concepts:

  1. Familiarity with Git fundamentals: commits, branches, merges, tags, and Git objects (commits, trees, blobs, and tags)
  2. Basic understanding of Git commands: git init, git add, git commit, git push, git pull
  3. Knowledge of Git branching and merging strategies: feature branches, pull requests, and merge conflicts
  4. Comprehension of Git workflows and best practices
  5. Understanding the concept of Git objects (SHA-1 hashes) and how they are used to identify commits, trees, blobs, and tags

Core Concept

Git reflogs are logs of changes to references (branches, tags, etc.) within a Git repository. They serve as a backup system for your project's history, providing a way to recover lost commits or access older versions of your project. Each reflog entry contains the following information:

  • The reference that was updated
  • The old value of the reference (SHA-1 hash)
  • The new value of the reference (SHA-1 hash)
  • The time and date when the change occurred
  • The number of the reflog index (a unique identifier for each entry within a specific reflog)

Reflog Structure

Each ref in Git maintains a separate reflog, which is stored as an object in the Git repository. A typical reflog entry looks like this:

refs/heads/master@{0}: branch: Master
refs/heads/master@{1}: commit: 1b61de420a21a2f1aaef93e38ecd0e45e8bc9f0a

In the above example, refs/heads/master is the reference (branch), and @{0} and @{1} are the reflog indexes. The older entry has a higher index number.

Accessing Reflogs

To view the reflogs for a specific branch or tag, use the following command:

git log --graph --pretty=format:'%h %ad | %s' @{u}..HEAD --all --date=short --full-history

This command shows a graphical representation of the commit history, along with the commit message and date for each entry. The @{u} reference refers to the upstream branch (if there is one). By using the --full-history flag, you can view all commits in the reflog, not just those that affected the current branch.

Worked Example

Let's explore a scenario where we accidentally delete a commit and need to recover it using Git reflogs:

  1. First, let's create a simple repository with three commits:
$ git init
$ echo "Initial commit" > README.md
$ git add .
$ git commit -m "Add initial commit" (commit hash: 1b61de4)
$ echo "Second commit" >> README.md
$ git add .
$ git commit -m "Add second commit" (commit hash: a2c3bf8)
$ echo "Third commit" >> README.md
$ git add .
$ git commit -m "Add third commit" (commit hash: 5973e04)
$ git branch feature1
$ git checkout feature1
$ git commit -am "Commit on feature1" (commit hash: 789abc1)
$ git checkout master
$ git branch -d feature1
$ git push origin master
  1. Now, let's delete the second commit from the master branch using git reset HEAD^. This will remove the second commit and move the pointer to the third commit.
  1. To recover the deleted commit using Git reflogs, use the following command:
$ git reflog show HEAD

This command displays the reflog entries for the current branch (master). Find the commit hash of the deleted commit (a2c3bf8) and create a new branch or checkout the commit using git cherry-pick .

  1. Finally, push the recovered commit to the remote repository:
$ git push origin master

Common Mistakes

  1. Not understanding the difference between Git reflogs and Git reflog: Git reflog is a log of changes to references within a single repository, while Git reflog is a shorthand for Git's internal "ref logging" mechanism that records all changes to local branches, tags, and other references.
  2. Not using Git reflogs to recover lost commits: Developers often forget about Git reflogs when dealing with lost commits or accidentally deleting branches. It's essential to understand how to use Git reflogs for such situations.
  3. Misconfiguring Git reflogs: By default, Git keeps reflogs for up to 90 days on the master branch and 30 days on other branches. However, you can customize this behavior using the git config command. Be careful not to set overly aggressive or too conservative retention periods.
  4. Not properly documenting Git reflog usage: It's important to document any recovery actions taken using Git reflogs in commit messages or project documentation to maintain a clear and organized repository history.
  5. Ignoring Git reflogs during merge conflicts: Developers may overlook Git reflogs when resolving merge conflicts, but they can be useful in understanding the changes made by different branches and recovering lost commits if necessary.
  6. Not using --full-history flag when viewing reflogs: The --full-history flag is essential for viewing all commits in the reflog, not just those that affected the current branch.
  7. Not understanding Git object hashes: Understanding how Git uses SHA-1 hashes to identify commits, trees, blobs, and tags can help developers better understand how Git reflogs work and how they are stored within a repository.

Practice Questions

  1. How do you view the reflogs for a specific branch in your repository?
  2. What information does each Git reflog entry contain?
  3. Suppose you accidentally delete a commit and need to recover it using Git reflogs. Describe the steps to accomplish this task.
  4. What is the difference between Git reflogs and Git reflog?
  5. How can you customize the retention period for Git reflogs in your repository?
  6. Explain how Git reflogs can help during merge conflicts.
  7. Discuss best practices for documenting Git reflog usage in a project.
  8. What is the purpose of using the --full-history flag when viewing Git reflogs?
  9. How does Git use SHA-1 hashes to identify commits, trees, blobs, and tags within a repository?
  10. Why might it be important to understand how Git stores objects in a repository when working with Git reflogs?

FAQ

  1. Why are Git reflogs important for developers?
  • Git reflogs help developers recover lost commits, access older versions of their project's history, and troubleshoot issues more effectively.
  1. How can I view the Git reflog for a specific branch or tag?
  • Use the command git log --graph --pretty=format:'%h %ad | %s' @{u}..HEAD --all --date=short --full-history.
  1. What information does each Git reflog entry contain?
  • Each entry contains the reference that was updated, the old value of the reference (SHA-1 hash), the new value of the reference (SHA-1 hash), the time and date when the change occurred, and the number of the reflog index.
  1. How can I recover a deleted commit using Git reflogs?
  • First, find the commit hash of the deleted commit from the reflog. Then, create a new branch or checkout the commit using git cherry-pick . Finally, push the recovered commit to the remote repository.
  1. How can I customize the retention period for Git reflogs in my repository?
  • Use the command git config --global core.reflogExpire to set the retention period for all branches or git config .reflogExpire to set it for a specific repository only.
  1. How can Git reflogs help during merge conflicts?
  • Git reflogs can provide insight into the changes made by different branches, helping developers resolve merge conflicts more effectively and understand the impact of each branch on the project's history.
  1. What are best practices for documenting Git reflog usage in a project?
  • Document any recovery actions taken using Git reflogs in commit messages or project documentation to maintain a clear and organized repository history. This helps other developers understand what changes were made and why, making collaboration more efficient.
  1. What is the purpose of using the --full-history flag when viewing Git reflogs?
  • The --full-history flag allows you to view all commits in the reflog, not just those that affected the current branch.
  1. How does Git use SHA-1 hashes to identify commits, trees, blobs, and tags within a repository?
  • Git uses SHA-1 hashes as unique identifiers for each object (commit, tree, blob, or tag) in the repository. Each object is created by taking its contents and hashing them using the SHA-1 algorithm. This creates a unique hash that can be used to identify the object within the repository.
  1. Why might it be important to understand how Git stores objects in a repository when working with Git reflogs?
  • Understanding how Git stores objects in a repository can help developers better understand how Git reflogs work and how they are stored within a repository. This knowledge can help developers troubleshoot issues, optimize their workflow, and recover lost commits more effectively.
REFLOGS (Git & Dev Tools) | Git & Dev Tools | XQA Learn