2013-03-31 45 views
0

我對git比較陌生。我使用bitbucket作爲我的遠程存儲庫。在嘗試將所有分支轉換爲標籤之後,我處於一種混亂的情況。我處於一個狀態,在bitbucket上我有與分支名稱相同的標籤。我想刪除多餘的遠程分支。重命名本地不存在的遠程分支

在本地,我有我想要的:

> git branch 
* master 

> git tag 
1.1.0 
1.2.0 
1.3.0 
1.3.1 
1.3.2 
2.0.1 

到目前爲止好。

在到位桶,但是,我有:

分支:

1.1.0 
1.2.0 
1.3.0 
1.3.1 
1.3.2 
2.0.1 
master 

標籤:

1.1.0 
1.2.0 
1.3.0 
1.3.1 
1.3.2 
2.0.1 

我想刪除除主所有遠程分支。我該怎麼做呢?

謝謝。

回答

1

要刪除任何被命名爲「主」,在遠程倉庫你推什麼給它—例如遠程對象,

git push origin :master 

將刪除。

現在的事實是,這裏的「主人」只是一個快捷鍵,該參考文件的全名是「refs/heads/master」。標籤位於「refs/tags」命名空間中,因此如果您碰巧在遠程存儲庫中同時擁有分支和名爲「foo」的標籤,則可以使用要刪除的對象的全名來消除任何歧義。

血淋淋的細節描述在gitrevisions(7) manual page

所以,刪除所有遠程分支機構,除了主人,你必須做這樣的事情:

git push origin :refs/heads/1.1.0 :refs/heads/1.2.0 ... 

您可以嘗試通過使用一個小殼的黑客攻擊,使這個少乏味:

$ (while read b; do echo :refs/heads/$b; done | xargs git push origin) 
1.1.0 
1.2.0 
1.3.0 
1.3.1 
1.3.2 
2.0.1 
^D 

(^ D這裏的意思是按ctrl-d來標示子殼的輸入結束)。


P.S. 順便說一句,我認爲,由於獲取的Git支持通配符refspecs時,像

git fetch origin '+refs/heads/*:refs/heads/*' 

,將用於推動工作,太。但好像它不—至少

git push origin ':refs/tags/*' 

似乎不適合我使用Git 1.8.1工作。

0

的語法來刪除遠程分支/標籤:

git push REMOTE_NAME :BRANCH_NAME 

我真的不知道,如果標籤或分支機構有偏好,如果他們有相同的名稱。

所以更具體,你可以指定它是使用refs/heads的Refspec符的一個分支:

git push REMOTE_NAME :refs/heads/BRANCH_NAME 

假設您的遠程名爲origin,這個命令應該刪除你提到的所有分支:

for branch_name in 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 2.0.1; do 
    git push origin :refs/heads/$branch_name 
done 
+0

是的,意識到並編輯我的答案就在您發佈評論之前。 – Tuxdude

2
git branch -m old_branch new_branch   # Rename branch locally  
git push origin :old_branch     # Delete the old branch  
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote 
相關問題