如何清理分支列表?

How to clean the branch list?

从远程克隆一个 repo 后,我在 master 分支上。当键入 git branch 查看分支列表时,列表中只有 master

随着时间的推移,检查现有分支并创建新分支,最终将新工作合并到 master 并推送到 origin。现在,键入 git branch 会显示一个包含数十个分支的长列表。

如何减少列表,从列表中删除特定分支,同时保留列表中的其他分支?

更新 1:澄清一下,我对从回购中删除任何分支不感兴趣(绝对不从远程回购中删除).只是让我在给定时间经常使用的分支子集之间导航更容易。

更新 2:考虑 -

$ 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

首先,列出所有本地分支:

$ git branch

我们需要知道哪些分支已经合并到“master”中并且可以轻松删除:

$ git checkout master
$ git branch --merged

现在,删除所有过时的分支:

$ git branch -d old-merged-feature

接下来,决定如何处理未合并的分支:

$ git branch --no-merged

如果其中一些只是您不再需要的废弃内容,请使用“-D”选项将其删除:

$ git branch -D old-abandoned-feature

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

您仍然需要删除那些本地分支。这不会在远程仓库中删除它们。

一个好的策略是删除 any branch merged to master

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

别忘了你可以 restore any deleted branch (if done within the past 90 days) through git reflog

git branch 命令列出使用来自位置 .git/refs/heads 的文件的分支,您可以在此处找到具有分支名称的文件。此位置中的每个文件对应一个分支,删除您不想在 git branch 命令输出中看到该分支的文件。

$ git branch
* branch-1
  master
  trunk
$ ls .git/refs/heads/
branch-1  master  trunk
$ rm .git/refs/heads/master
$ git branch
* branch-1
  trunk
$