Search code examples
gitgit-remote

How would I git remote prune just a specific remote branch?


One of my coworkers "reused" a branch name, so every time I pull, I get an error message:

error: 'refs/remotes/origin/branch' exists; cannot create 'refs/remotes/origin/branch/subbranch'
...
error: some local refs could not be updated; try running 
 'git remote prune origin' to remove any old, conflicting branches

The thing is, I don't want to do a wholesale remote pruning - I'm paranoid, and I want to keep those local references around, in case other coworkers have screwed up in new and interesting ways.

Is there a way I can prune just the offending branch without also pruning all of the other branches which are happily sitting as remote refs on my local repository?


Solution

  • It looks like you can specifically delete the local reference branch, if you use the -r option of git branch:

    git branch -r --delete origin/branch
    

    After this, the new branch can be created appropriately, and fetch and pull happen without errors. (And git remote prune --dry-run origin still reports branches to prune.)