列出所有没有远程分支的本地分支

154
问题:我想找到一种删除所有本地分支的方法,这些分支没有对应远程分支。把分支名称传递给git branch -D {branch_name} 很容易,但是如何首先获取该列表呢?
例如:
我创建了一个没有远程分支的新分支:
$ git co -b no_upstream

我列出了所有我的分支,只有一个带有远程

$ git branch -a
master
* no_upstream
remotes/origin/HEAD -> origin/master
remotes/origin/master

我应该运行什么命令才能得到答案no_upstream

我可以运行git rev-parse --abbrev-ref --symbolic-full-name @{u},它会显示没有远程:

$ git rev-parse --abbrev-ref --symbolic-full-name @{u}
error: No upstream configured for branch 'no_upstream'
error: No upstream configured for branch 'no_upstream'
fatal: ambiguous argument '@{u}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

但由于这是一个错误,它不允许我使用它或将其传递给其他命令。我打算将其用作一个 shell 脚本别名,如 git-delete-unbranched,或者制作一个超级简单的 Gem,例如 git-branch-delete-orphans


请参见https://dev59.com/pG445IYBdhLWcg3wRoLH。 - Tomer Cohen
1
可能是删除本地不再存在于远程的分支的重复问题。 - ks1322
12个回答

1
我使用
git branch --no-merged main
这将列出没有"origin/"对应分支的分支(显然),以及那些还有提交尚未到达目标分支的分支,所以这是一个很好的分支列表,用于在我合并了涉及多个"备份-变基-看看是否有效-再试一种方式"会话的PR之后进行清理。
--no-merged [<commit>]
Only list branches whose tips are not reachable from the specified commit (HEAD if not specified). Implies --list.

0

结合几个现有的答案:

[alias]
        branch-local = !git branch -vv | cut -c 3- | egrep '([0-9a-f]{7} [^[])|(: gone\\])' | sed -E 's/(^.+[0-9a-f]{7} (\\[.+\\])?).*$/\\1/'

E.g.:

$ git branch-local
autogen_autoreconf      fcfb1c6 [github/autogen_autoreconf: gone]
autotools_documentation 72dc477 [github/autotools_documentation: gone]
cray-mpich-messy        2196f4b
epel-sphinx             cfda770
lammps                  07d96a7 [github/lammps: gone]
lark-error              9ab5d6c [github/lark-error: gone]
no-root2                c3894d1
openmpi-cray            69326cf [github/openmpi-cray: gone]
shellcheck-cleanup      40d90ec
travis-python36         cde072e
update_vagrant_default  4f69f47 [github/update_vagrant_default: gone]
web-docs                e73b4a8

这个别名定义在哪里? - ekkis
1
@ekkis ~/.gitconfig - Ihor Drachuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接