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.
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:
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.
Short, sweet, and to the point. Thanks!
Glad you liked it Bradley. Thanks.