Delete Local and Remote Git Branches
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
Follow Us