Languages
[Edit]
EN

Git - clean up git branches in my local repository (git remove all local branches except master)

9 points
Created by:
OneCricketer
370

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

References

  1. Git branch - docs
  2. Git remote - docs
  3. xargs - linux docs

See also

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Git

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join