【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 <分支名>
相关推荐
AlexMercer10121 天前
Ubuntu从零开始配置Git
c++·git·ubuntu·gitee
胡西风_foxww1 天前
git 添加除了包含特定字符串的文件
git·字符串·文件·add·添加·特定·包含
parade岁月1 天前
Git 凭据管理器原理与多账号管理指南
git
zzzyulin1 天前
git note
git
六件套是我1 天前
【解答疑惑】git执行cherrypick后到另一个分支,然后再合并会出现问题吗?
git
sulikey2 天前
从零配置一个规范的 Python Git 仓库(适用于 Gitee / GitHub)
git·python·pycharm·gitee·github
学渣676562 天前
【面向小白】git rebase全面总结,什么时候用rebase
git
小龙报2 天前
《算法每日一题(1)--- 第31场蓝桥算法挑战赛》
c语言·开发语言·c++·git·算法·学习方法
222you2 天前
idea整合Git
git
今禾2 天前
Git完全指南(下篇):Git高级技巧与问题解决
前端·git·github