Git - clean up git branches in my local repository (git remove all local branches except master)
1. Problem
How to clean up git branches in my local repository?
I am interested in cleaning up local repo, this are my requirements after cleanup:
- remove all local branches except master
- get all remote branches (refresh local list of remote branches)
Current state on remote repo:
master
remote_branch_2
remote_branch_5
Before in local repo:
master
local_branch_1
local_branch_2
local_branch_3
remote_branch_1
remote_branch_2
remote_branch_3
remote_branch_4
After in local repo (same state as on remote repo):
master
remote_branch_2
remote_branch_5
Is it possible to achieve it in 1 command line?
If not the easiest way to do it?
2. Solution
1 liner cmd - ensure you are on MASTER branch
git branch | grep -v "master" | xargs git branch -D && git remote update --prune
Above line will do exactly what you described in question.
Explanation:
Current state on remote repo:
master
remote_branch_2
remote_branch_5
First command
git remove all local branches except master
git branch | grep -v "master" | xargs git branch -D
After this command local repo will looks:
master
remote_branch_1
remote_branch_2
remote_branch_3
remote_branch_4
Second command
git get all remote branches and refresh list in local repo of remote branches
git remote update --prune
After this command local repo will looks:
master
remote_branch_2
remote_branch_5