git刪除遠程分支和tag相關命令_git操作

來源:互聯網
上載者:User
這篇文章記錄我在使用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: git@github.com:xxx/xxx.git
  Push  URL: git@github.com: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 devel
To git@github.com: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 git@github.com: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 git@github.com:zrong/quick-cocos2d-x.git
 ! [remote rejected] devel (deletion of the current branch prohibited)
error: failed to push some refs to 'git@github.com:zrong/quick-cocos2d-x.git'
這是由於在 github 中,devel 是項目的預設分支。要解決此問題,這樣操作:


進入 github 中該項目的 Settings 頁面;
設定 Default Branch 為其他的分支(例如 master);
重新執行刪除遠程分支命令。
把本地tag推送到遠程


git push --tags
擷取遠程tag


git fetch origin tag <tagname>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.