[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 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.
Lis 18
Six rules to avoid delays in backend development
Hey there! I'm Catalin Dragutescu, a Full Stack Engineer at Railsware. Over the past six years, I've had the opportunity to work with engineering teams of various sizes. I've developed different growth strategies, tackled challenges, and prioritized tasks. I've seen both successes and setbacks. So I'm sure the topic of speeding up the development process interests everyone—from interns to seasoned developers. If you've encountered situations where hiring new developers doesn't increase productivity or large projects take too much time—keep reading. I'll share my own experience, discuss what strategies work and which don't, and talk about the aspects we considered when choosing a path to optimize development. So even if your primary focus isn't backend, you'll find this material useful.
Mar 6, 2024
Zarobki w IT. 10 najlepiej opłacanych stanowisk od lutego 2024
W lutym otrzymaliśmy ponad 70 nowych ankiet z informacjami o zarobkach programistów, testerów i innych specjalistów IT. W tym artykule wybraliśmy spośród nich 10 najlepiej opłacanych specjalistów IT.

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