赞
踩
https://blog.zengrong.net/post/1746.html
这篇文章记录我在使用git的过程中碰到远程分支和tag的相关内容,提纲:
加上-a参数可以查看远程分支,远程分支会用红色表示出来(如果你开了颜色支持的话):
1 2 3 4 5 6 7 8 9 10 | $ git branch -a master remote tungway v1.52 * zrong remotes/origin/master remotes/origin/tungway remotes/origin/v1.52 remotes/origin/zrong |
在Git v1.7.0 之后,可以使用这种语法删除远程分支:
1 | $ git push origin --delete <branchName> |
删除tag这么用:
1 | git push origin --delete tag <tagname> |
否则,可以使用这种语法,推送一个空分支到远程分支,其实就相当于删除远程分支:
1 | git push origin :<branchName> |
这是删除tag的方法,推送一个空tag到远程tag:
1 2 | git tag -d <tagname> git push origin :refs/tags/<tagname> |
两种语法作用完全相同。
假设这样一种情况:
origin/b1
;origin/b1
远程分支;b1
分支,运行 git branch -a
也不能看出这个branch被删除了,如何处理?使用下面的代码查看b1的状态:
1 2 3 4 5 6 7 8 9 10 11 12 | $ 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之后删除掉没有与远程分支对应的本地分支:
1 | git fetch -p |
在git中重命名远程分支,其实就是先删除远程分支,然后重命名本地分支,再重新提交一个远程分支。
例如下面的例子中,我需要把 devel 分支重命名为 develop 分支:
1 2 3 4 5 6 7 8 9 10 | $ 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 |
删除远程分支:
1 2 3 | $ git push --delete origin devel To git@github.com:zrong/quick-cocos2d-x.git - [deleted] devel |
重命名本地分支:
1 | git branch -m devel develop |
推送本地分支:
1 2 3 4 5 6 7 8 | $ 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 上操作的时候,我在删除远程分支时碰到这个错误:
1 2 3 4 5 | $ 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 是项目的默认分支。要解决此问题,这样操作:
1 | git push --tags |
1 | git fetch origin tag <tagname> |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。