How to Check If a Git Branch Is Deleted Locally

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a Git branch has been deleted locally. We will begin by verifying the absence of a specific branch using git branch to ensure a clean starting state. Then, we will explore the git reflog command, a powerful tool that records updates to references in your local repository, including branch deletions, allowing you to see the history of changes. Finally, we will test these methods by working with a recently deleted branch to confirm how to effectively check for its local deletion status.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BranchManagementGroup -.-> git/branch("Handle Branches") git/BranchManagementGroup -.-> git/reflog("Log Ref Changes") subgraph Lab Skills git/branch -.-> lab-560043{{"How to Check If a Git Branch Is Deleted Locally"}} git/reflog -.-> lab-560043{{"How to Check If a Git Branch Is Deleted Locally"}} end

Run git branch to Verify Absence

In this step, we will verify that a specific branch, which we will delete later, does not currently exist in our Git repository. This is a good practice to ensure we are starting from a clean state before performing operations like branch deletion.

First, navigate to your project directory if you are not already there. Remember, our project is located at ~/project/my-time-machine.

cd ~/project/my-time-machine

Now, let's list the existing branches in our repository using the git branch command. By default, this command shows you the local branches.

git branch

You should see output similar to this:

* master

This output indicates that currently, only the master branch exists, and it is the active branch (indicated by the asterisk *).

For the purpose of this lab, we will be working with a branch named feature/new-feature. Let's confirm that this branch does not exist yet by running git branch again. Since we just ran it and saw only master, we already know it's not there, but running the command again reinforces the concept of checking branch status.

git branch

The output should still show only the master branch:

* master

This confirms that the feature/new-feature branch is not present in our repository at this moment. This is the state we expect before we proceed to create and then delete this branch in subsequent steps. Understanding how to check the status of your branches is fundamental to managing your project's history effectively.

Check git reflog for Deletion

In this step, we will explore the git reflog command, which is a powerful tool for recovering lost commits or branches. The reflog (reference log) records updates to the tips of branches and other references in the local repository. This means it logs almost every change you make in your repository, including commits, merges, rebases, and even branch deletions.

First, ensure you are in your project directory:

cd ~/project/my-time-machine

Now, let's create a new branch that we will delete later. This will give us something to look for in the reflog.

git branch feature/new-feature

This command creates a new branch named feature/new-feature pointing to the current commit. Let's verify it exists:

git branch

You should now see both branches:

* master
  feature/new-feature

Now, let's delete the feature/new-feature branch using the -d flag, which is a "safe" delete (it prevents deletion if the branch has unmerged changes).

git branch -d feature/new-feature

You should see output confirming the deletion:

Deleted branch feature/new-feature (was <commit-id>).

Replace <commit-id> with the actual commit ID shown in your terminal.

Now, let's check the reflog to see if the deletion was recorded.

git reflog

The output will show a history of actions. You should see an entry related to the branch deletion, similar to this (the exact output may vary):

<commit-id> HEAD@{0}: branch: deleted feature/new-feature
<commit-id> HEAD@{1}: branch: Created from <another-commit-id>
... (other reflog entries)

The reflog entry HEAD@{0}: branch: deleted feature/new-feature indicates that the feature/new-feature branch was deleted. The HEAD@{0} refers to the most recent action. This demonstrates that even though the branch is gone from git branch, its deletion is recorded in the reflog, making it potentially recoverable.

Understanding git reflog is crucial because it acts as a safety net. If you accidentally delete a branch or lose commits due to a rebase or other operation, the reflog can help you find the commit ID you need to restore your work.

Test with Recently Deleted Branch

In this step, we will demonstrate how to potentially recover a recently deleted branch using the information from the git reflog. While we won't fully recover the branch in this specific lab (as it had no unique commits), we will practice the command used for recovery.

First, ensure you are in your project directory:

cd ~/project/my-time-machine

Recall from the previous step that we deleted the feature/new-feature branch. Let's confirm it's still gone:

git branch

The output should still show only the master branch:

* master

Now, let's look at the reflog again to find the entry for the deleted branch.

git reflog

Find the line that says branch: deleted feature/new-feature. Note the commit ID associated with this entry. It will look something like <commit-id> HEAD@{0}: branch: deleted feature/new-feature.

To recover a deleted branch, you would typically use the git branch <branch-name> <commit-id> command, where <branch-name> is the name you want to give the recovered branch, and <commit-id> is the commit ID from the reflog where the branch was last pointing.

In our case, the feature/new-feature branch was created and immediately deleted without any new commits on it. Therefore, its commit ID in the reflog will be the same as the tip of the master branch. Recovering it would essentially just recreate the branch pointing to the same commit as master.

Let's simulate the recovery command using the commit ID you found in the reflog for the deleted branch. Replace <commit-id> with the actual ID from your reflog output.

git branch recovered-feature <commit-id>

This command creates a new branch named recovered-feature pointing to the commit ID where feature/new-feature was before deletion.

Now, let's check our branches again:

git branch

You should now see the recovered-feature branch listed:

* master
  recovered-feature

This demonstrates how you can use the git reflog to find the commit ID of a lost branch and then use git branch to recreate it. This is a powerful technique for recovering from accidental deletions or other history-altering operations.

Summary

In this lab, we learned how to verify the absence of a local Git branch using the git branch command. We confirmed that before creating and deleting a branch, it's important to check the current branch status to ensure a clean starting point.

We also began exploring the git reflog command, understanding its role in tracking changes to references in the local repository, including branch deletions, which is crucial for potentially recovering lost work.