標籤:
轉自:http://zengrong.net/post/1746.htm
- 本站文章除註明轉載外,均為本站原創或者翻譯。
- 本站文章歡迎各種形式的轉載,但請18歲以上的轉載者註明文章出處,尊重我的勞動,也尊重你的智商;
- 本站部分原創和翻譯文章提供markdown格式源碼,歡迎使用文章源碼進行轉載;
- 本部落格採用 WPCMD 維護;
- 本文標題:Git查看、刪除、重新命名遠程分支和tag
- 本文連結:http://zengrong.net/post/1746.htm
Git查看、刪除、重新命名遠程分支和tag
- 2015-06-15:加入姊妹篇;
- 2013-11-06:加入重新命名遠程分支的內容;
- 2013-01-09:加入刪除遠程tag的內容。
姊妹篇:使用Git、Git GUI和TortoiseGit
這篇文章記錄我在使用git的過程中碰到遠程分支和tag的相關內容,提綱:
- 查看遠程分支
- 刪除遠程分支和tag
- 刪除不存在對應遠程分支的本地分支
- 重新命名遠程分支
- 把本地tag推送到遠程
- 擷取遠程tag
查看遠程分支
加上-a參數可以查看遠程分支,遠程分支會用紅色表示出來(如果你開了顏色支援的話):
$ git branch -a master remote tungway v1.52* zrong remotes/origin/master remotes/origin/tungway remotes/origin/v1.52 remotes/origin/zrong
刪除遠程分支和tag
在Git v1.7.0 之後,可以使用這種文法刪除遠程分支:
$ git push origin --delete <branchName>
刪除tag這麼用:
git push origin --delete tag <tagname>
否則,可以使用這種文法,推送一個空分支到遠程分支,其實就相當於刪除遠程分支:
git push origin :<branchName>
這是刪除tag的方法,推送一個空tag到遠程tag:
git tag -d <tagname>git push origin :refs/tags/<tagname>
兩種文法作用完全相同。
刪除不存在對應遠程分支的本地分支
假設這樣一種情況:
- 我建立了本地分支b1並pull到遠程分支
origin/b1;
- 其他人在本地使用fetch或pull建立了本地的b1分支;
- 我刪除了
origin/b1 遠程分支;
- 其他人再次執行fetch或者pull並不會刪除這個他們本地的
b1 分支,運行git branch -a 也不能看出這個branch被刪除了,如何處理?
使用下面的代碼查看b1的狀態:
$ git remote show origin* remote origin Fetch URL: [email protected]:xxx/xxx.git Push URL: [email protected]:xxx/xxx.git HEAD branch: master Remote branches: master tracked refs/remotes/origin/b1 stale (use ‘git remote prune‘ to remove) Local branch configured for ‘git pull‘: master merges with remote master Local ref configured for ‘git push‘: master pushes to master (up to date)
這時候能夠看到b1是stale的,使用 git remote prune origin 可以將其從本地版本庫中去除。
更簡單的方法是使用這個命令,它在fetch之後刪除掉沒有與遠程分支對應的本地分支:
git fetch -p
重新命名遠程分支
在git中重新命名遠程分支,其實就是先刪除遠程分支,然後重新命名本地分支,再重新提交一個遠程分支。
例如下面的例子中,我需要把 devel 分支重新命名為 develop 分支:
$ git branch -av* devel 752bb84 Merge pull request #158 from Gwill/devel master 53b27b8 Merge pull request #138 from tdlrobin/master zrong 2ae98d8 modify CCFileUtils, export getFileData remotes/origin/HEAD -> origin/master remotes/origin/add_build_script d4a8c4f Merge branch ‘master‘ into add_build_script remotes/origin/devel 752bb84 Merge pull request #158 from Gwill/devel remotes/origin/devel_qt51 62208f1 update .gitignore remotes/origin/master 53b27b8 Merge pull request #138 from tdlrobin/master remotes/origin/zrong 2ae98d8 modify CCFileUtils, export getFileData
刪除遠程分支:
$ git push --delete origin develTo [email protected]:zrong/quick-cocos2d-x.git - [deleted] devel
重新命名本地分支:
git branch -m devel develop
推送本地分支:
$ git push origin developCounting objects: 92, done.Delta compression using up to 4 threads.Compressing objects: 100% (48/48), done.Writing objects: 100% (58/58), 1.38 MiB, done.Total 58 (delta 34), reused 12 (delta 5)To [email protected]:zrong/quick-cocos2d-x.git * [new branch] develop -> develop
然而,在 github 上操作的時候,我在刪除遠程分支時碰到這個錯誤:
$ git push --delete origin develremote: error: refusing to delete the current branch: refs/heads/develTo [email protected]:zrong/quick-cocos2d-x.git ! [remote rejected] devel (deletion of the current branch prohibited)error: failed to push some refs to ‘[email protected]:zrong/quick-cocos2d-x.git‘
這是由於在 github 中,devel 是項目的預設分支。要解決此問題,這樣操作:
- 進入 github 中該項目的 Settings 頁面;
- 設定 Default Branch 為其他的分支(例如 master);
- 重新執行刪除遠程分支命令。
把本地tag推送到遠程
git push --tags
擷取遠程tag
git fetch origin tag <tagname>
參考文章
- https://makandracards.com/makandra/621-git-delete-a-branch-local-or-remote
- http://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-both-locally-and-in-github
- http://www.cnblogs.com/deepnighttwo/archive/2011/06/18/2084438.html
- http://stackoverflow.com/questions/14040754/deleting-remote-master-branch-refused-due-to-being-current-branch
- http://weli.iteye.com/blog/1441582
Git查看、刪除、重新命名遠程分支和tag【轉】