How do I delete a Git branch both locally and remotely?
I want to delete a branch both locally and on my remote project fork on GitHub.
Failed Attempts to Delete Remote Branch
$ git branch -d remotes/origin/bugfix
error: branch 'remotes/origin/bugfix' not found.
$ git branch -d origin/bugfix
error: branch 'origin/bugfix' not found.
$ git branch -rd origin/bugfix
Deleted remote branch origin/bugfix (was 2a14ef7).
$ git push
Everything up-to-date
$ git pull
From github.com:gituser/gitproject
* [new branch] bugfix -> origin/bugfix
Already up-to-date.
What do I need to do differently to successfully delete the remotes/origin/bugfix
branch both locally and on GitHub?
Executive Summary
$ git push -d <remote_name> <branch_name>
$ git branch -d <branch_name>
Note that in most cases the remote name is origin
.
Delete Local Branch
To delete the local branch use one of the following:
$ git branch -d branch_name
$ git branch -D branch_name
Note: The -d
option is an alias for --delete
, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D
, which is an alias for --delete --force
, which deletes the branch "irrespective of its merged status." [Source: man git-branch
]
Delete Remote Branch [Updated on 8-Sep-2017]
As of Git v1.7.0, you can delete a remote branch using
$ git push <remote_name> --delete <branch_name>
which might be easier to remember than
$ git push <remote_name> :<branch_name>
which was added in Git v1.5.0 "to delete a remote branch or a tag."
Starting on Git v2.8.0 you can also use git push
with the -d
option as an alias for --delete
.
Therefore, the version of Git you have installed will dictate whether you need to use the easier or harder syntax.
Delete Remote Branch [Original Answer from 5-Jan-2010]
From Chapter 3 of Pro Git by Scott Chacon:
Deleting Remote Branches
Suppose you're done with a remote branch — say, you and your collaborators are finished with a feature and have merged it into your remote's master branch (or whatever branch your stable codeline is in). You can delete a remote branch using the rather obtuse syntax git push [remotename] :[branch]
. If you want to delete your serverfix branch from the server, you run the following:
$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
- [deleted] serverfix
Boom. No more branch on your server. You may want to dog-ear this page, because you'll need that command, and you'll likely forget the syntax. A way to remember this command is by recalling the git push [remotename] [localbranch]:[remotebranch]
syntax that we went over a bit earlier. If you leave off the [localbranch]
portion, then you're basically saying, “Take nothing on my side and make it be [remotebranch]
.”
I issued git push origin :bugfix
and it worked beautifully. Scott Chacon was right—I will want to dog ear that page (or virtually dog ear by answering this on Stack Overflow).
Then you should execute this on other machines
git fetch --all --prune
to propagate changes.
Matthew's answer is great for removing remote branches and I also appreciate the explanation, but to make a simple distinction between the two commands:
To remove a local branch from your machine:
git branch -d {the_local_branch}
(use -D
instead to force deleting the branch without checking merged status)
To remove a remote branch from the server:
git push origin --delete {the_remote_branch}
Reference: https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote
The Short Answers
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch:
git push origin --delete <branch> # Git version 1.7.0 or newer
git push origin :<branch> # Git versions older than 1.7.0
Deleting a local branch:
git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force delete un-merged branches
Deleting a local remote-tracking branch:
git branch --delete --remotes <remote>/<branch>
git branch -dr <remote>/<branch> # Shorter
git fetch <remote> --prune # Delete multiple obsolete tracking branches
git fetch <remote> -p # Shorter
The Long Answer: there are 3 different branches to delete!
When you're dealing with deleting branches both locally and remotely, keep in mind that there are 3 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
Additional Details
The following sections describe additional details to consider when deleting your remote and remote-tracking branches.
Pushing to delete remote branches also deletes remote-tracking branches
Note that deleting the remote branch X
from the command line using a git push
will also delete 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
, though 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
Pruning the obsolete local remote-tracking branch origin/X
If you didn't delete your remote branch X
from the command line (like above), then your local repo 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).
Alternative to above automatic pruning for obsolete remote-tracking branches
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
See Also
上一篇: 'git pull'和'git fetch'有什么区别?
下一篇: 如何在本地和远程删除Git分支?