有没有脚本可以列出我创建的 git 分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36026374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a script to list git branches created by me?
提问by hawkeye
I'm aware that branches don't really store creator information - and they're just a pointer to a commit.
我知道分支并没有真正存储创建者信息——它们只是一个指向提交的指针。
My goal is to be able to clean out my old branches that have been merged back to the main branch, and list branches where this hasn't been done either. (A clean up).
我的目标是能够清除已合并回主分支的旧分支,并列出尚未完成此操作的分支。(清理)。
This is different to "finding unmerged branches" because I want to find merged branches as well, and I want to do it by author.
这与“查找未合并的分支”不同,因为我也想找到合并的分支,而且我想由作者来做。
My question is: Is there a script to list git branches created by me?
我的问题是:是否有脚本可以列出我创建的 git 分支?
回答by SachinSunny
This command lists all branches and their author names
此命令列出所有分支及其作者姓名
git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname
If you are using github you can also visit https://github.com/author/repo/branches/yoursto get all your branches
如果您使用 github,您还可以访问https://github.com/author/repo/branches/yours以获取您的所有分支
If you want to just delete all the already merged branches you can us the command
如果你只想删除所有已经合并的分支,你可以使用命令
git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d
For more details of git for-each-ref
visit here.
欲了解更多详情,git for-each-ref
请访问这里。
回答by Eric
As a slight condensation of SachinSunny's answer above, you can use the regular expression features of grep to accomplish a simpler command:
作为上面 SachinSunny 的回答的轻微浓缩,您可以使用 grep 的正则表达式功能来完成一个更简单的命令:
git branch --merged | grep -Pv "\*|master|dev" | xargs -n 1 git branch -d
回答by Skillzore
A bit late to the party here, but this is the top result on google when searching for "list git branch for author".
这里的聚会有点晚了,但这是搜索“list git branch for author”时在谷歌上的最高结果。
A one-liner to find your remote branches in git is:
在 git 中查找远程分支的单行代码是:
git branch -r | xargs -L1 git --no-pager show -s --oneline --author="$(git config user.name)"
git branch -r- lists all remote branches.
git branch -r- 列出所有远程分支。
xargs -L1- convertes the result of the previous command into arguments for the next command.
xargs -L1- 将上一个命令的结果转换为下一个命令的参数。
git show- git show, shows various kinds of objects in git. In this case it will show the latest commit on each branch, with full diff.
git show- git show,显示 git 中的各种对象。在这种情况下,它将显示每个分支上的最新提交,以及完整的差异。
The flag -ssuppresses diff output.
标志-s抑制差异输出。
The flag --onelinecondenses the commit info into one line.
标志--oneline将提交信息压缩为一行。
The flag --author="$(git config user.name)"only shows the commits for the current git user.
标志--author="$(git config user.name)"仅显示当前 git 用户的提交。
The flag --no-pagerruns git without a pager. Without this flag each commit of the result will be opened in it's own pager instance, which might work depending on your pager.
标志--no-pager在没有寻呼机的情况下运行 git。如果没有这个标志,结果的每个提交都将在它自己的寻呼机实例中打开,这可能取决于你的寻呼机。
The final result will look something like this:
最终结果将如下所示:
efbd5d738b (origin/feature/A) commit feature A
297e83d9a6 (origin/feature/B) commit feature B
951f6638de (origin/test/A) commit test A
307d16741b (origin/master) latest master commit
Be aware that the list will contain master and develop branches if the given author is the latest commiter on these branches.
请注意,如果给定作者是这些分支上的最新提交者,则该列表将包含 master 和 develop 分支。
回答by Paulo Fidalgo
As for Git version 2.23 you can just type:
对于 Git 2.23 版,您只需键入:
git branch
git branch
and it will give you the list the local list of branches. Remember that will also include master
and others that you have merged. To filter these pipe the result with egrep -v "(^\*|master|development)
它将为您提供本地分支机构列表的列表。请记住,这也将包括master
您已合并的其他人。过滤这些管道的结果egrep -v "(^\*|master|development)
If you want to include the remotes you can do:
git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname | egrep -v "(^\*|master|development)"| grep 'Your Name'
如果你想包括遥控器,你可以这样做:
git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname | egrep -v "(^\*|master|development)"| grep 'Your Name'
for deletion, pipe xargs git branch -D
用于删除,管道 xargs git branch -D