How to Delete Both Local and Remote Branches in Git
Local and remote branches are completely separate objects in Git; deleting one wouldn’t delete the other, even if there is a tracking connections between them. So, if you need to delete both local and remote branches, delete them separately. Let’s have a look how to do it.
How to Delete a Local Branch in Git
Make sure you are currently not on the branch you want to delete. For checking out from the current branch run the following command:
git checkout <branchname>
The -d or -D option (shortcut for --delete) will delete the <branchname>. You can delete more than one branch.
git branch -d<branchname>
If the branch has a reflog, it will be deleted either.
The -f or --force flag in combination with -d (or --delete), allows deleting the branch containing unmerged changes.
Note: Use the -f flag very carefully, as it easily may cause data loss.
How to Delete a Remote Branch in Git
To delete a remote branch use git push command with the "--delete" flag:
git push <remote_name> --delete <branch_name>
Note that in most cases remote branch name is origin. So your command will most probably look like:
git push origin --delete <branchname>
In git versions older than 1.7.0 the command will be as follows:
git push origin :<branch>
How to Delete Remote-Tracking Branch
To delete remote-tracking branch run command:
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch>
To delete multiple obsolete tracking branches run command:
git fetch <remote> --prune
git fetch <remote> -p