Delete Local and Remote Git Branches

Published: August 17, 2020 by Author's Photo Shane Rainville | Reading time: 2 minutes
git
Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

No matter which strategy you follow with version control, whether it be gitflow or some custom branching scheme, you will eventually need to remove branches from your Git repositories.

Remove Local Branches

To remove a branch from your local Git repository you the git branch command with the -d or -D flag.

git branch -D <branch-name>

For example, to delete a branch with the name feature/user-profile, you would run the following command:

git branch -D feature/user-profile

If the branch was deleted successfully, the output of the command will show the name of the deleted branch and it’s highest commit ID.

Deleted branch feature/user-profile (was 87beaa4)

The git branch -D command only applies to local repositories. A different method is required for deleting remote branches from repositories.

Remove Remote Branches

Fortunately, the git branch -D command does not remove a branch from your local repository and any remote repositories. In order to delete a remote repository you must perform a git push with the --delete flag.

git push --delete <remote> <branch-name>

For example, if your remote repository is named origin and the branch you would like to delete is named feature/password-reset-form, you will execute the following command.

git push --delete origin feature/password-reset-form

Which will output the following, if the branch is successfully deleted.

remote: This repository moved. Please use the new location:
remote:   https://github.com/cloudyyuts/demo-app.git
To https://github.com/cloudytuts/demo-app.git
 - [deleted]         feature/password-reset-form
Last updated on August 17, 2020 by Shane Rainville: Fix typos and grammar 591191981f7cc28a26f41f02745ecc0ba7650649
Author Photo
Blogger, Developer, pipeline builder, cloud engineer, and DevSecOps specialist. I have been working in the cloud for over a decade and running containized workloads since 2012, with gigs at small startups to large financial enterprises.

How to Squash Your Commits and Why Do It

Publised August 18, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

Git Branch Cheat Sheet

Publised August 17, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Deploy Jekyll on Kubernetes

Publised September 15, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Update Kubernetes Deployments

Publised September 11, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Configure Node-based apps in Kubernetes

Publised September 9, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Backup and Restore MongoDB Deployment on Kubernetes

Publised September 3, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Immediately Start Kubernetes CronJobs Manually

Publised September 2, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.

How to Deploy Java Apps with Tomcat on Kubernetes

Publised September 1, 2020 by Shane Rainville

Learn how to delete your Git branches on your local filesystem and from a remote repository, and keep your repository clean from branch clutter.