简体   繁体   English

Git BASH 删除所有匹配模式的本地分支

[英]Git BASH delete all local branches matching a pattern

I was trying to create a branch by the name test/miwarren/memberLead , but had orphan local branches (ie branches that were merged and then deleted on remote), which prevented that:我试图创建一个名为test/miwarren/memberLead ,但有孤立的本地分支(即合并然后在远程删除的分支),这阻止了:

$ git checkout -b test/miwarren/memberLead
fatal: cannot lock ref 'refs/heads/test/miwarren/memberLead': 'refs/heads/test/miwarren/memberLead/dob' exists; cannot create 'refs/heads/test/miwarren/memberLead'

I created branch for each member lead test sub-suite that I was working on at the time.我为当时正在处理的每个成员主要测试子套件创建了分支。

How to find and delete them all, so that I can create my branch?如何找到并删除它们,以便我可以创建我的分支?

The line of code that's going to get the job done将完成工作的代码行

Assuming that you have no other branches that match that pattern, that you need, on local or remote, you can just say:假设您没有其他与该模式匹配的分支,您需要在本地或远程,您可以说:

git for-each-ref --format='%(refname:lstrip=2)' 'refs/heads/test/miwarren/memberLead/*' | xargs git branch -d

To anyone reading this to apply this to your situation, make sure to replace test/miwarren/memberLead with the name of your branch, on which you face issue when trying to create!对于阅读本文以将其应用于您的情况的任何人,请确保将test/miwarren/memberLead替换为您的分支名称,您在尝试创建时会遇到问题! This work iff the branch you're trying to create is substring of the orphan branches that are giving you issue.如果您尝试创建的分支是给您提供问题的孤立分支的子字符串,那么这项工作就行了。

Explanation解释

We get the list of refs that match the pattern 'refs/heads/[your_branch_name]' with git for-each-ref , and map it to branch name, on the left-hand side, and then pipe that to git branch -d on the right-hand side.我们得到匹配模式'refs/heads/[your_branch_name]'git for-each-ref的 refs 列表,并将其映射到左侧的分支名称,然后将其通过管道传输到git branch -d在右手侧。

Left-hand side of it它的左侧

git for-each-ref 'refs/heads/[your_branch_name] , out of the box, returns all the refs that match the specified pattern. git for-each-ref 'refs/heads/[your_branch_name] ,开箱即用,返回与指定模式匹配的所有引用。 The --format='%(refname:lstrip=2)' flag tells that command "format the output, by stripping the first two words from it". --format='%(refname:lstrip=2)'标志告诉该命令“通过--format='%(refname:lstrip=2)'前两个单词来格式化输出”。 Surprise, surprise, the output equals the branch names.惊喜,惊喜,输出等于分支名称。

Right hand side of it它的右手边

Not much going on.没有太多事情发生。 Here's the official description of xargs .这是xargs的官方描述 git branch -d [your_branch_name] deletes the branch [your_branch_name] . git branch -d [your_branch_name]删除分支[your_branch_name]

Use this command使用这个命令

git branch | grep [PATTERN] | xargs git branch -D

These are three commands piped together.这是通过管道连接在一起的三个命令。 Here's how they work:以下是它们的工作原理:

  1. git branch prints a list of branches. git branch打印一个git branch列表。 It precedes the current branch with an asterisk.它在当前分支之前带有一个星号。
  2. grep [PATTERN] searches the list and returns those lines that match the pattern. grep [PATTERN]搜索列表并返回与模式匹配的那些行。
  3. xargs gives us the returned list from grep, and then we pass those to git branch -D , which deletes the branch. xargs为我们提供了从 grep 返回的列表,然后我们将它们传递给git branch -D ,它删除了分支。

Reference参考

https://www.seancdavis.com/blog/git-delete-multiple-local-branches/ https://www.seancdavis.com/blog/git-delete-multiple-local-branches/

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

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