Remove local stale branches

It’s no secret that we end up with tons of unused, stale branches (either merged or not) in local git repository.

You might want to clean-up unused branches for both organization and storage purposes. But you don’t want to delete branches one-by-one …


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Wahid Abduhakimov

It's no secret that we end up with tons of unused, stale branches (either merged or not) in local git repository.

You might want to clean-up unused branches for both organization and storage purposes. But you don't want to delete branches one-by-one which would literally take hours (sometimes).

Here is how to remove local branch in bulk.

1. Remove git branches on *nix terminal.

# this will delete only merged branches
git branch --merged | grep -v \* | xargs git branch -D

# this will delete all branches except the current one checked out
git branch | grep -v \* | xargs git branch -D

2. Remove git branches on Windows Powershell/Terminal.

# this will magically all branches except dev, main, master and develop.
git branch | Select-String -Pattern '^(?!.*(dev|main|master|develop)).*$' | ForEach-Object { git branch -D $_.ToString().Trim() }

# this will only delete merged branches.
git branch --merged | Select-String -Pattern '^(?!.*(dev|main)).*$' | ForEach-Object { git branch -D $_.ToString().Trim() }

Yeah, it's that easy. Go ahead and give your local git some breathe 🙂.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Wahid Abduhakimov


Print Share Comment Cite Upload Translate Updates
APA

Wahid Abduhakimov | Sciencx (2022-09-22T15:49:11+00:00) Remove local stale branches. Retrieved from https://www.scien.cx/2022/09/22/remove-local-stale-branches/

MLA
" » Remove local stale branches." Wahid Abduhakimov | Sciencx - Thursday September 22, 2022, https://www.scien.cx/2022/09/22/remove-local-stale-branches/
HARVARD
Wahid Abduhakimov | Sciencx Thursday September 22, 2022 » Remove local stale branches., viewed ,<https://www.scien.cx/2022/09/22/remove-local-stale-branches/>
VANCOUVER
Wahid Abduhakimov | Sciencx - » Remove local stale branches. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/22/remove-local-stale-branches/
CHICAGO
" » Remove local stale branches." Wahid Abduhakimov | Sciencx - Accessed . https://www.scien.cx/2022/09/22/remove-local-stale-branches/
IEEE
" » Remove local stale branches." Wahid Abduhakimov | Sciencx [Online]. Available: https://www.scien.cx/2022/09/22/remove-local-stale-branches/. [Accessed: ]
rf:citation
» Remove local stale branches | Wahid Abduhakimov | Sciencx | https://www.scien.cx/2022/09/22/remove-local-stale-branches/ |

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.