git branch -r #查看远程分支 git branch -a #查看全部分支,本地和远程 git remote show [remote-name] #查看远程仓库信息
其中git remote show [remote-name]展现的信息包括:git
git checkout -b local origin/daily/dev
上面的方法能够直接检出远程分支到本地,在本地新建local分支,并切换到local分支上,注意本地分支和远程分支不一样名。shell
这个方法会自动建立远程分支 /daily/dev
和本地分支local
的跟踪关系, 经过git remote show origin
能够看到包含以下信息:服务器
Local branches configured for 'git pull': local merges with remote /daily/dev master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)
其中
Local branches configured for 'git pull':
下的就是upstream
跟踪分支。工具
能够看出,远程分支 /daily/dev
和本地分支local
创建了git pull
的关系,可是没有创建git push
的关系。此时若是强行push,不会成功,会出现以下提示:gitlab
fatal: The current branch new has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin develop
咱们知道经过git clone git@gitlab.xxx.com:xxxxx.git
能够创建默认的本地master分支和远程master分支的pull和push的关系,可是咱们没法经过clone命令检出非master分支,那么对于非master分支怎么办呢?code
Git中push.default
能够指定在没有明确指定远程分支的状况下,默认push的远程分支,其取值能够是:blog
central / non-central workflows 是Git的两种常见工做流场景:rem
在Git 2.0以前,push.default
的内建值被设为'matching',2.0以后则被更改成了'simple'。get
在了解push.default以后,咱们有以下几种比较好的从远程分支检出本地分支的方法(基于V2.0+):workflow
因此若是你只有一个远程仓库,且你想检出的分支名称和远程分支不一样名(有些管理工具会自动生成比较丑的远程分支名,相似:/features/2017-03-31-featuresA-1),那么你能够经过设置push.default 默认推送到pull的远程分支(upstream 分支):
#检出重命名 git checkout -b dev origin/features/2017-03-31-featuresA-1 #设置push.default为upstream git config --global push.default upstream #or git config push.default upstream #取消设置 git config --unset push.default
若是不想经过修改upstream,那么只能经过设置检出本地分支名称和远程分支名称相同:
git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>
注意:若是使用git checkout -b features/feature_1 origin/features/feature_1
检出,那么远程分支名称是features/feature_1
,而不是origin/features/feature_1
。
这个也不算什么解法,可是强烈推荐,就是创建远程分支的时候,取个好点的名字。
git clone git@gitlab.xxx.com:xxxxx.git #从master创建新分支 git checkout -b dev #push并创建同名远程分支 git push origin dev