【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 <分支名>
相关推荐
疯狂的沙粒3 小时前
如何通过git命令查看项目连接的仓库地址?
大数据·git·elasticsearch
qq_254617774 小时前
Gerrit+repo管理git仓库,如果本地有新分支不能执行repo sync来同步远程所有修改,会报错
git
π大星星️5 小时前
Git分布式版本控制工具
分布式·git
kingbal5 小时前
IDEA:配置 Git 需要完成 Git 路径设置、账号认证以及仓库关联三个主要步骤
git·idea
司徒小夜17 小时前
处理git没做修改,但是文件显示变更的情况
git
m0_7493175219 小时前
vscode里如何用git
ide·git·vscode
Hello.Reader19 小时前
Git 安装全攻略Linux、macOS、Windows 与源码编译
linux·git·macos
王景程20 小时前
SELinux是什么以及如何编写SELinux策略
git·github
Chuncheng's blog20 小时前
如何基于Mihomo Party http端口配置git与bash命令行代理
git·http·bash
Tom Boom20 小时前
Git常用命令完全指南:从入门到精通
大数据·git·elasticsearch·docker·自动化测试框架