Home » How to Delete a Git Branch Both Locally and Remotely

How to Delete a Git Branch Both Locally and Remotely

Last updated on June 12, 2020 by

Sometimes, it is required to delete a Git branch both locally and remotely. Git provides a very easy way to handle branches in terms of creating and deleting branches. Let’s see how we do it. We hope you love it.

Table of Contents
1. Deleting a Branch Locally
2. Deleting a Branch Remotely
3. Directly Delete Branches on GitHub (GUI)

01 Deleting a Branch Locally

The git branch command is used to delete a branch. There are two variants of delete command available. We can try any of the following to delete a local GIT branch:

Syntax:

git branch -d <branch-name> // If branch pushed remotely and want to delete

git branch -D <branch-name> // If branch not pushed remotely or not merge and want to delete forcefully

Example:

Let’s suppose you want to delete a local branch 'list-view' then you can delete it like as shown below.

$ git branch -d list-view

Notes: Please note that Git will not allow you to delete the branch you are currently on so you need checkout a branch first. For example: If you are deleting a branch A then you must checkout to other branch like master. Use git checkout command to switch the branch.

The -d flag is an abbreviated form of --delete flag. You can use either -d or --delete. Both are used to delete a Git branch. Use -d if you have already pushed and merged it with your remote branches.

The -D flag is an abbreviated form of --delete --force flag. You can use either -D or --delete --force. Both are used to delete a Git branch. Use -D if you want to forcefully delete a branch even if you haven’t pushed or merged it.

02 Deleting a Branch Remotely

We can use the following command to delete a remote branch:

Syntax:

git push <remote-name> --delete <branch-name>

Example:

Let’s suppose you want to delete a remote branch 'list-view' then you can delete it like as shown below.

$ git push origin --delete list-view

Short Version of Command To Delete a Branch Remotely

git push <remote-name> :<branch-name>

Example:

$ git push origin :list-view

03 Directly Delete Remote Branches on GitHub

It could be a lot easier if we could delete a branch via a Graphical User Interface (GUI). Yes, You can easily delete remote branches on GitHub via simple clicks. Let’s see how to do it.

  1. Go to the GitHub and open the main page of your repository.
  2. Click on the branches tab as shown in the below screenshot.
number of branches on git
  1. Scroll to the branch that you want to delete, then click delete icon as shown in the below screenshot.
delete branch remotely via gui

Additionally read our guide, How To Rename Git Branch Both Locally and Remotely

That’s it from our end. We hope this article helped you to learn how to delete a Git branch both locally and remotely.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment