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

Re: Find/prune local branches after upstream branch is deleted?

From
Jed Brown <jed@59a2.org>
Date
Apr 19, 2013, 19:30 UTC
Message-ID
<87vc7i2u93.fsf@mcs.anl.gov>
In-Reply-To
<20130419180520.GA22865@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 18 quoted lines
> On Fri, Apr 19, 2013 at 12:50:48PM -0500, Jed Brown wrote:
>
>> Jeff King <peff@peff.net> writes:
>> 
>> > Try "git branch --merged master" to get a list of branches that have
>> > already been merged.
>> 
>> That's what I use, but I was hoping for something more precise.  For
>> example, a branch that started at 'maint' would show up there, but its
>> integration hasn't completed until it makes it back to 'maint'.
>
> Yeah, git has no way of knowing that. You can tell it by asking "git
> branch --merged maint" and looking for such topics, of course, but I
> think you are trying to remove as much thinking as possible from the
> process.
>
> So what is the clue that you would like to use to know that the branch
> is stale. Deletion of the branch on the remote?
I was hoping for ("has an upstream set" AND "upstream branch deleted").
Show 8 quoted lines
> For that, I don't think there is a single command. You can do "git push
> --dry-run --all" and look for new branches that would be pushed, but
> that is kind of hacky.  The answer you want is:
>
>   comm -23 \
>     <(git for-each-ref --format='%(refname:short)' refs/heads) \
>     <(git for-each-ref --format='%(refname:short)' refs/remotes/origin |
>       sed s,origin/,,)

This also finds my private branches that I just haven't pushed yet. The following has what I think are good semantics.

  git for-each-ref --format='%(refname:short) %(upstream)' refs/heads |
    while read ref upstream; do
      test -n "$upstream" && (git show-ref -q --verify "$upstream" || echo $ref)
    done

FWIW, Git sorts non-alpha characters differently from 'sort'. This means that Git output can't be sent to comm without first '| sort'.

$ git branch xy
$ git branch x-y
$ git branch
* master
  x-y
  xy
$ git branch | sort
* master
  xy
  x-y
Previous: Jeff King
Message 5 of 5 in “Find/prune local branches after upstream branch is deleted?”
  1. Jed BrownApr 19, 2013
  2. Jeff KingApr 19, 2013
  3. Jed BrownApr 19, 2013
  4. Jeff KingApr 19, 2013
  5. Jed BrownApr 19, 2013

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.