780

Let's say I had a branch named coolbranch in my repository.

Now, I decided to delete it (both remotely and locally) with:

git push origin :coolbranch
git branch -D coolbranch

Great! Now the branch is really deleted.

But when I run

git branch -a

I still get:

remotes/origin/coolbranch

Something to notice, is that when I clone a new repository, everything is fine and git branch -a doesn't show the branch.

I want to know - is there a way to delete the branch from the branch -a list without cloning a new instance?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yonix
  • 11,665
  • 7
  • 34
  • 52
  • 1
    Related: [Delete a Git branch both locally and remotely](http://stackoverflow.com/q/2003505/456814). –  Oct 18 '15 at 01:32
  • 130
    If you `git fetch -p` (or `git pull -p`) then remote branches will be pruned. – yoyo Jul 07 '16 at 22:07

6 Answers6

941

git remote prune origin will remove all such stale branches. That's probably what you'd want in most cases, but if you want to just remove that particular remote-tracking branch, you should do:

git branch -d -r origin/coolbranch

(The -r is easy to forget...)

-r in this case will "List or delete (if used with -d) the remote-tracking branches." according to the Git documentation found here: https://git-scm.com/docs/git-branch

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • 20
    `git remote prune origin` or any form of `git fetch --prune` flagging did not work for me in my case. ...But `git branch -d -r origin/feature/branch-name` did work. I'm not sure if it had something to do with it being under the `feature` namespace (git flow) but that's how it went down, in case any googlers find that happens to them. – Misterparker Jul 09 '16 at 05:34
  • 19
    Is there a reason this is necessary? Seems really bad to leave these non-existent branch names in the list and not automatically prune them. – akronymn Feb 27 '17 at 20:20
  • 2
    @akronymn Agreed. There's a lot of odd default behavior in git – Kellen Stuart Feb 17 '22 at 18:42
  • 3
    @akronymn there is a config flag you can set to do it automatically : set `fetch.prune` to `true` (cf [this answer](https://stackoverflow.com/a/59792628/11384184)) – Lenormju Jul 07 '22 at 14:07
  • git branch -d -r origin/sprint#12A works for me – Gopal Kohli May 19 '23 at 08:14
395

Try:

git remote prune origin

From the Git remote documentation:

prune

Deletes all stale remote-tracking branches under <name>. These stale branches have already been removed from the remote repository referenced by <name>, but are still locally available in "remotes/<name>".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Phillips
  • 12,693
  • 3
  • 29
  • 26
280

Don't forget the awesome

git fetch -p

which fetches and prunes all origins.

orip
  • 73,323
  • 21
  • 116
  • 148
36

In our particular case, we use Stash as our remote Git repository. We tried all the previous answers and nothing was working. We ended up having to do the following:

git branch –D branch-name (delete from local)
git push origin :branch-name (delete from remote)

Then when users went to pull changes, they needed to do the following:

git fetch -p
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shadowvail
  • 498
  • 5
  • 7
  • `git branch -d branch-name` worked for me. Notice `-D` to `-d`. In fact when I tried with upper case D `-D` it created a new branch with name -D – RP- Aug 24 '16 at 21:32
  • 1
    the "-D" is a way of forcing the delete regardless of its push or merge status. Obviously be careful using it, but in a lot of cases, it has been necessary. – Shadowvail Apr 16 '19 at 12:57
  • 1
    What does the `:` stand for in `git push origin :branch-name`? – Giraldi Dec 08 '21 at 08:49
  • @Giraldi `:` stands for _fromLocalBranch_ : _toRemoteBranch_. Efectively, pushing _empty_ to remote branch. – jesusduarte Jul 14 '22 at 01:15
32

Use:

git remote prune <remote>

Where <remote> is a remote source name like origin or upstream.

Example: git remote prune origin

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
likaiguo.happy
  • 2,138
  • 1
  • 15
  • 9
  • 3
    for future readers, here's the difference between [`git remote prune`, `git prune`, `git fetch --prune`](http://stackoverflow.com/questions/20106712/what-are-the-differences-between-git-remote-prune-git-prune-git-fetch-prune). – Emile Bergeron Mar 30 '16 at 01:44
7

I've been using

git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -D

to delete the local branches pointing to merged branches.