简体   繁体   English

git分支输出没有缩进空格? (除了正则表达式操作外)

[英]git branch output without indent whitespace? (besides regex manipulation)

Is there a git branch option that will print all branches without the two spaces in front of the branch name? 是否有一个git branch选项可以打印所有分支,而分支名称前没有两个空格? I thought --porcelain would be supported but apparently not for branch listing. 我以为--porcelain将得到支持,但显然不支持分支机构上市。

What I'm trying to do: I want to script deletion of merged branches without using low level regex manipulation like one would with awk , or in my case perl (so that my script is elegant and readable): 我想做的是:我想编写脚本来合并的分支的删除,而不使用像awk这样的低级正则表达式操作,或者在我的情况下是perl(这样我的脚本优雅且可读):

git branch | perl -pe 's{^\s*}{}g' | xargs -n 1 git branch -d

(Off topic, but for those who tend to hoard and hate deleting, this is a nice quick way to purge what you really don't need and keep a clean repo) (不在主题之列,但是对于那些倾向于收藏和讨厌删除的人,这是清除您真正不需要的东西并保持干净回购的一种不错的快速方法)

You can use git for-each-ref to get some string for each matched reference in your repository: 您可以使用git for-each-ref为存储库中的每个匹配引用获取一些字符串:

git for-each-ref refs/heads --format "%(refname:short)"

or 要么

git for-each-ref refs/heads --format "%(refname:strip=2)"

You can also use git branch directly. 您也可以直接使用git branch It probably uses for-each-ref internally. 它可能for-each-ref内部使用for-each-ref

To list the branches, use git branch --format "%(refname:short)" . 要列出分支,请使用git branch --format "%(refname:short)" This does not output the * . 这不会输出* Sample output: 样本输出:

develop
master
bug-123-fix-issue
task-345-do-stuff

To output the asterisk, use git branch --format "%(HEAD)%(refname:short)" . 要输出星号,请使用git branch --format "%(HEAD)%(refname:short)" Unfortunately, this creates a space in the beginning of all the other branches. 不幸的是,这在所有其他分支的开头创建了一个空间。 Sample output: 样本输出:

*develop
 master
 bug-123-fix-issue
 task-345-do-stuff

However, you can play with the command anyway you want, so you can reverse it to have the asterisk at the end. 但是,您可以随时使用该命令,因此可以将其反转以使星号结尾。 It still contains the space, but at the end. 它仍然包含空间,但最后。

git branch --format "%(refname:short)%(HEAD)"

Sample output: 样本输出:

develop*
master
bug-123-fix-issue
task-345-do-stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM