17

I want to delete some remote branches of my project repository. I've run the next command:

git push origin :name_of_branch

and when I list the remote branches with

git branch -r

the branch that I've deleted doesn't appear, but a partner of mine run

git fetch

and later

git branch -r

and in the list, the branch name_of_branch that I had deleted, is still in the list. However, when he tries to delete the branch with

git push origin :name_of_branch

He receives the next message:

error: unable to delete 'name_of_branch': remote ref does not exist
error: failed to push some refs to 'the_name_of_the_repository'

How could I delete the branch completely of the list?

Airam
  • 2,048
  • 2
  • 18
  • 36
  • Duplicate of [Delete a Git branch both locally and remotely?](http://stackoverflow.com/q/2003505/456814). –  Jun 06 '14 at 03:22

3 Answers3

26

This happens because when this partner of yours runs git fetch, the branch deletion is not "applied" to his repository. fetch only updates and adds branches.

They can run git remote prune origin to trim away remote branches in their list that no longer exist in the upstream repository.

Jan Krüger
  • 17,870
  • 3
  • 59
  • 51
  • 5
    FYI, `git remote prune` is somewhat deprecated in favor of `git fetch --prune` or `git fetch -p`. See the [Git version 1.6.6 release notes](https://github.com/git/git/blob/v2.0.0/Documentation/RelNotes/1.6.6.txt#L162-L166): "[`git fetch --prune` makes] `git remote update` and `git remote prune` less necessary (there is no plan to remove `remote update` nor `remote prune`, though)." –  Jun 06 '14 at 03:19
11

git fetch --prune <remote> can be used to remove all your remote-tracking branches that are tracking branches that no longer exist in a remote repository (i.e. they were deleted in the remote). From the official Linux Kernel Git documentation for fetch:

-p

--prune

After fetching, remove any remote-tracking branches which no longer exist on the remote.

You can also remote obsolete remote-tracking branches with the command

git branch -D -r <remote>/<branch>

as stated in the documentation for git branch:

Use -r together with -d to delete remote-tracking branches. Note, that it only makes sense to delete remote-tracking branches if they no longer exist in the remote repository or if git fetch was configured not to fetch them again.

Community
  • 1
  • 1
1
git branch -r -d 'remote-branch'
Spudley
  • 166,037
  • 39
  • 233
  • 307
wrapperm
  • 1,266
  • 12
  • 18