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.
What do I need to do differently to successfully delete theremotes/origin/bugfix
branch both locally and on GitHub?
Note that in most cases the remote name is origin
.
To delete the local branch use one of the following:
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
]
As of Git v1.7.0, you can delete a remote branch using
which might be easier to remember than
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.
From Chapter 3 of Pro Git by Scott Chacon:
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:
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
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
If you want more detailed explanations of the following commands, then see the long answers in the next section.
Deleting a remote branch:
Deleting a local branch:
Deleting a local remote-tracking branch:
When you're dealing with deleting branches both locally and remotely, keep in mind that there are 3 different branches involved:
The original poster used
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
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 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:
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:
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:
for deleting the remote branch:
for deleting the local branch, you have three ways:
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:
You can also use the following to delete the remote branch.
Which does the same thing as
but it may be easier to remember.
If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
Deleting the local branch:
Deleting the remote branch:
Tip: When you delete branches using
or
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
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 command:
(here example
is the branch name)
And after that delete remote branch using command:
Another approach is
WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,
will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).
I use the following in my Bash settings:
Then you can call:
Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.
Relevant blog post: Create and delete branches
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
:
Alternatively, you can add this to your global config from the command line using
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).
To delete your branch locally and remotely
Checkout to master branch - git checkout master
Delete your remote branch - git push origin --delete <branch-name>
Delete your local branch - git branch --delete <branch-name>
Delete locally:
To delete a local branch, you can use:
To delete a branch forcibly, use -D
instead of -d
.
Delete remotely:
There are two options:
I would suggest you use the 2nd way as it is more intuitive.
You can also do this using git remote prune origin
:
It prunes and deletes remote-tracking branches from a git branch -r
listing.
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:
I find that I also use the publish
and track
commands quite often.
Deleting Branches
Let's assume our work on branch "contact-form" is done and we've already integrated it into "master". Since we don't need it anymore, we can delete it (locally):
And for deleting the remote branch:
Delete remote branch
git push origin :<branchname>
Delete local branch
git branch -D <branchname>
Delete local branch steps:
One liner command delete both local, and remote:
D=branch-name; git branch -D $D; git push origin :$D
or add the alias below to your ~/.gitconfig; usage: git kill branch-name
Simply say:
Now you can do it with the GitHub Desktop app.
After launching the app
To delete Locally - (Normal),
If your branch in rebasing/merging progress and that was not done properly means, you will get an error Rebase/Merge in progress
so in that case, you won't be able to delete your branch.
So either your need to solve rebasing/merging otherwise you can do force Delete by using,
To delete in Remote:
can do the same using ,
Graphical Representation,
is easier to remember than
This won't work if you have a tag with the same name as the branch on the remote:
In that case you need to specify that you want to delete the branch, not the tag:
Similarly, to delete the tag instead of the branch you would use:
Many of the other answers will lead to errors/warnings. This approach is relatively fool proof although you may still need git branch -D branch_to_delete
if it's not fully merged into some_other_branch
, for example.
Remote pruning isn't needed if you deleted the remote branch. It's only used to get the most up to date remotes available on a repo you're tracking. I've observed git fetch
will add remotes, not remove them. Here's an example of when git remote prune origin
will actually do something:
User A does the steps above. User B would run the following commands to see the most up to date remote branches
I got sick of googling for this answer, so I took a similar approach
to the answer that crizCraig posted earlier.
Added the following to my Bash profile:
Then every time I'm done with a branch (merged into master
, for example) I run the following in my terminal:
...which then deletes my-branch-name
from origin
as as well as locally.
If you are sure you want to delete it, run
Now to clean up deleted remote branches run
Before executing
make sure you determine first what the EXACT name of the remote branch is by executing:
This will tell you what to enter EXACTLY for <branch>
value. (branch
is case sensitive!)
Mashup of all the other answers. Requires Ruby 1.9.3+, tested only on OS X.
Call this file git-remove
, make it executable, and put it in your path. Then use, for example, git remove temp
.
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?