16

To delete a local branch in git I use git branch -d, but how do I safely remove a remote branch?

I would like to delete it only when the remote branch is merged to my current branch.

Coleman
  • 565
  • 7
  • 15
Sławosz
  • 11,187
  • 15
  • 73
  • 106

2 Answers2

18

The answer is partly covered here: How can I know in git if a branch has been already merged into master?

While that post copes with local branches, you could find remote branches that are merged or not using

  • git branch -r --merged to detect all remote branches that are already merged into the current
  • git branch -r --unmerged to do the opposite

  • git branch -r --no-merged is correct for the new version of Git and I'm not sure whether git branch -r --unmerged is applicable for old git.

Once you found that a specific remote branch is already merged (i.e. it appears when typing git branch -r --merged), you could delete it as Michael Krelin answers using

git push <remote> :<remotebranchname>

See also the documentation of git branch for the --merged and --unmerged flags.

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • thanks for proposing to remove the downvote, I don't think 2 points are worth the hassle, though. As a bonus you get an upvote ;) – Michael Krelin - hacker Feb 01 '12 at 11:09
  • Have somebody a way to automate this process (list remote merged branches and delete them)? – eloyesp Jun 01 '12 at 20:17
  • 2
    In git 1.8 (dunno about previous versions) it's `--no-merged` instead of `--unmerged`. – Francesc Rosas May 16 '13 at 17:39
  • `git log -S '--unmerged' -p .` in Git's Documentation directory shows only entries about `git ls-files --unmerged`, but I can't find any trace of `git branch --unmerged` in current or past Git. – Matthieu Moy May 06 '15 at 11:22
5

Just to point out that for unmerged branches it seems the option now is --no-merged as explained on http://git-scm.com/docs/git-branch

Carlos
  • 480
  • 1
  • 5
  • 10