{"id":4269,"date":"2025-06-14T10:22:54","date_gmt":"2025-06-14T08:22:54","guid":{"rendered":"https:\/\/davidperezgar.com\/en\/?p=4269"},"modified":"2025-06-14T10:22:56","modified_gmt":"2025-06-14T08:22:56","slug":"how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command","status":"publish","type":"post","link":"https:\/\/davidperezgar.com\/en\/blog\/web-development\/how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command\/","title":{"rendered":"How to clean up local branches that no longer exist in the remote repository with a single command"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When we work on different projects and also use the strategy that I always recommend <a href=\"https:\/\/www.hostinger.com\/blog\/trunk-based-development-workflow-in-wordpress\" target=\"_blank\" rel=\"noopener\">Trunk Based development<\/a>, it is common that we create many temporary branches. Some of them are removed from the remote repository after doing merge or closing a task, but they remain in our local environment&#8230; taking up space and messing up the list of branches. It especially makes it difficult to search for the branches we are working on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But Git gives us a quick way to clean up those local branches that no longer have their remote equivalent. I&#8217;ll explain a command that I use frequently to leave my local repository spotless:<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Tabla de contenidos<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69df12f3ac632\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69df12f3ac632\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/davidperezgar.com\/en\/blog\/web-development\/how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command\/#%F0%9F%A7%AA_The_magic_command\" >\ud83e\uddea The magic command:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/davidperezgar.com\/en\/blog\/web-development\/how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command\/#%F0%9F%A7%A0_What_exactly_does_it_do\" >\ud83e\udde0 What exactly does it do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/davidperezgar.com\/en\/blog\/web-development\/how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command\/#%F0%9F%8E%AF_When_to_use_this_command\" >\ud83c\udfaf When to use this command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/davidperezgar.com\/en\/blog\/web-development\/how-to-clean-local-branches-that-no-longer-exist-in-remote-repository-with-one-command\/#%F0%9F%93%8C_Extra_tip\" >\ud83d\udccc Extra tip<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%A7%AA_The_magic_command\"><\/span>\ud83e\uddea The magic command:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\"><code class=\"\">git fetch -p &amp;&amp; git branch -vv | grep 'gone]' | awk '{print $1}' | xargs git branch -D\n<\/code><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%A7%A0_What_exactly_does_it_do\"><\/span>\ud83e\udde0 What exactly does it do?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s take it step-by-step:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"\"><strong><code>git fetch -p<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li class=\"\">Update remote repository information<\/li>\n\n\n\n<li class=\"\">The <code>-p<\/code> (or <code>--prune<\/code>) option removes references to remote branches that no longer exist.<\/li>\n\n\n\n<li class=\"\">\u2705 <em>We ensure that Git knows which branches are missing from the remote.<\/em><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"\"><strong><code>git branch -vv<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li class=\"\">Display all local branches with details: tracking, last commit, and if they are out of date.<\/li>\n\n\n\n<li class=\"\">This is where Git tells us if a branch is marked as <code>[gone]<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"\"><strong><code>grep 'gone]'<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li class=\"\">Filter the local branches whose remote tracking has disappeared.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"\"><strong><code>awk '{print $1}'<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li class=\"\">Extract only the local branch name from each line<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"\"><strong><code>xargs git branch -D<\/code><\/strong>\n<ul class=\"wp-block-list\">\n<li class=\"\">Forcibly <strong>delete<\/strong> all those orphaned local branches <\/li>\n\n\n\n<li class=\"\">\u26a0\ufe0f Use <code>-D<\/code> instead of <code>-d<\/code> so you don&#8217;t have to check to see if they have been merged.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%8E%AF_When_to_use_this_command\"><\/span>\ud83c\udfaf When to use this command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\">After doing merge of feature branches or fixes.<\/li>\n\n\n\n<li class=\"\">After a <code>git pull<\/code> in the main or develop.<\/li>\n\n\n\n<li class=\"\">When you notice that your list of branches starts to look like a graveyard of old branches.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%F0%9F%93%8C_Extra_tip\"><\/span>\ud83d\udccc Extra tip<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you prefer to go easy on yourself, you can change <code>-D<\/code> to <code>-d<\/code> in the last step:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">xargs git branch -d\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This will remove only the branches that have already been merged and avoid accidental loss.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">With this command, you can keep your environment clean, tidy and free of obsolete branches that just take up space. Try it and see how good a branch cleanup feels<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/medium.com\/@ezequiel.perez\/cleaning-up-local-git-branches-that-no-longer-exist-on-remote-a038d777f8c7\" target=\"_blank\" rel=\"noopener\"><\/a><a href=\"https:\/\/medium.com\/@ezequiel.perez\/cleaning-up-local-git-branches-that-no-longer-exist-on-remote-a038d777f8c7\" target=\"_blank\" rel=\"noopener\">Based on this blog article<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we work on different projects and also use the strategy that I always recommend Trunk Based development, it is common that we create many temporary branches. Some of them are removed from the remote repository after merge or close a task, but they remain in our local environment&#8230; taking space and messing up the list of branches. Especially it makes it difficult to search for the branches we are working on. But Git gives us a quick way to clean up those local branches that no longer have their remote equivalent. I&#8217;ll explain a command I use frequently to leave my local repository spotless: \ud83e\uddea The magic command: \ud83e\udde0 What &#8230; <a title=\"How to clean up local branches that no longer exist in the remote repository with a single command\" class=\"read-more\" href=\"https:\/\/davidperezgar.com\/blog\/desarrollo-web\/como-limpiar-ramas-locales-que-ya-no-existen-en-el-repositorio-remoto-con-un-solo-comando\/\" aria-label=\"Read more about How to clean up local branches that no longer exist in the remote repository with a single command\">Read more<\/a>&#8230;<\/p>\n","protected":false},"author":1,"featured_media":4271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","inline_featured_image":false,"_ayudawp_aiss_exclude":false,"webmentions_disabled_pings":false,"webmentions_disabled":false,"editor_notices":[],"footnotes":""},"categories":[168],"tags":[],"class_list":["post-4269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/4269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/comments?post=4269"}],"version-history":[{"count":0,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/4269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/media\/4271"}],"wp:attachment":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/media?parent=4269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/categories?post=4269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/tags?post=4269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}