【Git多分支使用教程】

Git多分支使用教程

Git多分支使用手册

目录

多分支只拉取一个

如果只需要克隆远程仓库中的单个分支 ,可以使用 --single-branch--branch 选项:

shell 复制代码
git clone --single-branch --branch <目标分支名称> <远程仓库地址>

示例

shell 复制代码
git clone --single-branch --branch feature/beijing-ybss/V1.0/20240717 http://git.sf-express.com/scm/gis-jw/gis-jw-core-databoard.git

多分支拉取指定几个

若需要克隆多个特定分支(避免克隆全部分支),可分两步操作:

步骤 1:克隆第一个分支

shell 复制代码
git clone --single-branch --branch <分支1名称> <远程仓库地址>
cd <项目目录>

示例

shell 复制代码
git clone --single-branch --branch feature/beijing-ybss/V2.0/20250325 http://git.sf-express.com/scm/gis-jw/gis-jw-core-databoard.git
cd gis-jw-core-databoard

步骤 2:获取其他分支

shell 复制代码
# 设置远程仓库关注的分支
git remote set-branches origin <分支2名称>

# 拉取目标分支
git fetch origin <分支2名称>

# 创建并切换到本地分支
git checkout -b <本地分支名> origin/<远程分支名>

示例

shell 复制代码
git remote set-branches origin feature/beijing-ybss/V1.0/20240717
git fetch origin feature/beijing-ybss/V1.0/20240717
git checkout -b feature/beijing-ybss/V1.0/20240717 origin/feature/beijing-ybss/V1.0/20240717

常见问题与解决方法

1. 错误:origin/分支名 is not a commit

原因 :未正确获取远程分支信息。
解决方法

shell 复制代码
git fetch -p origin  # 强制更新远程分支并清理无效引用

2. 分支名称冲突

现象 :本地已存在同名分支。
解决方法

shell 复制代码
git branch -D <冲突分支名>  # 删除本地冲突分支

3. --single-branch 限制

说明 :使用 --single-branch 克隆后,默认无法直接切换到其他分支。
解决方案

shell 复制代码
git remote set-branches origin <目标分支名>  # 告诉Git关注该分支
git fetch origin <目标分支名>
git checkout -b <本地分支名> origin/<目标分支名>

总结

  • 单分支克隆 :使用 --single-branch --branch
  • 多分支克隆 :先克隆主分支,再通过 git remote set-branchesgit fetch 获取其他分支。
  • 分支切换 :使用 git checkout <分支名>
相关推荐
mmsx16 小时前
使用git生成ssh的ed25519密钥
运维·git·ssh
荔枝吻16 小时前
【保姆级喂饭教程】Git图形化客户端Sourcetree安装及使用教程
git·sourcetree
T - mars1 天前
Git在Pycharm中的使用
git
sunarmy1 天前
curl: (56) OpenSSL SSL_read: Connection reset by peer, errno 104
git
典学长编程1 天前
高效学习之一篇搞定分布式管理系统Git !
大数据·git·搜索引擎
是2的10次方啊1 天前
.gitignore失效自救指南:原理揭秘与工程师实战排查全流程
git
海外空间恒创科技1 天前
香港站群服务器与普通香港服务器对比
服务器·git·github
Wetoria1 天前
管理 git 分支时,用 merge 还是 rebase?
前端·git
泰勒朗斯2 天前
如何在新机器上设置github完成内容git push
git·github
小妖6662 天前
git branch -a 还有一些已经删除了的分支
git