[NEW] Search for a job anonymously — check the details
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.

Mar 14, 2024

W poszukiwaniu lepszego życia. Przeprowadzka Hiszpana do Polski i praca w banku

Daniel Rodríguez Montano urodził się w mieście Ponferrada, na północno-zachodniej Hiszpanii, ale od listopada 2018 roku mieszka w Warszawie. Mówi, że przeprowadził się do Polski, żeby “mieć, gdzie spać i jeść trzy razy dziennie”. Aktualnie pracuje w jednym z polskim banków na stanowisku Apps Support Intermediate Analyst i niedługo bierze ślub z dziewczyną z Polski.
Apr 23, 2023

Jak sprawdza się w praktyce CD Projekt Red urlop menstruacyjny

Nowy program jest dostępny dla wszystkich pracownic firmy na żądanie. Do urlopu można podejść elastycznie i skorzystać tylko z kilku godzin.
Oct 15

CEO Nvidii pragnie, by w jego firmie pracowało 50 000 ludzi i 100 milionów asystentów AI

Według Jensena Huanga, ogromna liczba "pracowników" sztucznej inteligencji pomoże zwiększyć wydajność Nvidii.

This site uses cookies to offer you a better browsing experience.

Find out more on how we use cookies and how to change cookie preferences in our Cookies Policy.

Customize
Save Accept all cookies