git - 列出可以安全删除的 git 分支

标签 git

如果你执行 git branch -d branchname,如果它引用了你历史中较早的提交,它将删除分支名称,或者告诉你你需要使用 -D 否则。我经常创建后来推送到 master 的分支,因此可以按此标准删除。有没有一种简单的方法可以列出所有指向 master 早期提交的分支,即 git 不介意仅使用 -d 选项删除的分支?如果它同时适用于所有分支,而不仅仅是大师,则可获得加分。

我有 82 个本地分支机构,我知道现在可以安全地删除其中的大部分(如果不是大部分的话),但我不想花时间逐一尝试删除。

最佳答案

尝试:

$ git checkout master # or whatever branch you might compare against ...
$ git branch --no-merged
$ git branch --merged

来自 git branch documentation :

With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).

编辑:

要为每个分支显示此信息,您可以执行以下操作:

示例 repo :

o <--- experimental
|
o
|
o <--- next
|
o
|
o <--- master
|
o----o <--- broken
|
o
|


$ for branch in `git branch --no-color --verbose | \
sed -e 's/*//' | awk '{print $1}'`; \
do echo "[$branch]"; git checkout -q $branch; git branch --merged; done

[broken]
* broken
[master]
* master
[next]
master
* next
[experimental]
master
next
* experimental

关于git - 列出可以安全删除的 git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353804/

相关文章:

git - git 提交错误 : 'gpg-agent' is older than us (2. 0.30 < 2.1.20)

git - 如何在 GitHub 上从 master 创建开发分支

git - 如何 checkout 远程 Git 分支?

git - 如何确定 Git 中的 cherry-pick 的提交者?

git add -u 不添加修改过的文件

git - 使用用户名和密码执行 git pull

windows - 可与 SVN 存储库一起使用的 Git 扩展?

git - 带有登录凭据的 Bitbucket repo 克隆

git - git pull --rebase origin master 是什么意思?

基于 SHA 的 git pull