Back to Git & Dev Tools
2026-01-276 min read

The reflog (Git & Dev Tools)

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

Title: Mastering Git's Reflog: A full guide for Developers

Why This Matters

In the fast-paced world of software development, version control systems like Git are essential tools that help manage code changes effectively. However, even seasoned developers can find themselves in a pickle when they need to recover lost commits or revert accidental changes. This is where Git's Reflog comes into play, offering a safety net for your precious code.

Understanding and mastering the Reflog can help you navigate through various scenarios such as accidentally deleting commits, losing work due to mistakes, or even recovering from catastrophic events like hard drive failures. In this guide, we will explore Git's Reflog in detail, providing examples and best practices for using this powerful tool effectively.

Prerequisites

Before diving into the Reflog, ensure you have a good understanding of basic Git commands such as git init, git add, git commit, and git pull. Familiarity with branching and merging will also be beneficial for fully grasping the power of the Reflog. It is recommended to practice these commands in a test environment before working on critical projects.

Git Basics

To gain a deeper understanding of the Reflog, let's briefly review some essential Git concepts:

  • Git Commands: Basic Git commands include git init, git add, git commit, and git pull. These commands help create a new repository, stage files for committing, commit changes, and sync with remote repositories.
  • Branching: Branching allows you to create separate lines of development within your Git repository. This enables you to work on different features or fixes without affecting the main codebase until they are ready to be merged.
  • Merging: Merging combines changes from two or more branches into a single branch. This is often used when multiple developers have made changes to the same codebase, or when merging a feature branch back into the main branch.

Core Concept

Understanding Git's Reflog

Git's Reflog (short for Reference Log) is a built-in mechanism that keeps track of all changes made to your repository, even after you've deleted or lost commits. It acts as an extra layer of protection for your code, allowing you to recover lost work or undo unintended actions.

The Reflog stores information about every commit, including the author, date, and message, along with a unique identifier called a SHA-1 hash. This data is saved in a special file named .git/reflog within your repository's hidden .git directory.

Accessing the Reflog

To interact with the Reflog, you can use Git commands such as git reflog, git cherry-pick, and git reset. Here's a brief overview of how to access and manipulate your repository's Reflog:

  1. View the Reflog: Use the git reflog command to display a list of all commits, along with their SHA-1 hashes and other metadata.
$ git reflog
  1. Checkout a specific commit: To restore a lost commit, use the git checkout command followed by the SHA-1 hash of the desired commit.
$ git checkout <SHA-1 hash>
  1. Cherry-pick commits: If you want to selectively apply changes from one or more commits, use the git cherry-pick command followed by the SHA-1 hashes of the commits you wish to apply.
$ git cherry-pick <SHA-1 hash1> <SHA-1 hash2> ...
  1. Resetting with Reflog: In case you need to undo recent changes, use the git reset command followed by the desired commit's SHA-1 hash or a relative term like "HEAD~n" (where n is the number of commits you want to move back). To ensure that Git saves the discarded commits in the Reflog, use the --soft, --mixed, or --hard options.
$ git reset --soft <SHA-1 hash>
$ git reset HEAD~n

Internals (optional if burst config says so)

For those interested in the technical details, Git stores the Reflog as a series of reference objects within its database. Each reference object contains information about a specific commit, such as its parent commits, author, date, and message. The Reflog also keeps track of additional metadata like the SHA-1 hashes of the commits that were created or deleted since the last Git garbage collection run.

Worked Example

Let's consider a scenario where you accidentally delete a commit containing important changes to your codebase. To recover the lost commit, follow these steps:

  1. View the Reflog:
$ git reflog
  1. Identify the desired commit: Find the SHA-1 hash of the commit you want to restore and note it down.
  1. Checkout the commit:
$ git checkout <SHA-1 hash>
  1. Verify the recovery: Confirm that your lost changes have been successfully restored by checking the relevant files in your codebase.

Common Mistakes

1. Not understanding the difference between git reset and git checkout

While both commands allow you to modify your Git history, they behave differently when it comes to the Reflog. git checkout moves the HEAD pointer to a different commit without altering the Reflog, whereas git reset removes commits from the main branch and stores them in the Reflog. Be sure to use the appropriate command depending on your needs.

2. Not specifying a commit when using git reset

When you run git reset, Git will by default move the HEAD pointer to the most recent commit, discarding all changes since that commit. To avoid losing work unintentionally, always specify a commit SHA-1 hash or use a relative term like "HEAD~n" when using git reset.

3. Ignoring the Reflog when faced with lost commits

In some cases, developers may not be aware of the Reflog's existence and resort to panic measures such as cloning a backup repository or even starting from scratch. Remember that the Reflog can help you recover lost commits quickly and easily, so always check it first before taking drastic actions.

Practice Questions

  1. You have accidentally deleted a commit containing important changes to your codebase. What command would you use to restore the lost commit?
  2. Suppose you want to cherry-pick commits from two different branches in your Git repository. How can you achieve this using Git commands?
  3. Explain the difference between git reset --soft, git reset --mixed, and git reset --hard when it comes to their impact on the Reflog.
  4. You have made changes to a file in your working directory but not yet committed them. After running git add ., you realize that you want to discard these changes without committing them. What command would you use and how does it affect the Reflog?
  5. If you accidentally commit sensitive data to a public repository, can you use the Reflog to remove the offending commit from both the local and remote repositories?

FAQ

Q: Can I manually delete commits from my repository's Reflog?

A: No, you cannot manually remove entries from the Reflog. However, Git automatically cleans up old entries during garbage collection runs.

Q: How long does Git keep commits in its Reflog?

A: By default, Git keeps commits in the Reflog for approximately 30 days, but this period can be configured using the gc.reflogExpire and gc.reflogExpireUnreachable options.

Q: Is it possible to recover deleted files from a Git repository using the Reflog?

A: Yes, if you delete files within a commit that is still present in the Reflog, you can use the git checkout command to restore the deleted files. However, if the commit containing the deleted file has been removed from the main branch and only exists in the Reflog, you will need to create a new commit with the recovered files before you can push your changes to the remote repository.

Q: Can I use the Reflog to recover changes made to a specific file within a commit?

A: Yes, you can use git checkout to recover changes made to a specific file within a commit. First, identify the SHA-1 hash of the desired commit using git reflog. Then, use the following command to retrieve the contents of the specified file from that commit:

$ git checkout <SHA-1 hash> -- path/to/your/file

This will overwrite your current version of the file with the one from the chosen commit.

The reflog (Git & Dev Tools) | Git & Dev Tools | XQA Learn