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.
One day team lead walks to you and says, “Hey, we got some errors in the product. You need to fix them.”
And you said, “Yeah, sure.”
Your git commits looks as follows.
Do you work from the master branch?
Of course no. If you work from the master branch itself, then you may face severe problems in the future. We will demonstrate how it will happen in some time.
Now, you have decided to take another branch from the master branch to fix bugs. Both branches will point to the same branch as of now.
You started working on bug fixes and made 5 commits. Hence, your new branch will move ahead as follows.
Your new branch pointing to the C8 commit, whereas our master branch pointing to the C3 commit.
Now, a surprising thing happens. Your team lead again to you and says, “Hey, we have a critical bug in the product that needs to be fixed immediately.”
Phew! That’s a lot.
You are already working on bug fixes. Now, there are more which have the most priority than the previous ones.
So, you have to switch to fix the new bugs.
What about the code that you are written till now?
There is no problem at all as you have created a new branch to fix the previous bugs. All the code that you are working till now will be in the bug-fix branch.
Now, switch to the master branch and create another new branch called critical-bug-fix and start working on the new bug fixes.
Let’s assume you haven’t created a new branch for the previous bugs. What do you think?
You have to delete all the code written for previous bugs and start working on the new bugs. And you have to write all the code again sometime for the previous bugs.
This is the exact problem that we are talking about.
So, branches are helping us to develop code independently.
Now, you have written some code to fix the new bugs and committed them.
You have completed fixing the new bugs.
Now, you have switched to the previous bugs branch and started working on them.
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.
Open the terminal or cmd and navigate to the git repository.
See the branches that are present in the repository with the command git branch -a. It will show both local and remote branches.
Copy the branch name that you want to delete. In the above case, it’s one.
Checkout to the master or main or any other branch that’s not the deleting branch.
Delete the branch locally with git branch -d branchName. Replace branchName with your actual branch name.
Check the branch with the git branch -a command. You will still find the deleted branch in the remote because we didn’t delete it in the remote.
To delete the branch in the remote, run the command git push remoteName -d branchName. Replace the remoteName and branchName with appropriate names.
There is a shortcut command to delete the branch remotely. The command is git push remoteName :branchName.
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.
Python is a very versatile language, and Python developers often have to work with a variety of files and get information stored in them for processing. One popular file format you’re bound to encounter as a Python developer is the Portable Document Format popularly known as PDF