git/list[1] front-page[2] threads[3] people[4] search[5] about
 

finding unmerged branches

From
Joey Hess <joey@kitenet.net>
Date
Aug 27, 2009, 22:02 UTC
Message-ID
<20090827220241.GA1413@gnu.kitenet.net>

My situation is this: My project has a lot of remotes with lots of branches; about 250 branches in total. I want to figure out which of these branches to look at to consider merging.

So, I reach for git branch -a --no-merged master; that's what its man page says its for. But this still finds 120 branches, and a lot of them are not things I want to look at. Many of them are copies of some of my own branches.

What I really want is a way to find remote branches that are not merged with any of my local branches (or any origin branches). A slow and stupid implementation of that is in the attached git-unmerged script, and it weeds the branch list down to 68 branches, which are mostly really ones I might want to look at.

So, three questions:
* Is this situation somewhat common, or an I doing something wrong?
  (Assuming that I have a good reason to want to look at remote 
  branches rather than waiting to get merge requests.)
* Is there a better way to accomplish this than a slow perl script that
  runs git branch -r --merged foreach of my branches?
* Should git have something builtin to handle this case better?
-- 
see shy jo


#!/usr/bin/perl

my @remote_branches = split ' ', `git branch -r | awk '{print $1}'`;

# have to filter out the "* "
my @local_branches = split ' ', `git branch | sed 's/^..//'`;

my @origin_branches = grep /^origin\//, @remote_branches;

my %unmerged = (map { $_ => 1 } @remote_branches),
	(map { $_ => 0 } @local_branches, @origin_branches);

foreach my $branch (@local_branches, @origin_branches) {
	map { $unmerged{$_}=0 } split ' ', `git branch -r --merged "$branch" | awk '{print $1}'`
}

foreach my $branch (sort keys %unmerged) {
	print "$branch\n" if $unmerged{$branch};
}
Next: Avery Pennarun
Message 1 of 7 in “finding unmerged branches”
  1. Joey HessAug 27, 2009
  2. Avery PennarunAug 27, 2009
  3. Joey HessAug 28, 2009
  4. Björn SteinbrinkAug 27, 2009
  5. Junio C HamanoAug 27, 2009
  6. Joey HessAug 28, 2009
  7. Joey HessAug 28, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.