【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 <分支名>
相关推荐
wdfk_prog14 小时前
`git rm --cached`:如何让文件“脱离”版本控制
大数据·linux·c语言·笔记·git·学习·elasticsearch
Mark_Aussie15 小时前
本地项目上传到Git仓库
git
骚饼19 小时前
Git 命令配置别名、Git命令缩写(Mac版)
前端·git
SStone_TJ2 天前
【常用的git命令】
git
没有鸡汤吃不下饭2 天前
Git将某个分支合并到开发(dev)、测试(test)后突然想撤销该分支的功能,怎么处理?
前端·git·github
康一夏2 天前
git fatal:Server aborted the SSL handshake
git·网络协议·ssl
Vio7252 天前
在IntelliJ IDEA中使用Git
git
Net_Walke2 天前
git 的常用命令
git·物联网·github·iot
L X..3 天前
Git 无法访问 GitHub(Recv failure: Connection was reset)问题解决教程
git·github
建群新人小猿3 天前
客户标签自动管理:标签自动化运营,画像持久保鲜
android·java·大数据·前端·git