{"thread":{"id":"33546","subject":"Find/prune local branches after upstream branch is deleted?","startedAt":"2013-04-19T17:11:38Z","lastAt":"2013-04-19T19:30:32Z","messageCount":5,"participants":["Jed Brown","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"214848","messageId":"87ehe64f91.fsf@mcs.anl.gov","threadId":"33546","inReplyTo":null,"subject":"Find/prune local branches after upstream branch is deleted?","fromName":"Jed Brown","fromEmail":"jed@59a2.org","sentAt":"2013-04-19T17:11:38Z","receivedAt":"2013-04-19T17:11:38Z","isPatch":false,"sender":{"key":"jed@59a2.org","avatar":"https://gravatar.com/avatar/1391d04d82555f9058a9fdf5eead233e909a48e40480db31fc554e7afeb301da?d=mp&s=160"},"body":"Consider this workflow:\n\n$ git checkout -b my/branch\nhack, commit, ...\n$ git push -u origin my/branch\n\nThe branch gets reviewed, merged, and eventually deleted upstream.  The\nremote tracking branch gets pruned via 'git fetch --prune' or 'git\nremote prune', but that leaves my local branch with an upstream that has\nbeen deleted.  Is there a good way to discover this so I can prune my\nlocal branches?\n\n$ git branch -vv\n  my/branch        6d32ec0 [origin/my/branch] The commit message\n\nI can script it, but this seems like a pretty common thing.\n"},{"id":"214852","messageId":"20130419173717.GA26964@sigill.intra.peff.net","threadId":"33546","inReplyTo":"87ehe64f91.fsf@mcs.anl.gov","subject":"Re: Find/prune local branches after upstream branch is deleted?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-04-19T17:37:17Z","receivedAt":"2013-04-19T17:37:17Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Apr 19, 2013 at 12:11:38PM -0500, Jed Brown wrote:\n\n> Consider this workflow:\n> \n> $ git checkout -b my/branch\n> hack, commit, ...\n> $ git push -u origin my/branch\n> \n> The branch gets reviewed, merged, and eventually deleted upstream.  The\n> remote tracking branch gets pruned via 'git fetch --prune' or 'git\n> remote prune', but that leaves my local branch with an upstream that has\n> been deleted.  Is there a good way to discover this so I can prune my\n> local branches?\n> \n> $ git branch -vv\n>   my/branch        6d32ec0 [origin/my/branch] The commit message\n> \n> I can script it, but this seems like a pretty common thing.\n\nTry \"git branch --merged master\" to get a list of branches that have\nalready been merged.\n\n-Peff\n"},{"id":"214854","messageId":"87bo9a4dfr.fsf@mcs.anl.gov","threadId":"33546","inReplyTo":"20130419173717.GA26964@sigill.intra.peff.net","subject":"Re: Find/prune local branches after upstream branch is deleted?","fromName":"Jed Brown","fromEmail":"jed@59a2.org","sentAt":"2013-04-19T17:50:48Z","receivedAt":"2013-04-19T17:50:48Z","isPatch":false,"sender":{"key":"jed@59a2.org","avatar":"https://gravatar.com/avatar/1391d04d82555f9058a9fdf5eead233e909a48e40480db31fc554e7afeb301da?d=mp&s=160"},"body":"Jeff King <peff@peff.net> writes:\n\n> Try \"git branch --merged master\" to get a list of branches that have\n> already been merged.\n\nThat's what I use, but I was hoping for something more precise.  For\nexample, a branch that started at 'maint' would show up there, but its\nintegration hasn't completed until it makes it back to 'maint'.\n"},{"id":"214857","messageId":"20130419180520.GA22865@sigill.intra.peff.net","threadId":"33546","inReplyTo":"87bo9a4dfr.fsf@mcs.anl.gov","subject":"Re: Find/prune local branches after upstream branch is deleted?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-04-19T18:05:20Z","receivedAt":"2013-04-19T18:05:20Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Apr 19, 2013 at 12:50:48PM -0500, Jed Brown wrote:\n\n> Jeff King <peff@peff.net> writes:\n> \n> > Try \"git branch --merged master\" to get a list of branches that have\n> > already been merged.\n> \n> That's what I use, but I was hoping for something more precise.  For\n> example, a branch that started at 'maint' would show up there, but its\n> integration hasn't completed until it makes it back to 'maint'.\n\nYeah, git has no way of knowing that. You can tell it by asking \"git\nbranch --merged maint\" and looking for such topics, of course, but I\nthink you are trying to remove as much thinking as possible from the\nprocess.\n\nSo what is the clue that you would like to use to know that the branch\nis stale. Deletion of the branch on the remote?\n\nFor that, I don't think there is a single command. You can do \"git push\n--dry-run --all\" and look for new branches that would be pushed, but\nthat is kind of hacky.  The answer you want is:\n\n  comm -23 \\\n    <(git for-each-ref --format='%(refname:short)' refs/heads) \\\n    <(git for-each-ref --format='%(refname:short)' refs/remotes/origin |\n      sed s,origin/,,)\n\nYou said you didn't want to script, but I think that is the only option\nto answer that question cleanly.\n\nNote that if you wrap that in an alias, you would want to use \"bash -c\",\nas the <() syntax is not understood by many /bin/sh implementations.\n\n-Peff\n"},{"id":"214883","messageId":"87vc7i2u93.fsf@mcs.anl.gov","threadId":"33546","inReplyTo":"20130419180520.GA22865@sigill.intra.peff.net","subject":"Re: Find/prune local branches after upstream branch is deleted?","fromName":"Jed Brown","fromEmail":"jed@59a2.org","sentAt":"2013-04-19T19:30:32Z","receivedAt":"2013-04-19T19:30:32Z","isPatch":false,"sender":{"key":"jed@59a2.org","avatar":"https://gravatar.com/avatar/1391d04d82555f9058a9fdf5eead233e909a48e40480db31fc554e7afeb301da?d=mp&s=160"},"body":"Jeff King <peff@peff.net> writes:\n\n> On Fri, Apr 19, 2013 at 12:50:48PM -0500, Jed Brown wrote:\n>\n>> Jeff King <peff@peff.net> writes:\n>> \n>> > Try \"git branch --merged master\" to get a list of branches that have\n>> > already been merged.\n>> \n>> That's what I use, but I was hoping for something more precise.  For\n>> example, a branch that started at 'maint' would show up there, but its\n>> integration hasn't completed until it makes it back to 'maint'.\n>\n> Yeah, git has no way of knowing that. You can tell it by asking \"git\n> branch --merged maint\" and looking for such topics, of course, but I\n> think you are trying to remove as much thinking as possible from the\n> process.\n>\n> So what is the clue that you would like to use to know that the branch\n> is stale. Deletion of the branch on the remote?\n\nI was hoping for (\"has an upstream set\" AND \"upstream branch deleted\").\n\n> For that, I don't think there is a single command. You can do \"git push\n> --dry-run --all\" and look for new branches that would be pushed, but\n> that is kind of hacky.  The answer you want is:\n>\n>   comm -23 \\\n>     <(git for-each-ref --format='%(refname:short)' refs/heads) \\\n>     <(git for-each-ref --format='%(refname:short)' refs/remotes/origin |\n>       sed s,origin/,,)\n\nThis also finds my private branches that I just haven't pushed yet.  The\nfollowing has what I think are good semantics.\n\n  git for-each-ref --format='%(refname:short) %(upstream)' refs/heads |\n    while read ref upstream; do\n      test -n \"$upstream\" && (git show-ref -q --verify \"$upstream\" || echo $ref)\n    done\n\n\nFWIW, Git sorts non-alpha characters differently from 'sort'.  This\nmeans that Git output can't be sent to comm without first '| sort'.\n\n$ git branch xy\n$ git branch x-y\n$ git branch\n* master\n  x-y\n  xy\n$ git branch | sort\n* master\n  xy\n  x-y\n"}]}