How To Delete A Git Branch Locally - The Server Side

Detailed steps to delete a single Git branch

To delete a local branch in Git, follow these steps:

  1. Open a Git BASH or a command prompt in the root of your Git repository.
  2. If necessary, use the git switch or checkout command to move off the branch you wish to delete.
  3. Issue the following command: git branch --delete <branchname>
  4. 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.

  • Master vibe coding and prove it with the GitHub Copilot certification
  • Prove you know how CI/CD with the GitHub Action certification
  • Prove you know your way around Git and GitHub with the GitHub Foundation certification.
  • Show you know how to work on a team with a Scrum Master cert or Product Owner credential
  • Let employers know you understand LLMs and Agentic AI with a GCP AI Leader certification

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.

git no upstream branch error fix

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 successful

The following image demonstrates how to delete local and remote Git branches:

delete git remote local branch remove

These commands will delete both local and remote Git branches.

Cameron McKenzie

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

AWS Solutions Architect Book

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