How do I delete a Git branch both locally and remotely?
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.
$ 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 theremotes/origin/bugfix
branch both locally and on GitHub?
remotes/origin/bugfix
Note: for Git 2.5+ (Q2 2015), the exact message will be "
deleted remote-tracking branch
": see github.com/git/git/commit/…– VonC
May 25 '15 at 14:57
deleted remote-tracking branch
39 Answers
39
Executive Summary
$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>
Note that in most cases the remote name is origin
.
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
]
-d
--delete
-D
--delete --force
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
.
git push
-d
--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:
git push [remotename] :[branch]
$ 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]
.”
git push [remotename] [localbranch]:[remotebranch]
[localbranch]
[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).
git push origin :bugfix
Then you should execute this on other machines
git fetch --all --prune
to propagate changes.
If you know the syntax
git push origin local_branch:remote_branch
, then the syntax to delete a branch with git push origin :remote_branch
is kind of cute. There's a void before the :
– Marc-André Lafortune
May 11 '12 at 4:05
git push origin local_branch:remote_branch
git push origin :remote_branch
:
Don't forget to do a
git fetch --all --prune
on other machines after deleting the remote branch on the server. ||| After deleting the local branch with git branch -d
and deleting the remote branch with git push origin --delete
other machines may still have "obsolete tracking branches" (to see them do git branch -a
). To get rid of these do git fetch --all --prune
.– Trevor Boyd Smith
May 27 '15 at 16:51
git fetch --all --prune
git branch -d
git push origin --delete
git branch -a
git fetch --all --prune
in addition to @TrevorBoydSmith's
git branch -a
to view all branches, you can also use git branch -r
to view remote branches only. see also git remote show origin
- source: gitready.com/intermediate/2009/02/13/list-remote-branches.html– Sandra
Sep 9 '15 at 9:53
git branch -a
git branch -r
git remote show origin
I had to run
git branch -D Branch_Name
to get rid of the local branch– Kolob Canyon
Mar 10 '16 at 1:32
git branch -D Branch_Name
@KolobCanyon You only have to use -D if the branch has not been merged into another branch.
– BTRUE
Apr 5 '16 at 16:27
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)
git branch -d the_local_branch
-D
To remove a remote branch from the server:
git push origin --delete the_remote_branch
git push origin --delete the_remote_branch
Reference: https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote
@megido well
-D
force deletes, -d
gives you a warning if it's not already merged in.– TankorSmash
Nov 6 '14 at 19:04
-D
-d
If your local branch is not merge with master and ran
'git branch -d your_branch
then you will error like error: The branch 'your_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D your_branch'.
– geeks
Oct 31 '15 at 12:59
'git branch -d your_branch
error: The branch 'your_branch' is not fully merged. If you are sure you want to delete it, run 'git branch -D your_branch'.
I would suggest using -d instead of -D because it is safer. If -d fails due to unmerged commits then you need to assess that and if it is definitely OK to remove then use -D.
– rooby
Feb 2 '16 at 3:47
Others with repository clones where remote branches have been removed should run
git remote prune <name>
(e.g. git remote prune origin
) in order to locally remove stale branches that no longer exist in the remote.– code_dredd
Apr 18 '16 at 23:07
git remote prune <name>
git remote prune origin
If your branch in your fork (not always origin), use the proper repository. e.g. git push myRepo :mrbranchtodelete
– Dhanuka777
May 19 '16 at 0:53
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
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
.
origin/bugfix
bugfix
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.
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.
X
git push
origin/X
git fetch --prune
git fetch -p
You can verify that the remote-tracking branch origin/X
was also deleted by running the following:
origin/X
# 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 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.
X
origin/X
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
--prune
-p
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).
--all
--multiple
--prune
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 fetch -p
--remote
-r
git branch --delete --remotes origin/X
git branch -dr origin/X # Shorter
From your illustration, I can see there are local clone repo and remote origin repo. So there are at least two physical branches. Where is the third branch to delete? Is the third branch only a pointer pointing to a commit in the local clone repo?
– huggie
Feb 18 '16 at 2:00
@huggie that's pretty much correct. Branches in Git are just bookmarks attached to commits. So in my graphs above, there are
X
and origin/X
bookmarks in the local clone (2 branches), and then there is X
on the remote (making 3 branches).– user456814
Feb 23 '16 at 7:33
X
origin/X
X
+1 for the remote tracking branch. This branch is what causes issues when you clone someone else's branch. It keeps on tracking your commits and asking you if you want to push to that person's branch.
– Kermit_ice_tea
Jun 21 '17 at 19:48
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...
git push origin --delete
Also, git branch -D
, which simply delete the local branch ONLY!...
git branch -D
-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...
-D
--delete --force
-d
--delete
I also create the image below to show the steps:
git branch -a
will display local and remote branches.It will be help for you diagram introduce.– LoranceChen
Jul 27 '17 at 3:01
git branch -a
note that if you are setting on the branch you want to delete, you need to checkout a branch other than the one you need to delete (eg: master) before deleting the local branch.
– BaDr Amer
May 28 at 8:43
[off topic] how did you create the above amazing image?
– Lucas Caton
Jun 21 at 7:24
You can also use the following to delete the remote branch.
git push --delete origin serverfix
Which does the same thing as
git push origin :serverfix
but it may be easier to remember.
...and safer to use :O
– cst1992
Nov 29 '17 at 7:31
You forgot the part about deleting the local branch which can be done by:
git branch -d <local_branch>
or git branch -D <local_branch>
for force deleting– Amit Dash
Feb 14 at 4:23
git branch -d <local_branch>
git branch -D <local_branch>
If you want to delete a branch, first checkout to the branch other than the branch to be deleted.
git checkout other_than_branch_to_be_deleted
Deleting the local branch:
git branch -D branch_to_be_deleted
Deleting the remote branch:
git push origin --delete branch_to_be_deleted
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
.
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
.
git remote prune origin
You should clarify that the above
git push
operation deletes the local branch and the remote branch.– Annika Backstrom
May 21 '13 at 13:51
git push
Note that
git remote prune
is a somewhat obsolete way to remove obsolete remote-tracking branches, the newer way to do it is to use git fetch --prune
or git fetch -p
.– user456814
Jun 11 '14 at 16:30
git remote prune
git fetch --prune
git fetch -p
@RRMadhav, indeed you won't see the deleted branch after deleting it since the reference to the remote branch will be removed for you locally. Anyone else on your team that has checked out that branch will still have that reference and will still see it unless they prune the branch.
– pfrenssen
Dec 5 '14 at 14:27
git branch -D <name-of-branch>
git branch -D -r origin/<name-of-branch>
git push origin :<name-of-branch>
Note that
-D
forces the deletion. It's always better to use -d
, which will remind if you need to do something dangerous.– Jonathon Reinhart
Jan 10 '15 at 1:08
-D
-d
ahahah :) it's up to you: use -d if you want to see git crying or -D if you want to cry.
– Felipe
Feb 13 '15 at 11:21
This is simple: Just run the following command:
To delete a Git branch both locally and remotely, first delete the local branch using command:
git branch -d example
(here example
is the branch name)
example
And after that delete remote branch using command:
git push origin :example
Another approach is
git push --prune origin
WARNING: This will delete all remote branches that do not exist locally. Or more comprehensively,
git push --mirror
will effectively make the remote repository look like the local copy of the repository (local heads, remotes and tags are mirrored on remote).
git push --prune origin
looks dangerous...– Nate
Jun 28 '13 at 22:37
git push --prune origin
git push --prune origin didn't do anything for me on gitlab: git clone git://repo.git; git branch -d -r origin/some-branches; git push --prune origin; yields: Everything up-to-date; git fetch; brings locally deleted branches back; git push --mirror; now they are really gone!
– eMBee
Oct 8 '15 at 16:46
I use the following in my Bash settings:
alias git-shoot="git push origin --delete"
Then you can call:
git-shoot branchname
I ended up just add the alias "shoot" into my .gitconfig shoot = push origin --delete
– hdost
Dec 4 '14 at 18:06
If your origin is a Atlassian Stash and the branch is set as the default, you will get an error "By default, deleting the current branch is denied...". I had to change the default branch in Stash to point to another branch before I could delete.
– neoscribe
Dec 12 '14 at 0:29
This is perfectly simple as you've done it, but fyi git also lets you make custom commands. Put
git push origin --delete $1
in a file on your path called git-shoot
and git shoot branchname
will work too.– mahemoff
Oct 14 '15 at 7:09
git push origin --delete $1
git-shoot
git shoot branchname
Since January 2013, GitHub included a Delete branch button next to each branch in your "Branches" page.
Relevant blog post: Create and delete branches
I only started using Github this year, so I was wondering why this was such a highly rated question, and why none of the top answers were suggesting to just delete it from the Github web interface! Interesting that it's only a recent addition.
– Cam Jackson
Sep 11 '13 at 12:18
I was going to point this one out. Note that the button won't delete your local branch... see this answer for how to do that: stackoverflow.com/a/10999165/901641
– ArtOfWarfare
Oct 29 '13 at 14:02
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
:
~/.gitconfig
[alias]
rmbranch = "!f() git branch -d $1 && git push origin --delete $1; ;f"
Alternatively, you can add this to your global config 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).
-d
-D
This is what I was looking for. My own shell function alias didn't work (Unexpected EOF) and I couldn't figure out why, but this works great! The only change I made was replacing
&&
with ;
so that even if the first command fails the second will still execute (sometimes only local or only remote exists).– user1021726
Dec 16 '14 at 8:55
&&
;
To delete your branch locally and remotely
Checkout to master branch - git checkout master
git checkout master
Delete your remote branch - git push origin --delete <branch-name>
git push origin --delete <branch-name>
Delete your local branch - git branch --delete <branch-name>
git branch --delete <branch-name>
Delete locally:
To delete a local branch, you can use:
git branch -d branch_name
To delete a branch forcibly, use -D
instead of -d
.
-D
-d
git branch -D branch_name
Delete remotely:
There are two options:
git push origin :branchname
git push origin --delete branchname
I would suggest you use the 2nd way as it is more intuitive.
You can also do this using git remote prune origin
:
git remote prune origin
$ git remote prune origin
Pruning origin
URL: git@example.com/yourrepo.git
* [pruned] origin/some-branchs
It prunes and deletes remote-tracking branches from a git branch -r
listing.
git branch -r
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.
publish
track
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):
$ git branch -d contact-form
And for deleting the remote branch:
git push origin --delete contact-form
One liner command delete both local, and remote:
D=branch-name; git branch -D $D; git push origin :$D
D=branch-name; git branch -D $D; git push origin :$D
or add the alias below to your ~/.gitconfig; usage: git kill branch-name
git kill branch-name
[alias]
kill = "!f() git branch -D "$1"; git push origin --delete "$1"; ;f"
⚠️ Use
git branch -D
carefully in a script, since it force-deletes a branch without checking it has been merged. Use -d
to be safe.– caesarsol
Mar 13 '17 at 14:05
git branch -D
-d
Delete remote branch
git push origin :<branchname>
git push origin :<branchname>
Delete local branch
git branch -D <branchname>
git branch -D <branchname>
Delete local branch steps:
Does the remote branch deletion requires "git push" afterwards ?
– Samitha Chathuranga
Feb 17 '16 at 8:32
@SamithaChathuranga no,
git push origin :<branchname>
already pushes an 'empty` branch to the remote (hence deletes the remote branch)– Michał Szajbe
Jun 9 '17 at 22:20
git push origin :<branchname>
Simply say:
git branch -d <branch-name>
git push origin :<branch-name>
This works if its your own branch. But if you are pruning all unneeded branches in the repo (some of which aren't yours) it wouldn't suffice
– Kermit_ice_tea
Jun 30 '16 at 22:23
Now you can do it with the GitHub Desktop app.
After launching the app
I didn't downvote, but my thinking is that it isn't substantively helping. The question is obviously asking for a more commandline type answer without having to use an external program, if people were clicking into here, they likely won't be looking for a github for desktop ways.
– Daemedeor
Nov 6 '15 at 11:51
@Daemedeor , I dissagree. In 2010 when the OP asked the question, the UI way of doing it didn't exist and the only option was command line. To indicate that you want a command line only option it should be stated in the question or with the tag, command-line-interface, which in this case is no present.
– Eric
Nov 6 '15 at 16:00
This is exactly what I was looking for too, the Github Desktop documentation doesn't even mention the feature and this is where I ended up when searching for an answer as to what it does. It may not be "the answer" but it is useful.
– Daniel
Dec 5 '15 at 4:00
The git command for deleting a remote branch sucks and I tend to forget it (both new and old). Luckily there are GUI tools that have the option. Git Gui, TortoiseGit and GitHub Desktop have it - I wish Git Extensions had this functionality too. Anyway, what I remember is to start Git Gui from within Git Extensions when I need to delete a remote branch.
– vezenkov
Mar 25 '16 at 21:59
To delete Locally - (Normal),
git branch -d my_branch
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.
Rebase/Merge in progress
So either your need to solve rebasing/merging otherwise you can do force Delete by using,
git branch -D my_branch
To delete in Remote:
git push --delete origin my_branch
can do the same using ,
git push origin :my_branch # easy to remember both will do the same.
Graphical Representation,
git push origin --delete branchName
is easier to remember than
git push origin :branchName
This won't work if you have a tag with the same name as the branch on the remote:
$ git push origin :branch-or-tag-name
error: dst refspec branch-or-tag-name matches more than one.
error: failed to push some refs to 'git@github.com:SomeName/some-repo.git'
In that case you need to specify that you want to delete the branch, not the tag:
git push origin :refs/heads/branch-or-tag-name
Similarly, to delete the tag instead of the branch you would use:
git push origin :refs/tags/branch-or-tag-name
This is fine, but people really shouldn't be naming their branches and tags with the same name and same naming scheme in the first place.
– user456814
Jul 29 '14 at 10:00
Well, my scenario was that I was converting a branch to a tag and it made sense for the tag to have the same name as the branch. By converting I mean merging branch B to A and tagging the last commit in branch B with tag B so that after deleting branch B it can still be easily restored by simply checking out tag B.
– Amiramix
Jul 30 '14 at 11:59
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.
git branch -D branch_to_delete
some_other_branch
git checkout some_other_branch
git push origin :branch_to_delete
git branch -d branch_to_delete
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:
git fetch
git remote prune origin
User A does the steps above. User B would run the following commands to see the most up to date remote branches
git fetch
git remote prune origin
git branch -r
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:
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:
master
gitdelete my-branch-name
...which then deletes my-branch-name
from origin
as as well as locally.
my-branch-name
origin
expanding on this,
--delete "$@"
and -D "$@"
instead of $1
will handle it for multiple branches.– kunl
Jun 27 '16 at 13:15
--delete "$@"
-D "$@"
$1
I suggest running
git branch -d
(with lowercase 'd') first to ensure changes have been merged, and then push if successful (put &&
in between commands)– bryn
Jul 19 '16 at 14:17
git branch -d
&&
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
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!)
<branch>
branch
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
.
git-remove
git remove temp
#!/usr/bin/env ruby
require 'io/console'
if __FILE__ == $0
branch_name = ARGV[0] if (ARGV[0])
print "Press Y to force delete local and remote branch #branch_name..."
response = STDIN.getch
if ['Y', 'y', 'yes'].include?(response)
puts "nContinuing."
`git branch -D #branch_name`
`git branch -D -r origin/#branch_name`
`git push origin --delete #branch_name`
else
puts "nQuitting."
end
end
one-letter get courtesy of this answer: stackoverflow.com/a/8072675/8047
– Dan Rosenstark
Nov 19 '13 at 21:04
@chhh then you need to extend this functionality to make this a variable instead of an assumption.
– Dan Rosenstark
Dec 5 '14 at 16:44
sorry, but install Ruby for that kind of work ? More logical is implementation on bash, which will work out of box.
– Reishin
May 21 '15 at 19:37
@Reishin Ruby is installed on the box just like Bash, at least on OSX. Please see: stackoverflow.com/questions/2342894/…, where this topic has been discarded as opinion-based by SO.
– Dan Rosenstark
May 21 '15 at 20:03
@Yar this link is out of the context and have a more broader scope. I tell only about git and as topic is not originated only for OSX, that choose is strange for other systems (e.g. *UNIX, Windows)
– Reishin
May 21 '15 at 20:33
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?
Moderator note: If you intend to answer this question, do note that there are already 40 answers posted. Will your new answer add any substantial value?
– Robert Harvey♦
Jun 11 '14 at 16:10