【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 <分支名>
相关推荐
飏旎5 小时前
git pull和git fetch的区别
git
z涛.9 小时前
git的使用
git
大卫小东(Sheldon)14 小时前
智能生成git提交消息工具 GIM 发布 1.7 版本了
git·ai·rust
慧都小项17 小时前
UI测试平台TestComplete如何实现从Git到Jenkins的持续测试
git·ui·jenkins·代码质量·testcomplete·zephyr for jira
可曾去过倒悬山1 天前
Mac上优雅简单地使用Git:从入门到高效工作流
git·elasticsearch·macos
穗 禾1 天前
github与git新手教程(快速访问github)
网络·git·github
我不是程序猿儿1 天前
【git】在 GitLab 上如何把 A 分支(如 feature/xxx)合并到 B 分支(如 trunk)
服务器·git·gitlab
是芽芽哩!1 天前
GitLab 仓库 — 常用的 git 命令
git·gitlab
用手码出世界1 天前
Git之本地仓库管理
git·gitee
言慢行善1 天前
对git 熟悉时,常用操作
git