列出所有没有远程的本地分支 [英] List all local branches without a remote

查看:128
本文介绍了列出所有没有远程的本地分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我想要一种删除所有没有远程的本地分支的方法。很容易将分支名称分支到 git branch -D {branch_name} ,但是我怎样才能得到这个列表?



例如:

我创建了一个没有远程的新分支:

  $ git co -b no_upstream 

分支,只有一个是远程的

  $ git branch -a 
master
* no_upstream
remotes / origin / HEAD - >原产地/主人
遥控器/原产地/主人

我可以运行什么命令来获得 no_upstream 作为答案?



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

  $ git rev-parse --abbrev-ref --symbolic-full-name @ {u} 
错误:没有为分支'no_upstream'配置上游
错误:没有上游为分支'no_upstream'
致命的:模棱两可的论点'@ {u}':未知的版本或路径不在工作树中。
使用' - '分隔修订版本的路径,如下所示:
'git< command> [< revision> ...] - [< file>]'

但是,由于这是一个错误,它不会让我使用它或将它传递给其他命令。我打算使用它作为shell脚本别名到 git-delete-unbranched 或者可以创建一个超级简单的Gem,比如 git- branch-delete-orphans



任何帮助将不胜感激!谢谢

解决方案

我最近发现 git branch -vv 非常详细版本的 git branch 命令。



如果它们存在,它将输出分支和远程分支,格式如下:

  25-timeout-error-with-many-targets 206a5fa WIP:批量插入
31-target-suggestions f5bdce6 [origin / 31-target-suggestions]在保存
* 54-feedback-link b98e97c [origin / 54-feedback-link] WIP:邮件中的反馈链接时为团队和列表创建目标建议
65-digest-double-publish 2de4150 WIP:发布状态

很好的格式化输出,就像通过 cut 和 awk 来管理它以获取你的列表一样简单:

  git branch -vv | cut -c 3- | awk'$ 3!〜/ \ [/ {print $ 1}'

结果如下:

  25-timeout-error-with-many-targets 
65-digest-double-publish

cut 部分只是通过移除前两个在将它传递给 awk 之前,从输出中输出字符(包括 * )。


$ b

如果第三列中没有方括号, awk 部分会打印第一列。


$ b $ p 奖金:创建一个别名



通过在全局中创建一个别名 .gitconfig 文件(或任何地方):

  [别名] 
local-branches =!git branch -vv | cut -c 3- | awk'$ 3!〜/ \\ [/ {print $ 1}'

重要提示:反斜杠需要在别名中转义,否则您的gitconfig文件将会失效。

红利:远程过滤



如果由于某种原因,您有不同分支的多个追踪遥控器,指定您要检查的遥控器很容易。只需将远程名称附加到awk模式。在我的情况下,它是 origin 所以我可以这样做:

  git分支-vv | cut -c 3- | awk'$ 3!〜/ \ [origin / {print $ 1}'


Problem: I want a way of deleting all the local branches I have that do not have a remote. It's easy enough to pipe the names of the branches into a git branch -D {branch_name}, but how do I get that list in the first place?

For example:

I create a new branch with no remote:

$ git co -b no_upstream

I list all my branches, there's only 1 with a remote

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

What command can I run to get no_upstream as an answer?

I can run git rev-parse --abbrev-ref --symbolic-full-name @{u} and that will show that it has no remote:

$ 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>...]'

But as this is an error, it won't let me use it or pipe it to other commands. I'm intending to use this as either a shell script alias'd to git-delete-unbranched or maybe make a super simple Gem like git-branch-delete-orphans

Any help would be appreciated! Thanks

解决方案

I recently discovered git branch -vv which is the "very verbose" version of the git branch command.

It outputs the branches along with the remote branches if they exist, in the following format:

  25-timeout-error-with-many-targets    206a5fa WIP: batch insert
  31-target-suggestions                 f5bdce6 [origin/31-target-suggestions] Create target suggestion for team and list on save
* 54-feedback-link                      b98e97c [origin/54-feedback-link] WIP: Feedback link in mail
  65-digest-double-publish              2de4150 WIP: publishing-state

Once you have this nicely formatted output, it's as easy as piping it through cut and awk to get your list:

git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'

Results in the following output:

25-timeout-error-with-many-targets
65-digest-double-publish

The cut portion just normalizes the data by removing the first two characters (including *) from the output before passing it to awk.

The awk portion prints the first column if there is no square bracket in the third column.

Bonus: Create an alias

Make it easy to run by creating an alias in your global .gitconfig file (or wherever):

[alias]
  local-branches = !git branch -vv | cut -c 3- | awk '$3 !~/\\[/ { print $1 }'

Important: The backslash needs to be escaped in the alias or else you will have an invalid gitconfig file.

Bonus: Remote Filtering

If for some reason you have multiple tracking remotes for different branches, it's easy enough to specify which remote you want to check against. Just append the remote name to the awk pattern. In my case, it's origin so I can do this:

git branch -vv | cut -c 3- | awk '$3 !~/\[origin/ { print $1 }'

这篇关于列出所有没有远程的本地分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆