banner



github how to delete a branch

Netsparker Web Application Security Scanner - the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.

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.

  • Let's say you are working in a product development team.
  • 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.
Git Commits
Git Commits
  • Do you work from themasterbranch?
  • Of course no. If you work from themasterbranch 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 themasterbranch to fix bugs. Both branches will point to the same branch as of now.
New Branch
New Branch
  • You started working on bug fixes and made 5 commits. Hence, your new branch will move ahead as follows.
New Commits
New Commits
  • Your new branch pointing to theC8commit, whereas our masterbranch pointing to theC3commit.
  • 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 thebug-fix branch.
  • Now, switch to themasterbranch and create another new branch calledcritical-bug-fixand start working on the new bug fixes.
Another New Branch
Another New Branch
  • 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.
New Branch Commits
New Branch Commits
  • 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.
Repository Branches
Repository Branches
  • Copy the branch name that you want to delete. In the above case, it'sone.
  • Checkout to themasterormainor 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.
Delete Local Branch
Delete Local Branch
  • 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.
Branch List
Branch List
  • To delete the branch in the remote, run the command git push remoteName -d branchName. Replace the remoteName and branchName with appropriate names.
Delete Remote Branch
Delete Remote Branch
  • 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.

Branch List
Branch List

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

Delete Error
Delete Error

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.

  • Go to the GitHub.
  • Log in to your account.
  • Navigate to the repository in which you want to delete a branch.
Repository
Repository
  • Click on thebranchesbutton to see all the branches of the repository.
Repository Branches
Repository Branches
  • You will see the branches of the repository.
  • And you will also see a delete icon at the end.
Delete Branch Button
Delete Branch Button
  • Click the delete icon to delete the branch in the remote.
Deleted Branch
Deleted Branch
  • We can restore the branch by clicking theRestorebutton. It'll be available until we refresh or close the page.
Restore Button
Restore Button

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.

Branch List
Branch List

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.

remote prune
remote prune
fetch prune
fetch prune

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

Branches List
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 🙂

github how to delete a branch

Source: https://geekflare.com/delete-github-branch/

Posted by: singhsourn1974.blogspot.com

0 Response to "github how to delete a branch"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel