I want to delete a branch both locally and remotely.
$ git branch -d
Now you can do it with the GitHub Desktop application.
After launching the application
Switch to the branch you would like to delete
From the "Branch" menu, select, "Unpublish...", to have the branch deleted from the GitHub servers.
From the "Branch" menu, select, 'Delete "branch_name"...', to have the branch deleted off of your local machine (AKA the machine you are currently working on)
Before executing
git branch --delete <branch>
make sure you determine first what the exact name of the remote branch is by executing:
git ls-remote
This will tell you what to enter exactly for <branch>
value. (branch
is case sensitive!)
Use:
git push origin :bugfix # Deletes remote branch
git branch -d bugfix # Must delete local branch manually
If you are sure you want to delete it, run
git branch -D bugfix
Now to clean up deleted remote branches run
git remote prune origin
If you want to complete both these steps with a single command, you can make an alias for it by adding the below to your ~/.gitconfig
:
[alias]
rmbranch = "!f(){ git branch -d ${1} && git push origin --delete ${1}; };f"
Alternatively, you can add this to your global configuration from the command line using
git config --global alias.rmbranch \
'!f(){ git branch -d ${1} && git push origin --delete ${1}; };f'
NOTE: If using -d
(lowercase d), the branch will only be deleted if it has been merged. To force the delete to happen, you will need to use -D
(uppercase D).
In addition to the other answers, I often use the git_remote_branch tool. It's an extra install, but it gets you a convenient way to interact with remote branches. In this case, to delete:
grb delete branch
I find that I also use the publish
and track
commands quite often.
For deleting the remote branch:
git push origin --delete <your_branch>
For deleting the local branch, you have three ways:
1: git branch -D <branch_name>
2: git branch --delete --force <branch_name> # Same as -D
3: git branch --delete <branch_name> # Error on unmerge
Explain: OK, just explain what's going on here!
Simply do git push origin --delete
to delete your remote branch only, add the name of the branch at the end and this will delete and push it to remote at the same time...
Also, git branch -D
, which simply delete the local branch only!...
-D
stands for --delete --force
which will delete the branch even it's not merged (force delete), but you can also use -d
which stands for --delete
which throw an error respective of the branch merge status...
I also create the image below to show the steps: