626

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
  • 28,342
  • 21
  • 95
  • 123
yonix
  • 9,925
  • 7
  • 30
  • 50
  • 1
    Related: [Delete a Git branch both locally and remotely](http://stackoverflow.com/q/2003505/456814). –  Oct 18 '15 at 01:32
  • 75
    If you `git fetch -p` (or `git pull -p`) then remote branches will be pruned. – yoyo Jul 07 '16 at 22:07

5 Answers5

773

git remote prune origin, as suggested in the other answer, 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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mark Longair
  • 385,867
  • 66
  • 394
  • 320
  • 14
    `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
  • 11
    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
321

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
  • 28,342
  • 21
  • 95
  • 123
Brian Phillips
  • 11,829
  • 3
  • 27
  • 26
245

Don't forget the awesome

git fetch -p

which fetches and prunes all origins.

orip
  • 66,082
  • 20
  • 111
  • 144
  • For completeness: it must be the same as `git remote prune origin` and similar to `git pull --prune` mentioned at http://stackoverflow.com/a/6127884/94687 and http://stackoverflow.com/a/17983126/94687 respectively. And: `git remote update --prune` – imz -- Ivan Zakharyaschev Jul 01 '15 at 12:07
  • 3
    But branches will be still visible if u run `git branch -a` – Nikhil Sahu Apr 10 '17 at 10:23
  • 1
    @NikhilSahu: I'm not seeing the remote branches that were removed anymore with `git branch -a` – user276648 Oct 20 '17 at 05:49
23

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
  • 28,342
  • 21
  • 95
  • 123
Shadowvail
  • 368
  • 3
  • 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
22

Use:

git remote prune <remote>

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

Example: git remote prune origin

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
likaiguo.happy
  • 1,498
  • 11
  • 8
  • 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