手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Git 如何 clone 非 master 分支的代码

[日期:2017-05-24] 来源:Linux社区  作者:gaohaoyang [字体: ]

问题描述

我们每次使用命令

git clone git@gitlab.xxx.com:xxxxx.git

默认 clone 的是这个仓库的 master 分支。如果最新的代码不在 master 分支上,该如何拿到呢?如下图所示,最新的代码可能在 daily/1.4.1 分支上,我们希望拿到这个分支上的代码。

解决方案

我们在本地先建立一个分支,建议名称和远程的想要同步的分支名称一样。

git branch daily/1.4.1

在切换到这个本地分支

git checkout daily/1.4.1
# Switched to branch 'daily/1.4.1'

接下来就可以去建立上游分支的关联了,但是这个命令比较长,不好记,我们可以直接先 pull 一下,git 会提示我们相应的操作和命令。

git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> daily/1.4.1

我们看到最后一行,执行这个命令,即可完成与上游分支的关联。

git branch --set-upstream-to=origin/daily/1.4.1 daily/1.4.1
# Branch daily/1.4.1 set up to track remote branch daily/1.4.1 from origin.

然后再 pull 一下就好了!

git pull

更多 Git 教程系列文章: 

GitHub 使用教程图文详解  http://www.linuxidc.com/Linux/2014-09/106230.htm 

Git使用图文详细教程  http://www.linuxidc.com/Linux/2016-11/136781.htm

Ubuntu Git安装与使用 http://www.linuxidc.com/Linux/2016-11/136769.htm

Git 标签管理详解 http://www.linuxidc.com/Linux/2014-09/106231.htm 

Git 分支管理详解 http://www.linuxidc.com/Linux/2014-09/106232.htm 

Git 远程仓库详解 http://www.linuxidc.com/Linux/2014-09/106233.htm 

Git 本地仓库(Repository)详解 http://www.linuxidc.com/Linux/2014-09/106234.htm 

Git 服务器搭建与客户端安装  http://www.linuxidc.com/Linux/2014-05/101830.htm 

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm 

分享实用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm 

Git从入门到学会 http://www.linuxidc.com/Linux/2016-10/135872.htm

Git基本操作详解 http://www.linuxidc.com/Linux/2016-10/135691.htm

分布式版本控制系统 Git 详细教程  http://www.linuxidc.com/Linux/2017-05/143747.htm

Git 的详细介绍请点这里
Git 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2017-05/144175.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款