[NEW] Szukaj pracy anonimowo — sprawdź szczegóły
Close

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.

Paz 12, 2022

Technologie przeciwko depresji, Polska rozwinięta oraz zawody, których AI nigdy nie zastąpi: rozmawiamy z Markiem Kamińskim

Marek Kamiński nie potrafi nie motywować. Jako pierwszy na świecie zdobył oba bieguny Ziemi bez pomocy z zewnątrz, który to wyczyn został zapisany w księdze rekordów Guinnessa. Napisał 13 książek, stworzył fundację, która pomaga młodym ludziom. Niedawno Fundacja Marka Kamińskiego uruchomiła aplikację, mającą na celu pomoc w walce z depresją.
0
Cze 4, 2024

Dział Google Cloud przeprowadził redukcję pracowników

To już drugie zwolnienie w Google w ciągu miesiąca.
0
Paz 29

Porównywarka cen pozwała Google do sądu i wygrała. Google zapłaci grzywnę w wysokości 2,4 miliarda euro

Google przez siedem lat walczyło z werdyktem wydanym w czerwcu 2017 roku, ale we wrześniu tego roku najwyższy sąd w Europie – Trybunał Sprawiedliwości Unii Europejskiej – odrzucił jego apelacje.
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