bash git删除多个远程分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10555136/
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
Delete multiple remote branches in git
提问by Jake A. Smith
I have a team member who inadvertently pushed over 150 of his local branches to our central repo. Thankfully, they all have the same prefix. Using that prefix, is there a git command or cool little shell script I can use that will delete all of those at once?
我有一个团队成员无意中将他的 150 多个本地分支推送到我们的中央仓库。值得庆幸的是,它们都有相同的前缀。使用该前缀,是否有我可以使用的 git 命令或很酷的小 shell 脚本来一次删除所有这些?
回答by neevek
Use the following command to remove all branches with PREFIX
prefix on remote server.
使用以下命令删除PREFIX
远程服务器上所有带有前缀的分支。
git branch -r | awk -F/ '/\/PREFIX/{print }' | xargs -I {} git push origin :{}
You may want to do a dry-run first to see if it is the branches that you want to remove:
您可能需要先进行试运行,看看是否是您要删除的分支:
git branch -r | awk -F/ '/\/PREFIX/{print }'
回答by jfeston
If you like a simpler approach, for instance delete 3 or 4 branches:
如果您喜欢更简单的方法,例如删除 3 或 4 个分支:
git push origin --delete <branch1> <branch2> <branch3>
Important:Only works on Git v1.7.0and above.
重要提示:仅适用于Git v1.7.0及更高版本。
回答by Dmitriy
Thanks to Neevekfor great and elegant solution!
感谢Neevek提供出色而优雅的解决方案!
But i have some troubles with slashes in branch names (i'm using Git Flow), because of awk
field separator /
(-F
option)
但是由于awk
字段分隔符/
(-F
选项),我在分支名称中使用斜杠时遇到了一些麻烦(我正在使用 Git Flow )
So my solution is based on Neevek's, but correctly parses branch names with /
. In this case i presume that your remote called origin
.
Command for deleting remote branches with names staring with PATTERN
:
所以我的解决方案基于Neevek 的,但正确解析分支名称为/
. 在这种情况下,我假设您的遥控器调用了origin
. 删除名称以 开头的远程分支的命令PATTERN
:
git branch -r | awk -Forigin/ '/\/PATTERN/ {print }' | xargs -I {} git push origin :{}
And don't forget to check what you are going to delete:
并且不要忘记检查您要删除的内容:
git branch -r | awk -Forigin/ '/\/PATTERN/ {print }'
USEFUL TIP:If your branch names (without origin/
prefix) stored in a text file (one branch name per line), just run:
有用的提示:如果您的分支名称(无origin/
前缀)存储在文本文件中(每行一个分支名称),只需运行:
cat your_file.txt | xargs -I {} git push origin :{}
回答by Naren
This may be a duplicate answer but below tested and worked for me perfectly.
这可能是重复的答案,但经过以下测试,对我来说非常有效。
- Delete local branch forcefully
- 强行删除本地分支
git branch -D branch-name
git branch -D 分支名称
- Delete Remote branch
- 删除远程分支
git push origin --delete branch-name
git push origin --delete 分支名称
- Delete more than 1 local branch
- 删除超过 1 个本地分支
git branch -D branch-name1 branch-name2
git branch -D 分支名称1 分支名称2
- Delete more than 1 remote branch
- 删除超过 1 个远程分支
git push origin --delete branch-name1 branch-name2
git push origin --delete branch-name1 branch-name2
- Delete local branch with prefix. For example, feature/*
- 删除带有前缀的本地分支。例如,功能/*
git branch -D $(git branch --list 'feature/*')
git branch -D $(git branch --list 'feature/*')
git branch -D backticks$(git branch --list 'feature/*' backticks)
git branch -D 反引号$(git branch --list 'feature/*' backticks)
- List remote branch with prefix.
- 列出带有前缀的远程分支。
git branch -r | grep -Eo 'feature/.*'
git 分支 -r | grep -Eo '功能/.*'
- Delete remote branch with prefix
- 删除带有前缀的远程分支
git branch -r | grep -Eo 'feature/.*' | xargs -I {} git push origin :{}
git 分支 -r | grep -Eo '功能/.*' | xargs -I {} git push origin :{}
回答by Kirby
The same with grep:
git branch -r | grep -Eo 'PREFIX/.*' | xargs -i git push origin :{}
.
与 grep: 相同
git branch -r | grep -Eo 'PREFIX/.*' | xargs -i git push origin :{}
。
branch -r
shows origin/prefix/branchname
. So it will take prefix/branchname
.
branch -r
显示origin/prefix/branchname
。所以需要prefix/branchname
.
回答by lack
Neevek's solution is elegant, but it can be better: the solution as proposed calls 'git push' once per branch, which means an additional network round-trip per branch to be deleted. Since you're using awk anyway, why not use it to prefix the ':' and then xargs can call 'git push' exactly once and delete all the branches at once:
Neevek 的解决方案很优雅,但它可以更好:提议的解决方案每个分支调用一次“git push”,这意味着每个分支要删除一个额外的网络往返。既然你无论如何都在使用 awk,为什么不使用它来前缀 ':' 然后 xargs 可以只调用一次 'git push' 并一次删除所有分支:
Dry-run to list the branches that would be deleted:
试运行以列出将被删除的分支:
git branch -r | awk -F/ '/\/PREFIX/{print ":" }'
Final solution to actually push the deletes:
实际推送删除的最终解决方案:
git branch -r | awk -F/ '/\/PREFIX/{print ":" }' | xargs git push origin
回答by Ashish Sajwan
resource https://coderwall.com/p/eis0ba
资源https://coderwall.com/p/eis0ba
1 - List all your remote branches:
$ git branch -r
2 - Filter the branches by some regular expression. In this case I'm interested in deleting any branch with the 'feature-' prefix:
$ git branch -r | awk -F/ '/\/feature-/{print }'
3 - Pipe the last command to git push to delete them:
# **Edit** - Removed extra colon, which is not needed
$ git branch -r | awk -F/ '/\/feature-/{print }' | xargs -I {} git push origin {}
4 - Grab a beer.
5 - Remove any local reference to those branches:
$ git remote prune origin
回答by andrewmart.in
Thanks to Steve and Neevek, I found a solution that worked pretty well for me I figured worth sharing:
感谢 Steve 和 Neevek,我找到了一个非常适合我的解决方案,我认为值得分享:
Steve's solution worked for me with one minor adjustment. My remotes were named origin/feature/some-feature-name
so I trimmed your awk
:
史蒂夫的解决方案对我有用,只需稍作调整。我的遥控器被命名,origin/feature/some-feature-name
所以我修剪了你的awk
:
git branch -r | awk -Forigin/ '/\/feature/ {print }' | xargs -I {} git push origin :{}
It's now doing a nice little delete flow:
它现在正在做一个不错的小删除流程:
To github.com:project/project-name.git
- [deleted] feature/search-min-chars
To github.com:project/project-name.git
- [deleted] feature/search-placeholder
To github.com:project/project-name.git
- [deleted] feature/server-error-message
To github.com:project/project-name.git
- [deleted] feature/six-point-asterisk
Was wondering if anyone had any ideas for a more elegant solution, though, that might output something like this (my CLI scripting is pretty poor, so it'd take me awhile to figure this out):
想知道是否有人对更优雅的解决方案有任何想法,但可能会输出这样的内容(我的 CLI 脚本非常糟糕,所以我需要一段时间才能弄清楚):
git push origin :feature/search-min-chars :feature/search-placeholder :feature/server-error-message :feature/six-point-asterisk
This would result in a nice single output with one network request:
这将导致一个很好的单一输出与一个网络请求:
To github.com:project/project-name.git
- [deleted] feature/search-min-chars
- [deleted] feature/search-placeholder
- [deleted] feature/server-error-message
- [deleted] feature/six-point-asterisk
回答by Ewin Hong
I realize this is for git command, but if you looking for an alternate solution to do the similar or same result:
我意识到这是针对 git 命令的,但是如果您正在寻找替代解决方案来执行类似或相同的结果:
You can do it from here (Git Remove Remote Branches):
你可以从这里完成(Git Remove Remote Branches):
Then select the branches you want:
然后选择你想要的分支:
Make sure you have the permissions to remove the remote branches.
确保您有权删除远程分支。
回答by user3416278
Thanks to Neevek. This worked well after reconfiguring it for my purpose:
感谢尼维克。在为我的目的重新配置它后,这工作得很好:
git branch -r | awk -Forigin/ '/\/PATTERN/ {print "/" }' | xargs -I {} git push origin :{}
I also needed take the folder structure into account. My feature-branches are in a folder structure like origin/feature/PREFIX-FEATURENUMBER. So i had to build up my pattern from $2=folder + $3= branchname.
我还需要考虑文件夹结构。我的功能分支位于像 origin/feature/PREFIX-FEATURENUMBER 这样的文件夹结构中。所以我不得不从 $2=folder + $3= branchname 建立我的模式。