[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 25, 2022
Mobile Trends for Experts w Warszawie: o trendach i wpływie wojny na branżę aplikacji mobilnych
25 i 26 października w Warszawie odbywa się jedna z największych konferencji poświęconych branży mobile w Polsce - Mobile Trends for Experts. To ogólnokrajowe wydarzenie dla przedstawicieli sektora mobile, którzy szukają okazji do networkingu oraz zdobycia cennej na rynku wiedzy.
Lis 15, 2023
Trend "overemployed". Historia inżyniera, który pracował jednocześnie dla Tinder, Meta i IBM i zarabiał 820 000 USD rocznie
Cze 29, 2023
Rodzaje rekruterów: jakie zadać pytania
Poniższe porady będą przydatne dla deweloperów na poziomie od niższego do średniego, którzy otrzymują wiadomości od rekruterów.

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