1

In our workflow, development branches which are production-ready get pushed to the remote tracking repository where they are merged into master. The branch is deleted when the tests pass.

I have been bad about deleting my local version of those development branches, so now I have many local branches which don't have a corresponding branch on origin.

Is there a quick way to find which branches in my repository have already been deleted from the remote repository?

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • By "local branch", you mean branches under `refs/heads`, and not remote-tracking branches under `refs/remotes/origin`? –  Aug 06 '13 at 13:54
  • yes, `refs/heads` - which I think of as local, because the global repository doesn't see those changes unless I push. – AShelly Aug 06 '13 at 14:21

2 Answers2

8

Finding and deleting local branches under refs/heads

Assuming that the original poster means he wants to delete local branches under refs/heads and not remote-tracking branches under refs/remotes/origin, then to find branches that have already been merged into origin/master, simply use the following:

# Fetch latest copy of origin/master
$ git fetch origin

# Find merged branches
$ git branch --merged origin/master

The output will show you branches which have been merged nto origin/master, and are thus safe to delete with git branch -d <branch>. From the official Linux Kernel Git documentation for git branch:

"--merged" is used to find all branches which can be safely deleted, since those branches are fully contained by HEAD.

Deleting remote-tracking branches under refs/remotes/origin

If, on the other hand, the original poster meant that he wanted to delete his remote-tracking branches, then simply pass the -p or --prune flags to git fetch:

$ git fetch -p origin

From the official Linux Kernel Git documentation for git fetch:

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

2
git fetch -p

From the manual:

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

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Note that this only removes remote-tracking branches, and will not touch local branches under `refs/heads`. –  Aug 06 '13 at 13:53