• Git 远程分支的pull与push


    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

  • 相关阅读:
    Inception V1-V4
    NDCG的理解
    进程与线程
    Java中的接口和抽象类
    HashMap的工作原理
    基于比较排序的算法复杂度的下界
    数据库-left join,right join,inner join,full join
    外排序 External sorting
    数据流中的中位数 Find Median from Data Stream
    Codeforces Round #272 (Div. 2)
  • 原文地址:https://www.cnblogs.com/chrischennx/p/6653774.html
Copyright © 2020-2023  润新知