6

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
ysap
  • 7,723
  • 7
  • 59
  • 122

3 Answers3

4

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks. "You would still need to delete those local branches." - if this is the only way to achieve what I want, then I guess I'll just forget about this... – ysap Mar 11 '17 at 14:37
  • @ysap I don't get exactly what you mean by "clean the list". For me, cleaning the list means deleting local branches. But the git branch -a would still list those same branches as remote – VonC Mar 11 '17 at 14:38
  • Sorry about the confusion. Probably "clean" isn't the best term here. As I wrote in the question, every branch that I checkout gets added to the reported list, and it gets congested with time. Think about I just want to check what my colleague did in his branch so I check it out, and now it is "reported" forever. If I delete that branch (`-d`), then next time I check it out I get an error that the branch does not exist (obviously...). I want to remove it from the `git branch` list, but to be able to check it out easily anytime after that. – ysap Mar 11 '17 at 14:48
  • @ysap next time you want to checkout a branch deleted, a simple `git checkout -b abranch origin/abranch` is enough. – VonC Mar 11 '17 at 14:49
  • I see, but this relies on the assumption that the branch in origin is up to date, and that it even exists. Please see my 2nd update to the question. – ysap Mar 11 '17 at 15:01
  • @ysap yes, you need to push mybranch somewhere. Later, a simple `git fetch` will ensure that remote branch is back in your repo and up to date. The `checkout -b mybranch origin/mybranch` will then work as expected. – VonC Mar 11 '17 at 15:13
2

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
$
Nanda
  • 961
  • 6
  • 12
  • Thanks. I had to re-clone my repo recently, so the problem went away for now - but this looks promising and I will check that when relevant. But isn't mangling with the `.git/` contents dangerous? – ysap Jul 17 '17 at 20:39
  • `.git/refs/heads/` file has the latest commit-id of that branch. Deleting this will not cause any issue, when we do `git checkout ` a new file is created and updated with the latest commit-id of that branch. – Nanda Jul 19 '17 at 16:37
  • Thanks for clarifying. Once I can verify, I'll up-vote. – ysap Jul 19 '17 at 16:53
  • This is doing the trick. Hopefully it does not have any unseen side-effects. – ysap Aug 21 '17 at 03:42
  • This is weird. Coming back to this hack now that my branch list is again too long, and `ls`-ing the `heads` folder, some of the branch names I get in the `branch` command do not exist as files/subfolders. Any idea? – ysap Apr 02 '18 at 20:49
  • ... OTOH, I found the missing branches under the `logs` folder. But, strangely enough, there are many branches there that are not listed with the `branch` command. – ysap Apr 02 '18 at 20:52
  • I was so irritated with the long list of branches whenever I typed `git branch` . Thanks for solving my issue. – Nehal Jaisalmeria Nov 30 '20 at 07:03
1

At first, list all local branches:

$ git branch

We need to know what branches are already merged in “master” and can be easily removed:

$ git checkout master
$ git branch --merged

Now, remove all outdated branches with:

$ git branch -d old-merged-feature

Next, decide what to do with not merged branches:

$ git branch --no-merged

If some of them is just abandoned stuff that you don’t need anymore, remove it with “-D” option:

$ git branch -D old-abandoned-feature
Ty T.
  • 586
  • 5
  • 18
  • Thanks. I am not sure it makes sense, but I don't want to *delete* the branches. I want them to still exist in the repo. I just want to clean the list. Apparently, of the 100's of branches in the repo, the list contains only those I actually checked out or created since cloning. – ysap Mar 10 '17 at 23:28
  • Look up git prune that might be more of what your after. – Ty T. Mar 10 '17 at 23:33