How To Delete A Git Branch Locally - The Server Side
Maybe your like
Detailed steps to delete a single Git branch
To delete a local branch in Git, follow these steps:
- Open a Git BASH or a command prompt in the root of your Git repository.
- If necessary, use the git switch or checkout command to move off the branch you wish to delete.
- Issue the following command: git branch --delete <branchname>
- Run the git branch -a command to verify the local Git branch is deleted.
Git Error: Cannot delete branch
One rule of local Git branch deletion is that you cannot delete a branch that is currently checked out.
If you try to delete a local Git branch while it is currently checked out , you run into the Cannot delete branch error, as seen below:
git@DELETE /c/local/branch (main) $ git branch -a * main new-branch old-branch git@DELETE /c/local/branch (main) $ git branch --delete main error: Cannot delete branch 'main' checked out at 'C:/git/delete'Switch before you delete
In the above example, the user tried to delete the main Git branch while the it was checked out, which caused an error.
However, deletion of the local Git branches named new-branch or old-branch would succeed, as those branches are not in a checked-out state:
git@DELETE /c/local/branch (main) $ git branch --delete old-branch Deleted branch old-branch (was 44a55a1).Delete local Git branch command
The command to delete a local git branch can take one of the following two forms:
- git branch --delete old-branch
- git branch -d old-branch
The only difference between the, is that the second local branch delete Git command uses an abbreviated syntax. Both commands do the exact same thing.
Remote vs local Git branch deletes
Be advised that when you delete a local Git branch, the corresponding remote branch in a repository like GitHub or GitLab remains alive and active.
| Git, GitHub & GitHub Copilot Certification Made Easy |
|---|
| Want to get certified on the most popular AI, ML & DevOps technologies of the day? These five resources will help you get GitHub certified in a hurry.
Get certified in the latest AI, ML and DevOps technologies. Advance your career today. |
You must take further steps if the goal is to delete both local and remote branches.
When you delete a local Git branch, the deletion will not be reflected in remote repos like GitHub or GitLab.
One of the most commonly asked questions on StackOverflow is this:
How do you delete both local and remote Git branches?🤔
Here's a 60 second short on how to do it.#GitHub #DevOps pic.twitter.com/a52PC7e3vH
— Cameron McKenzie | Docker | GitHub | AWS | Java (@cameronmcnz) August 22, 2023
Remote Git branch deletion
To remove a remote Git branch in a repository such as GitHub or GitLab, the git push origin command is used with the --delete switch and a reference to the branch to delete.
For example, the following command will delete a remote branch named old-branch:
git@DELETE /c/local/remote/branch (main) git push origin --delete old-branch * [deleted] alpha Remote Git branch delete alpha successfulThe following image demonstrates how to delete local and remote Git branches:
These commands will delete both local and remote Git branches.

Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.
Next Steps
The AWS Solutions Architect Book of Exam Questions by Cameron McKenzie
So what’s next? A great way to secure your employment or even open the door to new opportunities is to get certified. If you’re interested in AWS products, here are a few great resources to help you get Cloud Practitioner, Solution Architect, Machine Learning and DevOps certified from AWS:
- AWS Certified Cloud Practitioner Book of Exam Questions
- AWS Certified Developer Associate Book of Exam Questions
- AWS Certified AI Practitioner Book of Exam Questions & Answers
- AWS Certified Machine Learning Associate Book of Exam Questions
- AWS Certified DevOps Professional Book of Exam Questions
- AWS Certified Data Engineer Associate Book of Exam Questions
- AWS Certified Solutions Architect Associate Book of Exam Questions
Put your career on overdrive and get AWS certified today!
Tag » How To Delete Local Branch Git
-
How To Delete A Git Branch Both Locally And Remotely
-
How Do I Delete A Local Branch In Git? | Learn Version Control With Git
-
Git Delete Branch How-To, For Both Local And Remote | CloudBees
-
How Do I Delete A Git Branch Locally And Remotely? - Stack Overflow
-
How To Delete Local Branches In Git - GitKraken
-
Delete A Git Branch Locally And Remotely - Baeldung
-
How To Clean Up Git Branches - Devconnected
-
How To Delete A Git Branch Locally And Remotely | Level Up Coding
-
How To Delete Remote Branches In Git
-
Git Branch Delete Local Example - YouTube
-
Delete A Git Branch - Azure Repos - Microsoft Learn
-
Delete A Local And A Remote GIT Branch | By Aram Koukia
-
How To Delete A Git Branch Locally And Remotely? - Medium
-
How To Delete Local/Remote Git Branches - EfficientCoder