How to Recover Deleted Branches and Commits in Git with the “Reflog”

Photo by Pixabay on PexelsThere’s a little-known feature in Git called the “Reflog”. It’s one of many features in Git that can help you save your neck! For example when you’ve accidentally deleted a branch. Or when you performed a “reset” and realize y…

Photo by Pixabay on Pexels

There’s a little-known feature in Git called the “Reflog”. It’s one of many features in Git that can help you save your neck! For example when you’ve accidentally deleted a branch. Or when you performed a “reset” and realize you’ve just deleted valuable commits by mistake.

Let’s dive in and talk about what the Reflog actually is, what you can use it for, and how you can use it in practice.

What the Reflog Is

You can think of the Reflog as something like a “journal”: it’s where Git protocols all HEAD pointer movements, e.g. when a checkout, commit, merge, rebase, reset, etc. happens. In other words: whenever something really important happens, Git protocols it in the Reflog. This, of course, means that the Reflog is extremely helpful when things go wrong.

Before we move on, let’s take a quick look at an example Reflog:

By default, the Reflog this sorted chronologically. This means that the most recent action is listed at the top. You can also easily see what kind of action was performed: whether it was a commit, pull, checkout, or something else.

What Can the Reflog Do for You?

As I already said, the Reflog is perfect for undoing mistakes. Let’s look at two practical examples where the Reflog can be very helpful.

How to Undo Deleting a Branch

To keep your Git repository clean and tidy, it’s essential that you delete obsolete branches in your repository from time to time. Sometimes, however, you might have been too quick and have deleted a branch that you urgently need to get back. This is a perfect example for using the Reflog! Let’s take a look at an example scenario:

Note: in some of the screenshots in this article I’m using the Git desktop GUI “Tower” for easier visualization.

Let’s say we (think we) don’t need this “feature/login” branch anymore. When we delete it in a second, we will not only lose the branch: since its latest commit isn’t contained anywhere else, we will also lose this commit! But let’s just close our eyes, delete the branch, and let the catastrophe take its course. ?

But since it’s our current HEAD branch, we first need to switch to another branch before we can delete it:

# switch away from the branch, to master
$ git switch master

# delete the unwanted feature branch
$ git branch -D feature/login

Note that we had to use the capital “-D” option when deleting the branch, because it contained unmerged changes (that latest commit that is present nowhere else). The branch is now gone — and I’ll give you a couple of seconds to mourn when you realize that this was a grave mistake! ?

Let’s open up the Reflog and see if we can undo this mishap:

$ git reflog
776f8ca (HEAD -> master) HEAD@{0}: checkout: moving from feature/login to master
b1c249b HEAD@{1}: checkout: moving from master to feature/login
776f8ca (HEAD -> master) HEAD@{2}: checkout: moving from feature/login to master
b1c249b HEAD@{3}: Branch: renamed refs/heads/develop to refs/heads/feature/login
b1c249b HEAD@{5}: commit: Change Imprint page title

Think back to what we just did: before deleting the branch, we switched to master. And this is the most recent item that has been protocoled in the Reflog. Now, to undo our fatal mistake of deleting the feature branch, we can simply return to the state before that latest entry!

We can copy the necessary commit hash from the Reflog and use the “git branch” command to get our deleted branch back:

$ git branch feature/login b1c249b

Voila! We just brought our branch back from the dead:

$ git branch
feature/login
* master

In case you’re using a desktop GUI like Tower, here’s how you can do this:

How to Recover Deleted Commits

Another area where the Reflog can be tremendously helpful is when you want to recover deleted commits. Again, let’s look at an example scenario:

We’ll reset our current HEAD branch to this older revision, and thereby “delete” any newer commits that came afterwards. Pretty straightforward!

$ git reset --hard 2b504bee

But what if you realize that this was a mistake — and you desperately need those removed commits back?!? ? Let’s take another look at the Reflog to see if it can help us:

$ git reflog
2b504be (HEAD -> master, origin/staging) HEAD@{0}: reset: moving to 2b504bee
26acef5 HEAD@{1}: commit (amend): Move new product page
83bfa7d HEAD@{2}: commit (amend): Move product page

The latest entry in the Reflog is the “reset” operation that we just performed (our mistake, so to say). If we want to undo this action and get our old commits back, we can simply return to the state before! So let’s copy the commit hash of that second entry and use “git reset” once more:

$ git reset --hard 26acef5

Voila! If you check “git reflog” again, you’ll see that our seemingly lost commits are recovered and available on our HEAD branch again!

Discovering the Power of Git

The Reflog is just one out of many examples: Git has a huge amount of advanced features that can be tremendously helpful — to undo mistakes, produce better code, or make you more productive in your daily work.

There are lots of guides and tutorials out there to help you get more proficient with Git. A great resource to get you started is the “First Aid Kit for Git”, a free series of short videos that teach you how to undo almost any mistake with Git.

Have fun becoming a better programmer!

About the Author

Tobias Günther is the co-founder of Tower, the popular Git desktop client that helps more than 100,000 developers around the world to be more productive with Git.


How to Recover Deleted Branches and Commits in Git with the “Reflog” was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Tobias Günther | Sciencx (2024-03-29T13:44:06+00:00) » How to Recover Deleted Branches and Commits in Git with the “Reflog”. Retrieved from https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/.
MLA
" » How to Recover Deleted Branches and Commits in Git with the “Reflog”." Tobias Günther | Sciencx - Wednesday March 3, 2021, https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/
HARVARD
Tobias Günther | Sciencx Wednesday March 3, 2021 » How to Recover Deleted Branches and Commits in Git with the “Reflog”., viewed 2024-03-29T13:44:06+00:00,<https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/>
VANCOUVER
Tobias Günther | Sciencx - » How to Recover Deleted Branches and Commits in Git with the “Reflog”. [Internet]. [Accessed 2024-03-29T13:44:06+00:00]. Available from: https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/
CHICAGO
" » How to Recover Deleted Branches and Commits in Git with the “Reflog”." Tobias Günther | Sciencx - Accessed 2024-03-29T13:44:06+00:00. https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/
IEEE
" » How to Recover Deleted Branches and Commits in Git with the “Reflog”." Tobias Günther | Sciencx [Online]. Available: https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/. [Accessed: 2024-03-29T13:44:06+00:00]
rf:citation
» How to Recover Deleted Branches and Commits in Git with the “Reflog” | Tobias Günther | Sciencx | https://www.scien.cx/2021/03/03/how-to-recover-deleted-branches-and-commits-in-git-with-the-reflog/ | 2024-03-29T13:44:06+00:00
https://github.com/addpipe/simple-recorderjs-demo