[英]Git BASH delete all local branches matching a pattern
我試圖創建一個名為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'
我為當時正在處理的每個成員主要測試子套件創建了分支。
如何找到並刪除它們,以便我可以創建我的分支?
假設您沒有其他與該模式匹配的分支,您需要在本地或遠程,您可以說:
git for-each-ref --format='%(refname:lstrip=2)' 'refs/heads/test/miwarren/memberLead/*' | xargs git branch -d
對於閱讀本文以將其應用於您的情況的任何人,請確保將test/miwarren/memberLead
替換為您的分支名稱,您在嘗試創建時會遇到問題! 如果您嘗試創建的分支是給您提供問題的孤立分支的子字符串,那么這項工作就行了。
我們得到匹配模式'refs/heads/[your_branch_name]'
和git for-each-ref的 refs 列表,並將其映射到左側的分支名稱,然后將其通過管道傳輸到git branch -d
在右手側。
git for-each-ref 'refs/heads/[your_branch_name]
,開箱即用,返回與指定模式匹配的所有引用。 --format='%(refname:lstrip=2)'
標志告訴該命令“通過--format='%(refname:lstrip=2)'
前兩個單詞來格式化輸出”。 驚喜,驚喜,輸出等於分支名稱。
沒有太多事情發生。 這是xargs
的官方描述。 git branch -d [your_branch_name]
刪除分支[your_branch_name]
。
git branch | grep [PATTERN] | xargs git branch -D
這是通過管道連接在一起的三個命令。 以下是它們的工作原理:
git branch
打印一個git branch
列表。 它在當前分支之前帶有一個星號。grep [PATTERN]
搜索列表並返回與模式匹配的那些行。xargs
為我們提供了從 grep 返回的列表,然后我們將它們傳遞給git branch -D
,它刪除了分支。https://www.seancdavis.com/blog/git-delete-multiple-local-branches/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.