Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

Git - clean up git branches in my local repository

0 contributions
9 points

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
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more