Remove all git local branches except “main”

Working locally would tempt to acummulate a lot of git branches locally, most of the time we want to keep only the “main” branch, to execute this just run:

git branch | grep -v “main” | xargs git branch -D

Explanation

The first sec…


This content originally appeared on DEV Community and was authored by Ariel Mejia

Working locally would tempt to acummulate a lot of git branches locally, most of the time we want to keep only the "main" branch, to execute this just run:

git branch | grep -v "main" | xargs git branch -D

Explanation

  • The first section git branch list all the branches locally.
  • grep -v "main" filter out from the output branches list the main branch
  • xargs git branch -D runs git branch -D to all the branch items inside the branch list

Happy Coding!


This content originally appeared on DEV Community and was authored by Ariel Mejia


Print Share Comment Cite Upload Translate Updates
APA

Ariel Mejia | Sciencx (2024-08-24T08:17:03+00:00) Remove all git local branches except “main”. Retrieved from https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/

MLA
" » Remove all git local branches except “main”." Ariel Mejia | Sciencx - Saturday August 24, 2024, https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/
HARVARD
Ariel Mejia | Sciencx Saturday August 24, 2024 » Remove all git local branches except “main”., viewed ,<https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/>
VANCOUVER
Ariel Mejia | Sciencx - » Remove all git local branches except “main”. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/
CHICAGO
" » Remove all git local branches except “main”." Ariel Mejia | Sciencx - Accessed . https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/
IEEE
" » Remove all git local branches except “main”." Ariel Mejia | Sciencx [Online]. Available: https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/. [Accessed: ]
rf:citation
» Remove all git local branches except “main” | Ariel Mejia | Sciencx | https://www.scien.cx/2024/08/24/remove-all-git-local-branches-except-main/ |

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.