Home > Git > How to Delete a Git Branch – Locally and Remotely?

How to Delete a Git Branch – Locally and Remotely?

Hi guys, if you are searching for the best tutorial guide on git delete branches then you are in right place. In this tutorial, I will show you how you can delete a locally or remotely git branch. For both scenarios locally or remotely, you have to run the different commands in your terminal, I’ll explain you in detail in this guide.

As you know Git is a very powerful software for tracking the changes in files. It is usually used when you are working in a team and have a big project then you need Git software to collaborate with other programmers in your team.

Git is a software to tracks the changes in the files and makes a separate version of that commit for all changes files. So in case of something happen wrong then you can restore the correct version of the file.

GitHub is the online hosting web interface software, where you can host your files. It keeps your original files and changes in the file store in a different version if you have committed to it.

Github gives you access to create a new repository and create branches. A repository can have many branches and you can create as many as you want like development, production, staging, etc.

After creating the repository and branch, you can start working on your project. So as working on a big project, you may need to create many branches for each task.

What is Git Branch?

Branching is an important feature of version control systems such as Git. It allows you to make a new version of an existing project in Git. You can make modifications to the new branch without impacting the project’s original version.

Let’s take an example, suppose you are working in a team on a project like “Online Insurance Application“. One day you may find bugs on the application and you have to fix them. Or you need to add an extra feature in the application. So here you need to create a separate branch to fix the bugs.

So you have to clone the branch and name it as “bug-fix“. Then you fixed all bugs and merged both branches. Now you have all updated files in your main branch and don’t want to keep the “bug-fix” branch. So you can delete it using the git command.

The above case could be locally or remotely but both have different commands to delete the git branch. So, the commands we will see in this tutorial.

Delete a Git Branch Locally and Remotely

So when you finished working on the branch and merged with the main branch then you no longer need that branch. So you have to delete that branch from locally and remotely using the git command.

I mean to say locally is that where you are working on projects such as localhost and remotely is that where you hosted your project files such as GitHub.

So let’s see how we can delete a locally and remotely git branch.

Git Delete Local Branch

To delete a local branch from your local machine, you have to use the git command that is git branch -d. The -d flag tells Git is that you want to delete that branch.

NOTE: Before deleting the git branch make sure you are in that branch which you are not deleting. Because git will not allow you to delete currently working on a branch.

So I would recommend you to switch to the main branch before deleting any other branches. For example, git checkout main


  • -d : It will delete the branch if the branch has already merged into main branch or pushed on remote.
  • -D : It will force delete the branch, even if you hasn’t merged with main branch or pushed on remote.

Like the above example, we have created a branch called “bug-fix” to fix the bugs on the project. Then we merged into the main branch. So now you want to delete the “bug-fix” branch. So use the following command as a reference to delete the branch from the local system.

Normally delete git branch:

Forcefully delete git branch:

Git Delete Remote Branch

To delete a remote branch from the server where you hosted your project files, you have to use the git command like git push origin –delete branch. This command basically tells Git to push your local changes into remote and then delete the branch that you have specified.


Let’s take an example as we did above. Suppose we have a branch “remote-bug-fix” on the remote server and you already merged into the main branch. Now you want to delete this branch then use the following command as a reference.

The above command will delete the remote branch from your GitHub versioning system where you hosted your files.

Shorter Command to Delete a Remote Git Branch

There is a shorter command that you can use to delete the branch from git. In this command you don’t need to pass the --delete flag and we will use : instead.

So see the following shorter command to delete the remote git branch.


And if we want to delete the above same branch from remote then we will use the shorter command like the following.

After deleting the remote branch, you should run a fetch command to sync our local files with the remote files. The fetch command will retrieve the updated files and changes into your local repository.

So use the following to fetch the remote files into the local repository and keep the local repository up-to-date.

The -p flag means, delete any local branches that no longer exist on the remote repository. For more detail, you can read the git fetch command tutorial.


So in this tutorial, we learned how to delete a local git branch and remote git branch. And learned about forcefully deleting the local branch, even if it has not merged with the main branch. You also see the shorter command to delete the remote branch in git.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment