Git 远程分支的pull与push
2017-03-31 22:00
176 查看
Git 远程分支的pull与push
远程分支信息查看
git branch -r #查看远程分支 git branch -a #查看所有分支,本地和远程 git remote show [remote-name] #查看远程仓库信息
其中git remote show [remote-name]展示的信息包括:
会列出远程仓库的 URL 与跟踪分支的信息
列出了当你在特定的分支上执行 git push 会自动地推送到哪一个远程分支
列出了哪些远程分支不在你的本地
哪些远程分支已经从服务器上移除了
执行 git pull 时哪些分支会自动合并
……
检出远程非master分支到本地
git checkout -b local origin/daily/dev
上面的方法可以直接检出远程分支到本地,在本地新建local分支,并切换到local分支上,注意本地分支和远程分支不同名。
这个方法会自动创建远程分支
/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,不会成功,会出现如下提示:
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默认push设置
我们知道通过git clone git@gitlab.xxx.com:xxxxx.git可以建立默认的本地master分支和远程master分支的pull和push的关系,但是我们无法通过clone命令检出非master分支,那么对于非master分支怎么办呢?
Git中
push.default可以指定在没有明确指定远程分支的情况下,默认push的远程分支,其取值可以是:
nothing - push操作无效,除非显式指定远程分支(想让push变得简单的就不要用这个)
current - push当前分支到远程同名分支,如果远程同名分支不存在则自动创建同名分支(central 和 non-central workflows都适用)
upstream - push当前分支到它的upstream分支上(通常用于central workflow)
simple - simple和upstream是相似的(通常用于central workflow),只有一点不同,simple必须保证本地分支和它的远程 upstream分支同名,否则会拒绝push操作
matching - push所有本地和远程两端都存在的同名分支
central / non-central workflows 是Git的两种常见工作流场景:
central workflows - 集中式工作流,一个分支的push和pull都是同一个远程仓库
non-central workflows - 非集中式工作流,一个分支的push和pull可能分别都有不同的远程仓库
在Git 2.0之前,
push.default的内建值被设为'matching',2.0之后则被更改为了'simple'。
在了解push.default之后,我们有如下几种比较好的从远程分支检出本地分支的方法(基于V2.0+):
解法一
所以如果你只有一个远程仓库,且你想检出的分支名称和远程分支不同名(有些管理工具会自动生成比较丑的远程分支名,类似:/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
参考
Git push与pull的默认行为Git config
相关文章推荐
- Git 远程分支的pull与push
- 拾遗:git pull 与 push 远程分支与本地分支顺序识别问题
- Git 远程分支的pull与push
- git的操作(拉代码到本地/commit到本地/pull/push到远程/新建分支/合并分支/)
- (git fetch git push git pull)远程本地分支互相推送更新
- git push时提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支
- [Github] git push "提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。"的解决办法
- Git的纯命令操作,Install,Clone , Commit,Push,Pull,版本回退,撤销更新,分支的创建/切换/更新/提交/合并,代码冲突
- git push & git pull 推送/拉取分支
- git 拉取远程指定分支 pull本地不存在的分支
- git pull远程所有分支
- Git的纯命令操作,Install,Clone , Commit,Push,Pull,版本回退,撤销更新,分支的创建/切换/更新/提交/合并,代码冲突
- 解决git push远程分支错误
- git如何push到远程分支
- git 修改远程pull和push地址
- git 远程操作 git pull git push
- git push & git pull 推送/拉取分支
- eclipse导入远程git代码及(push、pull、及maven工程导入)
- eclipse导入远程git代码及(push、pull、及maven工程导入)
- git push & git pull 推送/拉取分支