Git查看、刪除、重新命名遠程分支和tag【轉】

來源:互聯網
上載者:User

標籤:

轉自: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的相關內容,提綱:

  1. 查看遠程分支
  2. 刪除遠程分支和tag
  3. 刪除不存在對應遠程分支的本地分支
  4. 重新命名遠程分支
  5. 把本地tag推送到遠程
  6. 擷取遠程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>

兩種文法作用完全相同。

刪除不存在對應遠程分支的本地分支

假設這樣一種情況:

  1. 我建立了本地分支b1並pull到遠程分支 origin/b1;
  2. 其他人在本地使用fetch或pull建立了本地的b1分支;
  3. 我刪除了 origin/b1 遠程分支;
  4. 其他人再次執行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 是項目的預設分支。要解決此問題,這樣操作:

  1. 進入 github 中該項目的 Settings 頁面;
  2. 設定 Default Branch 為其他的分支(例如 master);
  3. 重新執行刪除遠程分支命令。
把本地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【轉】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.