• Cleaning House

    defunkt 12 Jun 2008

    In the past month I’ve done a lot of traveling. Traveling means noodling – lots of topic branches laying around from airplane rides and hotel hacking. Today I decided to get rid of the topic branches which had been merged into master. But how?

    When in doubt, ask Scott Chacon. Here’s how to delete all the branches which are a subset of master (and therefor contain nothing juicy):

    git branch --contains master | grep -v master | xargs git branch -d
    
  • Comments

    kballard Thu Jun 12 18:58:57 -0700 2008

    That’s actually not going to work. git branch --contains master gives you all branches that are a descendent of master, whereas you want all branches that are an ancestor of master. You should be using git checkout master && git branch --merged

    mernen Thu Jun 12 21:34:48 -0700 2008

    As I recall, git-branch -d will remove anything safe and ignore others, so it’d still work. But thanks for the tips, both of you. It never even occurred to me git-branch could have such parameters.

    schacon Thu Jun 12 21:58:48 -0700 2008

    kballard is right, I misunderstood your question, which was asking for ‘subsets of a given branch’ – “git branch -contains master” will give you every branch that has the sha in it’s history that ‘master’ resolves to. If master has moved forward since the branching, it won’t work how you’re describing it here. To find the branches that have already been merged in, ‘-merged’ is in fact what you want. Apologies for the confusion.

    When in doubt, ask Scott Chacon, then make damn sure he understood what you were asking, and was not drinking heavily at the time :)

    jwarchol Fri Jun 13 09:09:14 -0700 2008

    What version of git has the—merged option on branch? I’ve got 1.5.5.3 and it’s not there.

    schacon Fri Jun 13 15:05:07 -0700 2008

    really? taking a quick look at the docs, it looks like—merged was added on Apr 17:

    commit e8b404c27e98a8d1b0e123fe80ce19efbdbf73d7
    Author: Lars Hjemli <hjemli@gmail.com>
    Date:   Thu Apr 17 22:24:50 2008 +0200
    
        git-branch: add support for --merged and --no-merged
    

    and 1.5.5.3 was tagged on May 27:

    tag v1.5.5.3
    Tagger: Junio C Hamano <gitster@pobox.com>
    Date:   Tue May 27 22:32:56 2008 -0700
    
    GIT 1.5.5.3
    

    So it certainly should have been there. Perhaps you mean ‘1.5.5-rc3’? That was released a week before the ‘—merged’ option was added.

    Scott

    chrisk Mon Jun 16 15:32:41 -0700 2008

    I think git branch --merged is scheduled for 1.5.6.

    
      chrisk@chrisk:~/project›master $ git --version
      git version 1.5.5.4
      chrisk@chrisk:~/project›master $ git branch --merged
      error: unknown option `merged'
      [snip]
    
    Please log in to comment.