How Delete a Git Branch Locally and Remotely?
Last Updated :
02 Jul, 2024
In Git, branches are an important part of the workflow, allowing developers to work on different features, bug fixes etc. without affecting the main codebase. However, once a branch has served its purpose, it is a good practice to delete it to keep the repository clean and organized. This article will guide you through the steps to delete a Git branch both locally and remotely.
Delete a Git Branch Locally
Git won't allow you to delete a Git branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:
Syntax
git checkout <branch-name>
Here we will check out our main branch from my test branch.

Now in order to delete the test branch locally, we use the command :
Syntax
git branch -d <branch-name>
We will delete my test branch as an example.

Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with --delete --force. This will forcefully delete the branch even if it hasn't been pushed or merged with the remote. the full command is:
Syntax
git branch -D <branch-name>
With this, we can successfully delete a local branch.
Delete a Git Branch Remotely
You can’t use the git branch command to delete a remote branch. Instead, you have to use the git push command with the --delete flag, followed by the name of the branch that you want to delete. You also need to specify the remote name (origin in this case) after "git push". The command is as follows:
Syntax
git push <remote-name> --delete <branch-name>
Here I will delete my test branch in my remote repository as shown below.

This command will delete the branch remotely. You can also use the shorthand:
Syntax
git push <remote-name> :<branch-name>
As you can see my remote branch is no more in my GitHub repo:

With this, we have successfully deleted our remote branch. A common error faced by many in this step is:
error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to 'git@repository_name'
This means that someone has already deleted the branch that you want to delete. If this happens you can use the following command to synchronize your branch list in the local environment:
Syntax
git fetch -p
The -p flag here means "prune". After fetching the branches which no longer exist remotely will be deleted in your local working environment.
Local Deletion vs Remote Deletion
Action | Local Deletion | Remote Deletion |
---|
Command | git branch -d branch-name | git push origin --delete branch-name |
Force Deletion | git branch -D branch-name | git push origin :branch-name |
Purpose | Removes branch from local repository | Removes branch from remote repository |
Safety | -d flag is safer, prevents unintentional deletion | Must ensure the branch is not needed anymore |
Conclusion
Removing the branches which are not important keeps the repository organized, improves collaboration, and makes sure a better development workflow. However, it's important to exercise caution when deleting branches, especially remote branches, as it may impact ongoing work by other team members. Therefore, always communicate with your team and verify that the branch is no longer required before proceeding with deletion.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
DBMS Tutorial â Learn Database Management System Database Management System (DBMS) is a software used to manage data from a database. A database is a structured collection of data that is stored in an electronic device. The data can be text, video, image or any other format.A relational database stores data in the form of tables and a NoSQL databa
7 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Introduction of ER Model The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
Computer Network Tutorial A Computer Network is a system where two or more devices are linked together to share data, resources and information. These networks can range from simple setups, like connecting two devices in your home, to massive global systems, like the Internet. Below are the main components of a computer netw
7 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read