Git: Deleting branches
How to delete branches#
Deleting branches is one of the basic git operations. Branches may exist locally (on your machine) or remotely (on a remote repository). You may want to delete these branches from time to time to clean up the repository.
There are separate commands for deleting branches locally and on remote.
Deleting local branch#
To begin deleting a local branch, first ensure you are not on the branch you want to delete. Then, run this command:
$ git branch -d <branch>
It will delete the branch if it has been merged to master
.
What if you want to delete a branch, whether it has been merged to master
or not?
Use the -D
option:
$ git branch -D <branch>
It will delete the local branch forcefully.
Deleting remote branch#
Deleting remote branches require a push
operation. Here is how you do it:
$ git push origin --delete <branch>
Alternatively, you can also use:
$ git push origin :<branch>
Summary#
Local git branches can be deleted using the git branch
command with the -d
or -D
option. Remote git branches can be deleted using the git push
command with the --delete
option or :
.