How to Check If a Git Branch Is Deleted on Remote

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a Git branch has been deleted on a remote repository. We will cover the essential steps to update your local repository's view of the remote, list the available remote branches, and verify their existence using different Git commands.

You will begin by using git fetch --prune to synchronize your local repository with the remote and remove any outdated remote-tracking branches. Then, you will use git branch -r to list the remote-tracking branches that are now reflected in your local repository. Finally, you will learn how to use git ls-remote to directly query the remote repository for a specific branch, providing an alternative method for verification.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git(("Git")) -.-> git/CollaborationandSharingGroup(["Collaboration and Sharing"]) git/BranchManagementGroup -.-> git/branch("Handle Branches") git/CollaborationandSharingGroup -.-> git/fetch("Download Updates") git/CollaborationandSharingGroup -.-> git/remote("Manage Remotes") subgraph Lab Skills git/branch -.-> lab-560044{{"How to Check If a Git Branch Is Deleted on Remote"}} git/fetch -.-> lab-560044{{"How to Check If a Git Branch Is Deleted on Remote"}} git/remote -.-> lab-560044{{"How to Check If a Git Branch Is Deleted on Remote"}} end

Run git fetch --prune to Update

In this step, we'll learn how to update our local Git repository with the latest changes from a remote repository. Imagine you're working on a project with others, and they've made changes and pushed them to a central server. You need a way to get those changes into your local copy.

The command we use for this is git fetch. This command downloads commits, files, and refs from a remote repository into your local repository. It's like getting the latest updates from the server without actually merging them into your current work.

We'll also use the --prune option. This option removes any remote-tracking branches that no longer exist on the remote. This helps keep your list of remote branches clean and up-to-date.

Let's assume you have a remote repository configured (we'll cover how to add remotes in a future lab). For now, we'll simulate fetching from a remote.

Open your terminal, make sure you are in your ~/project/my-time-machine directory, and run the following command:

cd ~/project/my-time-machine
git fetch --prune origin

You might see output similar to this (the exact output depends on the remote repository):

From origin
 * [new branch]      feature/new-feature -> origin/feature/new-feature
 - [deleted]         (none)              -> origin/old-branch

This output shows that Git has fetched new branches (feature/new-feature) and removed branches that no longer exist on the remote (old-branch).

Running git fetch --prune is a good practice to keep your local view of the remote repository accurate. It allows you to see what changes have been made by others before you decide to integrate them into your own work.

Check git branch -r for Remote Branch

In the previous step, we used git fetch --prune to update our local repository with information from the remote. Now, let's see what remote branches are available in our local repository after the fetch.

We can list remote-tracking branches using the git branch command with the -r option. This command shows you the branches that Git has fetched from remote repositories.

Make sure you are still in the ~/project/my-time-machine directory and run the following command:

cd ~/project/my-time-machine
git branch -r

You should see a list of remote branches, typically prefixed with the remote name (like origin) followed by the branch name. For example:

  origin/master
  origin/feature/new-feature

This output tells you about the branches that exist on the remote repository (origin in this case) and that your local Git has information about. These are not local branches that you can directly work on; they are references to the state of the branches on the remote.

Understanding remote-tracking branches is important because they represent the state of the remote repository as of the last git fetch or git pull. They allow you to see what's happening on the remote without affecting your local working branches.

Verify with git ls-remote

In the previous step, we used git branch -r to see the remote-tracking branches in our local repository. Another useful command to see references on a remote repository is git ls-remote.

Unlike git fetch which downloads the objects, git ls-remote simply lists the references (like branches and tags) in a remote repository without fetching them. This is useful for quickly checking what's available on the remote without updating your local copy.

Make sure you are in the ~/project/my-time-machine directory and run the following command, specifying the remote name (which is typically origin):

cd ~/project/my-time-machine
git ls-remote origin

You should see output similar to this:

a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 HEAD
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 refs/heads/master
f0e1d2c3b4a59687765443210fedcba987654321 refs/heads/feature/new-feature

This output shows the commit hash followed by the reference name. HEAD usually points to the default branch on the remote (often master or main). refs/heads/ indicates a branch, and refs/tags/ would indicate a tag.

Comparing the output of git branch -r and git ls-remote can help you understand the difference between what's actually on the remote (git ls-remote) and what your local repository knows about the remote after the last fetch (git branch -r).

This command is a quick way to peek at the remote repository's structure without performing a full fetch operation.

Summary

In this lab, we learned how to check if a Git branch has been deleted on a remote repository. The first crucial step is to update our local repository's view of the remote using git fetch --prune. This command downloads the latest information from the remote and, importantly, removes any local remote-tracking branches that no longer exist on the remote, ensuring our local view is accurate.

After fetching and pruning, we can then use commands like git branch -r to list the remote-tracking branches that are still present in our local repository. By comparing the output before and after the fetch, or by simply observing the pruned branches during the fetch, we can determine if a specific branch has been deleted on the remote.