git 分支 查看 切换 更新 删除 添加

创建分支

git branch dev

创建+切换分支

git branch -b dev

查看远程分支

加上-a参数可以查看远程分支,远程分支会用红色表示出来(如果你开了颜色支持的话):

$ git branch -a
  master
  remote
  tungway
  v1.52
* zrong
  remotes/origin/master
  remotes/origin/tungway
  remotes/origin/v1.52
  remotes/origin/zrong

查看本地分支

$ git branch
* dev
  master

切换分支

$ git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'

删除远程分支和tag

在Git v1.7.0 之后,可以使用这种语法删除远程分支:

$ git push origin --delete 

删除tag这么用:

git push origin --delete tag 

否则,可以使用这种语法,推送一个空分支到远程分支,其实就相当于删除远程分支:

git push origin :

这是删除tag的方法,推送一个空tag到远程tag:

git tag -d 
git push origin :refs/tags/

两种语法作用完全相同

删除不存在对应远程分支的本地分支##

假设这样一种情况:

我创建了本地分支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 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

把本地tag推送到远程

git push --tags

获取远程tag

git fetch origin tag 

更新远程分支列表

git remote update origin --prune

你可能感兴趣的:(git 分支 查看 切换 更新 删除 添加)