How to Delete GitHub Branch?

Destroying something when it’s not necessary is a mandatory thing.

It creates more space for new things and allows us to maintain the rest of the things easily. So, today we are going to explore different ways to delete a branch in GitHub.

Branches are like God’s gift for the developers. If you are a developer, you know what I mean. You may skip the next section and hop to delete the branch section if you are familiar with the branches’ actual use case. And keep on reading if you are a beginner to git or curious about branches.

What are branches?

A branch is a pointer referring to a commit. Reading won’t be good enough to understand about branches. We need to see how the branches work to understand them clearly.

We are going to see how the actual developers use branches in their projects with some illustrations. Note, each circle in the illustrations represents a commit.

Let’s see the workflow of branches with a simple real-time scenario.

So, you are managing things very carefully with the help of branches. There is no mess in it. If there are no things like branches, then imagine the situation that we will get into.

Hence, the conclusion is clear about branches. They are a boon for the developers like us.

Without further ado, let’s see how to delete a branch.

Delete Branch Using Git Client

When we are talking about deleting a branch, we are deleting it locally and remotely. So, don’t confuse yourself when we delete the same branch two times. Let’s see the steps to delete the branch.

Now, recheck the branches. You didn’t find the deleted branch in both local and remote if you have followed the above steps correctly.

We will get an error message saying branch not found if we try to delete a branch that doesn’t exist.

That’s it; we have successfully deleted a branch both locally and remotely.

There’s a slightly different way to do it using the GitHub web app.

Let’s see it.

Delete Branch Using Web

There’s no much difference between the previous method and this one. Here, we are going to use the GitHub web app to delete the remote branch. And we will delete the local branch as we delete in the above method.

Let’s see how to delete the remote branch using the GitHub web app.

Now, we have deleted the branch in the remote. Go to the local repository and delete the branch using the command we have seen in the first method.

Now, run the command git branch -a to check all branches.

We still see the deleted remote branch in the list. What’s it? How do we resolve it? See the below scenario where you will get into this type of situation at work.

Let’s assume you are working in a team. Your team lead deleted a remote branch when a particular task is done. How do you know about it? Is there any way to know about remotely deleted branches?

We need to sync the local and remote repositories about the deleted branches. There are certain commands to do it. They are

git remote prune remoteName
git fetch -p remoteName

The -p is the shortcut to prune in the second command. The prune option in both the above commands deletes the references to the remote.

Now, run the command git branch -a to check the branches list.

You will see that the remote branch is not showing up in the list. But, the local branch is still present. Yeah, it is. There is not a problem with it. You may keep or delete it.

So, check the branches that are not present in the remote that are present in local. Delete the local branches that are deleted in the remote.

Your branches are clean now. And you are good to go.

Conclusion

Most of the time, we will use the terminal or cmd for any git operations. And it’s convenient. But it’s not mandatory. By the end of the day, it’s a personal preference.

Whatever tool or method you use, the result is the same. Choose the one that you are convenient with and follow it to complete the task—two steps to delete a branch. Delete locally and remotely.

Next, learn how to delete the GitHub repo.

Happy Developing 🙂