762

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?

2

6 Answers 6

917

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

4
  • 16
    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. Jul 9, 2016 at 5:34
  • 16
    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, 2017 at 20:20
  • 2
    @akronymn Agreed. There's a lot of odd default behavior in git Feb 17, 2022 at 18:42
  • 2
    @akronymn there is a config flag you can set to do it automatically : set fetch.prune to true (cf this answer)
    – Lenormju
    Jul 7, 2022 at 14:07
386

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.

1
273

Don't forget the awesome

git fetch -p

which fetches and prunes all origins.

4
  • For completeness: it must be the same as git remote prune origin and similar to git pull --prune mentioned at stackoverflow.com/a/6127884/94687 and stackoverflow.com/a/17983126/94687 respectively. And: git remote update --prune Jul 1, 2015 at 12:07
  • 4
    But branches will be still visible if u run git branch -a Apr 10, 2017 at 10:23
  • 1
    @NikhilSahu: I'm not seeing the remote branches that were removed anymore with git branch -a
    – user276648
    Oct 20, 2017 at 5:49
  • Thanks, this might be the most simple way!
    – Mr.D
    Sep 16, 2022 at 15:04
35

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
4
  • 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, 2016 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, 2019 at 12:57
  • 1
    What does the : stand for in git push origin :branch-name?
    – Giraldi
    Dec 8, 2021 at 8:49
  • @Giraldi : stands for fromLocalBranch : toRemoteBranch. Efectively, pushing empty to remote branch. Jul 14, 2022 at 1:15
32

Use:

git remote prune <remote>

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

Example: git remote prune origin

1
5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.