{"id":7187,"date":"2014-08-11T10:33:26","date_gmt":"2014-08-11T07:33:26","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=7187"},"modified":"2023-05-14T16:47:17","modified_gmt":"2023-05-14T13:47:17","slug":"git-clean-up-in-local-and-remote-branches","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/git-clean-up-in-local-and-remote-branches\/","title":{"rendered":"Git Clean-up in Local and Remote Branches, Repositories"},"content":{"rendered":"\n<p>After working with branch per feature for a while any Git-repository becomes a mess of outdated and not finished branches.<\/p>\n\n\n\n<p>To deal with this issue, <strong>we need to clean-up three kinds of branches<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Local branches &#8211; our day-to-day working branches<\/li>\n\n\n\n<li>References to remote branches &#8211; aka origin\/branch-name items<\/li>\n\n\n\n<li>Actual remote branches &#8211; branches on remote server(e.g.: github, bitbucket, gitorius)<\/li>\n<\/ul>\n\n\n\n<p>In this tutorial we suppose, that &#8220;master&#8221; &#8211; is a default branch, where everything is merged (for someone it could be &#8220;develop&#8221;, or &#8220;release&#8221; branch), and &#8220;origin&#8221; &#8211; it&#8217;s a name of remote. If you use different names, just change them in appropriate commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Local branches<\/h2>\n\n\n\n<p>At first, list all local branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git branch\n<\/pre>\n\n\n\n<p>We need to know what branches are already merged in &#8220;master&#8221; and can be easily removed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git checkout master\n$ git branch --merged\n<\/pre>\n\n\n\n<p>Now, remove all outdated branches with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git branch -d old-merged-feature\n<\/pre>\n\n\n\n<p>Next, decide what to do with not merged branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git branch --no-merged\n<\/pre>\n\n\n\n<p>If some of them is just abandoned stuff that you don&#8217;t need anymore, remove it with &#8220;-D&#8221; option:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git branch -D old-abandoned-feature\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">References to remote branches<\/h2>\n\n\n\n<p>After each <span class=\"lang:ruby crayon-inline\">git pull<\/span> or <span class=\"lang:ruby crayon-inline\">git fetch<\/span> command Git creates references to remote branches in local repository, but doesn&#8217;t clean up stale references.<\/p>\n\n\n\n<p>List referenced remote branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git branch -r\n<\/pre>\n\n\n\n<p>Clean-up outdated references:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git remote prune origin\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Tip<\/h3>\n\n\n\n<p>Update repository with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git fetch -p\n<\/pre>\n\n\n\n<p>and Git automatically prunes all stale references.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Remote branches<\/h2>\n\n\n\n<p>Usually, remote repository is a big garbage heap of stale branches, if there is no responsible housekeeping person.<\/p>\n\n\n\n<p>After previous <span class=\"lang:ruby crayon-inline\">git remote prune origin<\/span> we should have synched list of remote branches.<\/p>\n\n\n\n<p>At first, we can find branches which are already merged in &#8220;master&#8221;:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git checkout master\n$ git branch -r --merged\n<\/pre>\n\n\n\n<p>But this command does not provide much information. What if this branch is merged, but still used for feature development. Would be cool to know last commit date and author.<\/p>\n\n\n\n<p>This magic snippet provides all required information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ for branch in `git branch -r --merged | grep -v HEAD`; do echo -e `git show --format=\"%ci %cr %an\" $branch | head -n 1` \\\\t$branch; done | sort -r\n<\/pre>\n\n\n\n<p>Now, you can delete own remote branches, and ask other authors to clean-up theirs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git push origin --delete branch-name\n<\/pre>\n\n\n\n<p>Similar snippet for not merged branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ for branch in `git branch -r --no-merged | grep -v HEAD`; do echo -e `git show --format=\"%ci %cr %an\" $branch | head -n 1` \\\\t$branch; done | sort -r\n<\/pre>\n\n\n\n<p>This list should be reviewed more thoroughly to avoid losing important commits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Tip for Github users<\/h3>\n\n\n\n<p>After the last Github update, <strong>Branches<\/strong> page is divided into &#8220;Your branches&#8221;, &#8220;Active branches&#8221; and &#8220;Stale branches&#8221;, and it shows same information as previous commands.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus snippets<\/h2>\n\n\n\n<p>Usually, it&#8217;s simple to remove local and appropriate remote branches at once.<\/p>\n\n\n\n<p>This snippet shows only local merged branches, which have appropriate remote merged branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ comm -12  &lt;(git branch --merged|awk '{print($1)}') &lt;(git branch -r --merged|awk '{print($1)}'|awk -F \\\/ '{print($2)}')\n<\/pre>\n\n\n\n<p>More hardcore snippet with date and author information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ for branch in `comm -12  &lt;(git branch --merged|awk '{print($1)}') &lt;(git branch -r --merged|awk '{print($1)}'|awk -F \\\/ '{print($2)}')`;do echo -e `git show --format=\"%ci %cr %an\" $branch | head -n 1` \\\\t$branch; done | sort -r\n<\/pre>\n\n\n\n<p>Same snippets for not merged branches:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ comm -12  &lt;(git branch --no-merged|awk '{print($1)}') &lt;(git branch -r --no-merged|awk '{print($1)}'|awk -F \\\/ '{print($2)}')\n<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ for branch in `comm -12  &lt;(git branch --no-merged|awk '{print($1)}') &lt;(git branch -r --no-merged|awk '{print($1)}'|awk -F \\\/ '{print($2)}')`; do echo -e `git show --format=\"%ci %cr %an\" $branch | head -n 1` \\\\t$branch; done | sort -r\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Moving stuff into .gitconfig<\/h2>\n\n\n\n<p>It&#8217;s hard to remember such code, so the best way is to create shell scripts and put them in local &#8220;bin&#8221; folder. Note that these snippets work only in bash(and zsh). For example, let&#8217;s put first snippet into &#8220;bin\/git-both-merged&#8221; file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted wrap:true lang:sh\">#!\/usr\/bin\/env bash\n\ncomm -12  &lt;(git branch --merged|awk '{print($1)}') &lt;(git branch -r --merged|awk '{print($1)}'|awk -F \\\/ '{print($2)}')\n<\/pre>\n\n\n\n<p>Don&#8217;t forget to make it executable(<span class=\"lang:sh crayon-inline\">chmod 755 git-both-merged<\/span>), and you can also make a git alias for this script. Put next line in .gitconfig:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[alias]\n  # ... other aliases\n  both-merged = !git-both-merged\n<\/pre>\n\n\n\n<p>Now you can call it via git command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted toolbar:2 wrap:true theme:tomorrow-night lang:sh nums:false\">$ git both-merged\n<\/pre>\n\n\n\n<p>In the same vein you can create git aliases for all snippets from this article.<\/p>\n\n\n\n<p>That&#8217;s it, folks. Now, it&#8217;s time to clean-up all stale stuff!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After working with branch per feature for a while any Git-repository becomes a mess of outdated and not finished branches. To deal with this issue, we need to clean-up three kinds of branches: In this tutorial we suppose, that &#8220;master&#8221; &#8211; is a default branch, where everything is merged (for someone it could be &#8220;develop&#8221;,&#8230;<\/p>\n","protected":false},"author":25,"featured_media":9453,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Boiko"],"class_list":["post-7187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=7187"}],"version-history":[{"count":25,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7187\/revisions"}],"predecessor-version":[{"id":16282,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/7187\/revisions\/16282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/9453"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=7187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=7187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=7187"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=7187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}