Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clean the branch list?

Tags:

git

git-branch

After cloning a repo from the remote, I am on the master branch. When typing git branch to view the branch list, there's only master in the list.

Over time, checking out existing branches as well as creating new branches, eventually merging new work into master and pushing to origin. Now, typing git branch shows a long list with dozens of branches.

How can I reduce the list, removing specific branches from the list while retaining others on the list?

Update 1: To clarify, I am not interested in deleting any branch from the repo (and definitely not from the remote repo). Just making it easier to navigate between the subset of branches I regularly use at a given time.

Update 2: Consider -

$ git clone myrepo
$ git branch
        * master
$ git checkout mybranch
$ git branch
          master
        * mybranch
$ git checkout master
$ git branch
        * master
          mybranch
$ git "I'll not be dealing with mybranch for 91 days, do something to clean the list"
$ git branch
        * master

(playing DOOM for 91 days)

$ git checkout mybranch
$ git branch
          master
        * mybranch
like image 260
ysap Avatar asked Mar 10 '17 16:03

ysap


2 Answers

I don't want to delete the branches. I want them to still exist in the repo. I just want to clean the list

You would still need to delete those local branches. That won't delete them on the remote repo.

One good policy is to delete any branch merged to master.

git branch -d $(git branch --merged | grep -vw $(git rev-parse --abbrev-ref HEAD))

Don't forget you can restore any deleted branch (if done within the past 90 days) through git reflog.

like image 93
VonC Avatar answered Sep 20 '22 04:09

VonC


git branch command lists branches using files from the location .git/refs/heads, here you can find files with branch names. Each file in this location corresponds to a branch, remove the files for which you don't want to see the branch in git branch command's output.

$ git branch
* branch-1
  master
  trunk
$ ls .git/refs/heads/
branch-1  master  trunk
$ rm .git/refs/heads/master
$ git branch
* branch-1
  trunk
$
like image 43
Nanda Avatar answered Sep 22 '22 04:09

Nanda