How to Delete a Local and Remote Branch using Git

In this post, we are going to see how to delete a local and remote branch using Git. If you are using Visual Studio Code as an IDE, you already know how it is a piece of cake there.

Here’s how to do it using Visual Studio Code.

But what if you are required to delete a branch using Git? Don’t worry! The following post has got you covered.

merge delete meme

How to Delete a Local Branch using Git

You can delete your local branches using the following command:

git branch -d branch_name 

The -d option is for --delete, which deletes the local branch, only if you have already pushed and merged it with your remote branches.

git branch -D branch_name

The -D option is for --delete --force, which basically deletes the branch irrespective of its push and merge status.

Local Branch Delete Example

Let’s take an example of deleting a local branch to see our command in action.

Step 1: Open git bash by navigating to your repositories folder and right clicking on “Git Bash Here”.

You might get a screen similar to like this:

You can check how many branches you have by a simple command. The following command will show you all the branches in your repo:

git branch

Step 2: Just go ahead and type that in git, once you have navigated to your desired branch. Just don’t be in the same branch you wish to delete.

The location you are at will be highlighted in green with an asterisk mark showing you where you at in your branches.

As you can see in the image above, we have a local branch called updateProfileMedia. Let’s delete this branch since, well, I no longer require it.

In order to delete this branch on local we can simply use the following command:

git branch -d updateProfileMedia

You might get a deleted branch message representation of that will be:

git delete branch

How to Delete a Remote Branch using Git

For remote branches, you can make use of the following:

git push <remote_name> --delete <branch_name>

An alternative for the above is:

$ git push <remote_name> :<branch_name>

where <remote_name> would be your root remote in most cases “origin”.

So just use the following command:

$ git push origin :<branch_name>

Then again, I would highly recommend you use a good IDE. An IDE like VSC makes sure you don’t have to remember such commands. One click and it’s done.

If you like the tutorial on how to delete a local and remote branch using Git, check out our other tutorials as well.

Don’t forget to spend a thought in the comments section below. Any feedback or ideas are highly appreciated.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

7 Responses

  1. Bradley says:

    Short, sweet, and to the point. Thanks!

  1. December 17, 2019

    […] Now that you know how to delete it using VSC, you can also check out how to delete it using git. […]

  2. February 6, 2020

    […] Then it is a good habit to maintain a clean repo, so maybe you want to select the delete source branch when merge request is accepted. That way you don’t have to actually go and delete your branches manually as was explained in the Git Delete a branch chapter. […]

  3. February 26, 2020

    […] So here’s an exercise for you guys. Now that you have created this branch, show me how you delete it. […]

  4. April 27, 2020

    […] sure to delete your branches when all the work has been done, and your branches have been merged with the […]

  5. March 15, 2023

    […] Hope you enjoyed learning about deleting branches in Bitbucket. Also, learn how to delete a local or remote branch on Git. […]

Leave a Reply