{"id":193,"date":"2014-06-11T16:49:46","date_gmt":"2014-06-11T20:49:46","guid":{"rendered":"https:\/\/blog.splice.com\/?p=193"},"modified":"2017-12-09T12:27:16","modified_gmt":"2017-12-09T17:27:16","slug":"cleaning-git-branches","status":"publish","type":"post","link":"https:\/\/splice.com\/blog\/cleaning-git-branches\/","title":{"rendered":"Cleaning Up Git Branches"},"content":{"rendered":"<p>Navigating a git repository can get unwieldy when there are lots of branches, especially if many of those branches aren&#8217;t relevant anymore. The other day we realized that we had 192 branches on our front-end codebase, 127 of which were already merged.<\/p>\n<p>Time to clean up!<\/p>\n<h2>TL;DR<\/h2>\n<p>You can chain together unix commands to do all the cleanup in a single go.<\/p>\n<pre><code>$ git checkout master &amp;&amp; git branch -r --merged | grep -v master | sed -e 's\/origin\\\/\/:\/' | xargs git push origin<\/code><\/pre>\n<p>If we want to do the same cleanup locally, the command is:<\/p>\n<pre><code>$ git checkout master &amp;&amp; git branch --merged | grep -v master | xargs git branch -d<\/code><\/pre>\n<h2>Unpacking the command<\/h2>\n<p>On a normal day, you might type <code>git push<\/code>. Git is smart enough to know that you probably want to push to a remote named <code>origin<\/code>, and that you want to push any local branches to remote branches of the same name. If you want to be specific about it, you can say:<\/p>\n<pre><code>$ git push origin feature-x<\/code><\/pre>\n<p>which will push the <em>local<\/em> <code>feature-x<\/code> branch up to the <em>remote<\/em> <code>feature-x<\/code> branch.<br \/>\nThis is great if your local branches are named the way you want, but what if they&#8217;re not? For example, what if you want to deploy <code>staging<\/code> to Heroku? Heroku runs whatever is in <code>master<\/code>. If you push your local <code>staging<\/code> to a heroku branch named <code>staging<\/code> it will be there, but nobody will see it. You would have to rename your master to <code>real-master<\/code>, rename <code>staging<\/code> to <code>master<\/code>, and then push that up to Heroku. There&#8217;s a better way. You can specify the local and remote names separately:<\/p>\n<pre><code>$ git push origin new-and-improved-master:master<\/code><\/pre>\n<p>Which brings us to the deletion command:<\/p>\n<pre><code>$ git push origin :the-branch-name<\/code><\/pre>\n<p>This pushes up an empty branch reference to the specific branch on the remote, which deletes it.<\/p>\n<h2>Tedious, Tedious Repetition<\/h2>\n<p>So we could go ahead and type out <code>git push origin :old-feature-x<\/code> over and over again to delete the 132 merged branches, or we could write a script that does it all at once.<\/p>\n<h2>Glorious Unix Pipes<\/h2>\n<p>To see all the remote branches, we can use usual <code>git branch<\/code> command with the <code>--remote<\/code> flag (<code>-r<\/code> for short).<\/p>\n<pre><code>$ git branch -r<\/code><\/pre>\n<p>There are two flags that can quickly show us which are merged into the branch that you are currently on: <code>--merged<\/code> and <code>--no-merged<\/code> That should be master, if you&#8217;re cleaning up, otherwise this can go horribly wrong.<\/p>\n<pre><code>$ git branch -r --merged<\/code><\/pre>\n<p>The output includes two lines that you don&#8217;t want to delete:<\/p>\n<pre><code>\r\norigin\/HEAD -&gt;origin\/master\r\norigin\/master\r\n<\/code><\/pre>\n<p>Let&#8217;s get rid of it using grep&#8217;s <code>--invert-match<\/code> flag (<code>-v<\/code> for short).<\/p>\n<pre><code>$ git branch -r --merged | grep -v master<\/code><\/pre>\n<p>Each branch in the output is prefixed with <code>origin\/<\/code>, which doesn&#8217;t quite work. We need to change that to <code>:<\/code>. Sed can help.<\/p>\n<pre><code>git branch --remote --merged | grep -v master | sed -e 's\/origin\\\/\/:\/'<\/code><\/pre>\n<p>Now we need to loop. We certainly could use a bash <code>for<\/code> loop:<\/p>\n<pre><code>$ for reference in `git branch -r --merged | grep -v master`; do git push origin $reference; done<\/code><\/pre>\n<p>On the other hand, there&#8217;s a handy unix command named <code>xargs<\/code>, which allows you to run a command on each line of the input.<\/p>\n<h2>When Things Get Out of Date<\/h2>\n<p>Sometimes your local environment knows about branches, but the remote has already deleted them. That will blow up, since you&#8217;ll be trying to delete branches that don&#8217;t exist. Not a huge deal, but it can be avoided using:<\/p>\n<pre><code>$ git remote update --prune<\/code><\/pre>\n<h2>Too Much Typing<\/h2>\n<p>Finally, you can create an alias in your shell configuration file (e.g. <code>.bash_profile<\/code>):<\/p>\n<pre><code>alias gitpurge=\"git checkout master &amp;&amp; git remote update --prune | git branch -r --merged | grep -v master | sed -e 's\/origin\\\/\/:\/' | xargs git push origin\"<\/code><\/pre>\n<p>Update: Thanks to @luislavena for pointing out that <code>git remote update --prune<\/code> is the equivalent of <code>git fetch origin &amp;&amp; git remote prune origin<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Navigating a git repository can get unwieldy when there are lots of branches, especially if many of those branches aren&#8217;t relevant anymore. The other day&#8230;<\/p>\n","protected":false},"author":2,"featured_media":197,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[61],"tags":[115],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Removing merged git branches<\/title>\n<meta name=\"description\" content=\"Splice engineering spotlight: git, branch, remote, cleanup\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Removing merged git branches\" \/>\n<meta property=\"og:description\" content=\"Splice engineering spotlight: git, branch, remote, cleanup\" \/>\n<meta property=\"og:url\" content=\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog | Splice\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Splice\" \/>\n<meta property=\"article:author\" content=\"http:\/\/facebook.com\/spliceofficial\" \/>\n<meta property=\"article:published_time\" content=\"2014-06-11T20:49:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-12-09T17:27:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"600\" \/>\n\t<meta property=\"og:image:height\" content=\"450\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Team Splice\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/splice\" \/>\n<meta name=\"twitter:site\" content=\"@splice\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Team Splice\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\"},\"author\":{\"name\":\"Team Splice\",\"@id\":\"https:\/\/splice.com\/blog\/#\/schema\/person\/1264212de6b56a47de705ec3ad103943\"},\"headline\":\"Cleaning Up Git Branches\",\"datePublished\":\"2014-06-11T20:49:46+00:00\",\"dateModified\":\"2017-12-09T17:27:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\"},\"wordCount\":493,\"publisher\":{\"@id\":\"https:\/\/splice.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg\",\"keywords\":[\"engineering\"],\"articleSection\":[\"Engineering\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\",\"url\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\",\"name\":\"Removing merged git branches\",\"isPartOf\":{\"@id\":\"https:\/\/splice.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg\",\"datePublished\":\"2014-06-11T20:49:46+00:00\",\"dateModified\":\"2017-12-09T17:27:16+00:00\",\"description\":\"Splice engineering spotlight: git, branch, remote, cleanup\",\"breadcrumb\":{\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/splice.com\/blog\/cleaning-git-branches\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage\",\"url\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg\",\"contentUrl\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg\",\"width\":600,\"height\":450,\"caption\":\"Cleaning Up Git Branches\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/splice.com\/blog\/cleaning-git-branches\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/splice.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cleaning Up Git Branches\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/splice.com\/blog\/#website\",\"url\":\"https:\/\/splice.com\/blog\/\",\"name\":\"Splice Blog\",\"description\":\"An inside look at making music\",\"publisher\":{\"@id\":\"https:\/\/splice.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/splice.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/splice.com\/blog\/#organization\",\"name\":\"Splice\",\"alternateName\":\"Splice Sounds\",\"url\":\"https:\/\/splice.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/splice.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2023\/09\/Splice-logo-black-background.png\",\"contentUrl\":\"https:\/\/splice.com\/blog\/wp-content\/uploads\/2023\/09\/Splice-logo-black-background.png\",\"width\":2928,\"height\":1540,\"caption\":\"Splice\"},\"image\":{\"@id\":\"https:\/\/splice.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/Splice\",\"https:\/\/x.com\/splice\",\"https:\/\/www.instagram.com\/splice\/\",\"https:\/\/www.youtube.com\/@splice\",\"https:\/\/discord.com\/invite\/splice\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/splice.com\/blog\/#\/schema\/person\/1264212de6b56a47de705ec3ad103943\",\"name\":\"Team Splice\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/splice.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4428b1733406dca9c238897202320bb3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4428b1733406dca9c238897202320bb3?s=96&d=mm&r=g\",\"caption\":\"Team Splice\"},\"sameAs\":[\"http:\/\/splice.wpengine.com\",\"http:\/\/facebook.com\/spliceofficial\",\"https:\/\/instagram.com\/splice\",\"https:\/\/x.com\/http:\/\/twitter.com\/splice\",\"https:\/\/www.youtube.com\/channel\/UCb8xLPa5RP8TnCsU7E5ASKQ\"],\"url\":\"https:\/\/splice.com\/blog\/author\/splice\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Removing merged git branches","description":"Splice engineering spotlight: git, branch, remote, cleanup","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/splice.com\/blog\/cleaning-git-branches\/","og_locale":"en_US","og_type":"article","og_title":"Removing merged git branches","og_description":"Splice engineering spotlight: git, branch, remote, cleanup","og_url":"https:\/\/splice.com\/blog\/cleaning-git-branches\/","og_site_name":"Blog | Splice","article_publisher":"https:\/\/www.facebook.com\/Splice","article_author":"http:\/\/facebook.com\/spliceofficial","article_published_time":"2014-06-11T20:49:46+00:00","article_modified_time":"2017-12-09T17:27:16+00:00","og_image":[{"width":600,"height":450,"url":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg","type":"image\/jpeg"}],"author":"Team Splice","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/splice","twitter_site":"@splice","twitter_misc":{"Written by":"Team Splice","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#article","isPartOf":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/"},"author":{"name":"Team Splice","@id":"https:\/\/splice.com\/blog\/#\/schema\/person\/1264212de6b56a47de705ec3ad103943"},"headline":"Cleaning Up Git Branches","datePublished":"2014-06-11T20:49:46+00:00","dateModified":"2017-12-09T17:27:16+00:00","mainEntityOfPage":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/"},"wordCount":493,"publisher":{"@id":"https:\/\/splice.com\/blog\/#organization"},"image":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage"},"thumbnailUrl":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg","keywords":["engineering"],"articleSection":["Engineering"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/","url":"https:\/\/splice.com\/blog\/cleaning-git-branches\/","name":"Removing merged git branches","isPartOf":{"@id":"https:\/\/splice.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage"},"image":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage"},"thumbnailUrl":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg","datePublished":"2014-06-11T20:49:46+00:00","dateModified":"2017-12-09T17:27:16+00:00","description":"Splice engineering spotlight: git, branch, remote, cleanup","breadcrumb":{"@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/splice.com\/blog\/cleaning-git-branches\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#primaryimage","url":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg","contentUrl":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2014\/06\/cleaning-up-git.jpg","width":600,"height":450,"caption":"Cleaning Up Git Branches"},{"@type":"BreadcrumbList","@id":"https:\/\/splice.com\/blog\/cleaning-git-branches\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/splice.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Cleaning Up Git Branches"}]},{"@type":"WebSite","@id":"https:\/\/splice.com\/blog\/#website","url":"https:\/\/splice.com\/blog\/","name":"Splice Blog","description":"An inside look at making music","publisher":{"@id":"https:\/\/splice.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/splice.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/splice.com\/blog\/#organization","name":"Splice","alternateName":"Splice Sounds","url":"https:\/\/splice.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/splice.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2023\/09\/Splice-logo-black-background.png","contentUrl":"https:\/\/splice.com\/blog\/wp-content\/uploads\/2023\/09\/Splice-logo-black-background.png","width":2928,"height":1540,"caption":"Splice"},"image":{"@id":"https:\/\/splice.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Splice","https:\/\/x.com\/splice","https:\/\/www.instagram.com\/splice\/","https:\/\/www.youtube.com\/@splice","https:\/\/discord.com\/invite\/splice"]},{"@type":"Person","@id":"https:\/\/splice.com\/blog\/#\/schema\/person\/1264212de6b56a47de705ec3ad103943","name":"Team Splice","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/splice.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4428b1733406dca9c238897202320bb3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4428b1733406dca9c238897202320bb3?s=96&d=mm&r=g","caption":"Team Splice"},"sameAs":["http:\/\/splice.wpengine.com","http:\/\/facebook.com\/spliceofficial","https:\/\/instagram.com\/splice","https:\/\/x.com\/http:\/\/twitter.com\/splice","https:\/\/www.youtube.com\/channel\/UCb8xLPa5RP8TnCsU7E5ASKQ"],"url":"https:\/\/splice.com\/blog\/author\/splice\/"}]}},"_links":{"self":[{"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/posts\/193"}],"collection":[{"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/comments?post=193"}],"version-history":[{"count":0,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/posts\/193\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/media\/197"}],"wp:attachment":[{"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/categories?post=193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/splice.com\/blog\/wp-json\/wp\/v2\/tags?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}