Travelling back in time with git reflog

Hi! I’m João Pedro Fernandes Colnaghi, a Full Stack Engineer at Railsware, a product studio focused on building impactful solutions.

Git is a cornerstone of modern development workflows, but one of its most powerful features often flies under the radar: git reflog. While git log offers a straightforward history of your commits, git reflog goes deeper, keeping track of every action in your repository—even those you might think are lost.

It’s an invaluable tool for debugging, recovering lost work, and managing complex workflows. In this guide, I’ll walk you through how to use git reflog effectively and why it’s a must-have in your development toolkit. Let’s get started.

What makes git reflog so unique?

Unlike the commit history you see in git log, the reflog tracks everything: commits, branch checkouts, resets, rebases—you name it. Think of it as a ledger of all the changes you’ve made in your local repository, even the ones you thought were lost.

Here’s the thing: branch history is tied to your repository’s state, but git reflog exists independently. Even if you delete a branch or reset to a previous commit, the reflog remembers.

A practical example output:

It can also be combined with commands like git reset, git checkout, and git rebase, git reflog. That can help you recover from mistakes, refine your history, and troubleshoot issues.

Let’s break this down with detailed, real-world scenarios:

Recovering lost commits

One of the most common use cases for git reflog is recovering lost commits. This can happen due to scenarios like a bad rebase, a force-push, or making commits in a detached HEAD state.

Let’s say we have a branch with the following commits:

However, for some reason, we decide to rebase this branch and drop one of the commits:

Then, later you realize you actually needed that commit. Now what? Rewrite the code all over again? No, that’s when you have git reflog to the rescue!

With this information, you can either reset the state to before the start of the rebase or cherry-pick the desired commit, depending on your needs.

Cleaning up reflog entries

When it comes to manually managing the reflog, you have two options:

  1. Pruning Old Entries: Use git reflog expire to remove outdated entries efficiently. This is the recommended approach for routine cleanup.
  2. Deleting Specific Entries: Use git reflog delete if you need to remove particular entries from the reflog.

Here’s an example of both approaches:

These commands are provided here for informational purposes only and should generally not be executed manually unless absolutely necessary. Automated maintenance tasks (e.g., git gc) typically handle reflog cleanup effectively.

Key takeaways

With git reflog and related commands like git reset, and git rebase, you can:

  • Recover from common Git errors with ease.
  • Tidy up your commitment history for clarity and collaboration.
  • Troubleshoot and debug even complex workflows confidently.

These tools streamline your Git workflow, helping you efficiently address mistakes and refine your repository. Instead of getting stuck fixing errors, they let you focus on moving your work forward.

Cze 8, 2023

Jak wyglądały pierwsze biura Apple, Google, Facebook, HP i innych gigantów branży IT. Przegląd zdjęć

Użytkownik Twittera Jon Erlichman opublikował wpis dotyczący pierwszych biur największych światowych firm. Na liście znajdują się zdjęcia takich firm jak Amazon, Google, Facebook, Ericsson, HP, Dell, Salesforce, YouTube i wiele innych.
0
Sie 18

„Jestem programistą, który nie pamięta czasów sprzed AI” — od sprzedawcy w sklepie do developera sztucznej inteligencji

Jak znaleźć pierwszą pracę w IT, jakie wyzwania czekają programistów, którym nieznana jest era „przed AI”, na jakiej umowie najlepiej pracować w Polsce jako programista AI oraz czterdzieści najbardziej zagrożonych przez AI zawodów, które Microsoft opublikował w swoim raporcie. O tym wszystkim rozmawiamy z Grzegorzem w wywiadzie.
0
Gru 30, 2024

Dla kogo naprawdę jest Agile i dlaczego firmy zawodzą w jego wdrożeniu? Rozmowa z Agile Coachem

Dawid Likos uważa, że Agile jest odpowiedzią na świat VUCA i BANI. Dawid ma wielokulturowe doświadczenie: pomagał w transformacji Agile i Lean zespołom na Filipinach, Kolumbii i innych krajach.
0

Ta strona używa plików cookie, aby zapewnić Ci lepsze wrażenia podczas przeglądania.

Dowiedz się więcej o tym, jak używamy plików cookie i jak zmienić preferencje dotyczące plików cookie w naszej Polityka plików cookie.

Zmień ustawienia
Zapisz Akceptuj wszystkie cookies