Git查看、刪除、重命名遠程分支和tag
- 本文鏈接:http://zengrong.net/post/1746.htm
這篇文章記錄我在使用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]
這時候能夠看到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 devel
To [email protected]:zrong/quick-cocos2d-x.git
- [deleted] devel
重命名本地分支:
git branch -m devel develop
推送本地分支:
$ git push origin develop
Counting 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 devel
remote: error: refusing to delete the current branch: refs/heads/devel
To [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
關聯文章
- 修改了SSH默認端口之後,如何配置git?
- 安裝gitolite
- 在CentOS上編譯安裝Ruby 1.9
- 讀寫FREByteArray
- 對RTMP視頻流進行BitmapData.draw()出錯的解決辦法
Git查看、刪除、重命名遠程分支和tag