842

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
  • 1
    Related: Delete a Git branch both locally and remotely. Commented Oct 18, 2015 at 1:32
  • 148
    If you git fetch -p (or git pull -p) then remote branches will be pruned. Commented Jul 7, 2016 at 22:07

7 Answers 7

1024

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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 Agreed. There's a lot of odd default behavior in git
@akronymn there is a config flag you can set to do it automatically : set fetch.prune to true (cf this answer)
git branch -d -r origin/sprint#12A works for me
432

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 Comment

For completeness: it must be similar to git pull --prune mentioned at stackoverflow.com/a/17983126/94687 And: git remote update --prune
298

Don't forget the awesome

git fetch -p 

which fetches and prunes all origins.

3 Comments

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
But branches will be still visible if u run git branch -a
@NikhilSahu: I'm not seeing the remote branches that were removed anymore with git branch -a
39

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 Comments

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
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.
What does the : stand for in git push origin :branch-name?
@Giraldi : stands for fromLocalBranch : toRemoteBranch. Efectively, pushing empty to remote branch.
32

Use:

git remote prune <remote> 

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

Example: git remote prune origin

1 Comment

for future readers, here's the difference between git remote prune, git prune, git fetch --prune.
17

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.

4 Comments

This is the right answer to the question asked.
This worked for me as well. Normal "git fetch -p" ONLY addresses remote-tracking branches. This command is more aggressive and does remote and local cleanup.
careful about that awk command if you're trying to operate on the currently checked out branch as the * will throw the args count off by 1 (shouldn't be the case since you're trting to delete)
It works for me
0

Using sed instead:

git remote prune origin git branch -vv | cut -c3- | grep "\[.*: gone\]" | sed "s/\([^[\s]]*\) .*/\1/" | xargs -r git branch -d 

Note that using -D instead of -d in the git branch command is forcing the deletion of the branch, regardless the non pushed or merged commits. It eventually all depends on what you are trying to achieve.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.