I want to delete a branch both locally and remotely.
$ git branch -d
I got sick of googling for this answer, so I took a similar approach to the answer that crizCraig posted earlier.
I added the following to my Bash profile:
function gitdelete(){
git push origin --delete $1
git branch -D $1
}
Then every time I'm done with a branch (merged into master
, for example) I run the following in my terminal:
gitdelete my-branch-name
...which then deletes my-branch-name
from origin
as as well as locally.
If you want more detailed explanations of the following commands, then see the long answers in the next section.
git push origin --delete <branch> # Git version 1.7.0 or newer
git push origin -d <branch> # Shorter version (Git 1.7.0 or newer)
git push origin :<branch> # Git versions older than 1.7.0
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force-delete un-merged branches
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter
git fetch <remote> --prune # Delete multiple obsolete remote-tracking branches
git fetch <remote> -p # Shorter
When you're dealing with deleting branches both locally and remotely, keep in mind that there are three different branches involved:
X
.X
.origin/X
that tracks the remote branch X
.The original poster used:
git branch -rd origin/bugfix
Which only deleted his local remote-tracking branch origin/bugfix
, and not the actual remote branch bugfix
on origin
.
To delete that actual remote branch, you need
git push origin --delete bugfix
The following sections describe additional details to consider when deleting your remote and remote-tracking branches.
Note that deleting the remote branch X
from the command line using a git push
will also remove the local remote-tracking branch origin/X
, so it is not necessary to prune the obsolete remote-tracking branch with git fetch --prune
or git fetch -p
. However, it wouldn't hurt if you did it anyway.
You can verify that the remote-tracking branch origin/X
was also deleted by running the following:
# View just remote-tracking branches
git branch --remotes
git branch -r
# View both strictly local as well as remote-tracking branches
git branch --all
git branch -a
If you didn't delete your remote branch X
from the command line (like above), then your local repository will still contain (a now obsolete) remote-tracking branch origin/X
. This can happen if you deleted a remote branch directly through GitHub's web interface, for example.
A typical way to remove these obsolete remote-tracking branches (since Git version 1.6.6) is to simply run git fetch
with the --prune
or shorter -p
. Note that this removes all obsolete local remote-tracking branches for any remote branches that no longer exist on the remote:
git fetch origin --prune
git fetch origin -p # Shorter
Here is the relevant quote from the 1.6.6 release notes (emphasis mine):
"git fetch" learned
--all
and--multiple
options, to run fetch from many repositories, and--prune
option to remove remote tracking branches that went stale. These make "git remote update" and "git remote prune" less necessary (there is no plan to remove "remote update" nor "remote prune", though).
Alternatively, instead of pruning your obsolete local remote-tracking branches through git fetch -p
, you can avoid making the extra network operation by just manually removing the branch(es) with the --remote
or -r
flags:
git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter
Tip: When you delete branches using
git branch -d <branchname> # Deletes local branch
or
git push origin :<branchname> # Deletes remote branch
only the references are deleted. Even though the branch is actually removed on the remote, the references to it still exists in the local repositories of your team members. This means that for other team members the deleted branches are still visible when they do a git branch -a
.
To solve this, your team members can prune the deleted branches with
git remote prune <repository>
This is typically git remote prune origin
.
This is simple: Just run the following command:
To delete a Git branch both locally and remotely, first delete the local branch using this command:
git branch -d example
(Here example
is the branch name.)
And after that, delete the remote branch using this command:
git push origin :example
I use the following in my Bash settings:
alias git-shoot="git push origin --delete"
Then you can call:
git-shoot branchname
It's very simple:
To delete the remote branch
git push -d origin <branch-name>
Or
git push origin :<branch-name>
To forcefully delete local branch
git branch -D <branch-name>