Remove local branches that are no longer in the origin

This is a simple script that I use to remove local branches that are no longer on the origin. Just to clean my local env.

git remote prune origin
git branch -vv | grep ‘: gone]’ | grep -v “\*” | awk ‘{ print $1; }’ | xargs -r git branch -d

You…


This content originally appeared on DEV Community and was authored by Daniel Teixeira

This is a simple script that I use to remove local branches that are no longer on the origin. Just to clean my local env.

git remote prune origin
git branch -vv | grep ': gone]' | grep -v "\*" | awk '{ print $1; }' | xargs -r git branch -d

You can also create an alias and add to your .bashrc (if you use Linux or macOS).

Edit ~/.bashrc file.

Add this in the end of the file:

alias git-clean="git remote prune origin; git branch -vv | grep ': gone]' | grep -v \"\*\" | awk '{ print $1; }' | xargs -r git branch -d;"

Reload bash: source ~/.bashrc

That's all. :)


This content originally appeared on DEV Community and was authored by Daniel Teixeira


Print Share Comment Cite Upload Translate Updates
APA

Daniel Teixeira | Sciencx (2025-02-04T17:54:52+00:00) Remove local branches that are no longer in the origin. Retrieved from https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/

MLA
" » Remove local branches that are no longer in the origin." Daniel Teixeira | Sciencx - Tuesday February 4, 2025, https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/
HARVARD
Daniel Teixeira | Sciencx Tuesday February 4, 2025 » Remove local branches that are no longer in the origin., viewed ,<https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/>
VANCOUVER
Daniel Teixeira | Sciencx - » Remove local branches that are no longer in the origin. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/
CHICAGO
" » Remove local branches that are no longer in the origin." Daniel Teixeira | Sciencx - Accessed . https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/
IEEE
" » Remove local branches that are no longer in the origin." Daniel Teixeira | Sciencx [Online]. Available: https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/. [Accessed: ]
rf:citation
» Remove local branches that are no longer in the origin | Daniel Teixeira | Sciencx | https://www.scien.cx/2025/02/04/remove-local-branches-that-are-no-longer-in-the-origin/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.