当前位置: 首页 > backend >正文

使用命令行拉取 Git 仓库

1. 克隆远程仓库(首次获取)


# 克隆仓库到当前目录(默认使用 HTTPS 协议)
git clone https://github.com/用户名/仓库名.git# 克隆仓库到指定目录
git clone https://github.com/用户名/仓库名.git 自定义目录名# 使用 SSH 协议克隆(需要配置 SSH 密钥)
git clone git@github.com:用户名/仓库名.git# 克隆指定分支
git clone -b 分支名 https://github.com/用户名/仓库名.git

示例:


git clone https://github.com/octocat/Spoon-Knife.git
git clone -b develop git@github.com:vuejs/vue.git my-vue-project

2. 拉取最新代码(已有本地仓库)


# 进入本地仓库目录
cd 你的仓库目录# 拉取当前分支最新代码(默认拉取 origin 远程)
git pull# 拉取指定远程的指定分支
git pull origin 分支名

3. 关联远程仓库(本地已有项目)


# 初始化本地仓库
git init# 添加远程仓库地址
git remote add origin https://github.com/用户名/仓库名.git# 验证远程仓库是否添加成功
git remote -v# 首次拉取并合并代码
git pull origin master

4. 解决常见问题

权限拒绝(Permission Denied)


# 现象:
ERROR: Repository not found.
fatal: Could not read from remote repository.# 解决方案:
1. 检查 URL 是否正确
2. 使用 SSH 协议需配置密钥:- 生成密钥:ssh-keygen -t ed25519 -C "your_email@example.com"- 将公钥(~/.ssh/id_ed25519.pub)添加到 GitHub/GitLab
3. 或切换回 HTTPS 协议

目录已存在冲突


# 现象:
fatal: destination path 'xxx' already exists and is not an empty directory.# 解决方案:
1. 删除或重命名现有目录
2. 或指定新的目录名:git clone https://github.com/xxx/xxx.git new-folder-name

5. 高级操作

稀疏克隆(只拉取部分目录)


git clone --filter=blob:none --no-checkout https://github.com/xxx/xxx.git
cd xxx
git sparse-checkout init --cone
git sparse-checkout set dir1 dir2
git checkout main

拉取子模块


# 克隆包含子模块的仓库
git clone --recurse-submodules https://github.com/xxx/xxx.git# 已有仓库初始化子模块
git submodule update --init --recursive

各协议对比

协议类型URL 示例特点
HTTPShttps://github.com/user/repo.git无需配置密钥,需输入账号密码
SSHgit@github.com:user/repo.git需配置密钥,无需每次输入密码
GitHub CLIgh repo clone user/repo官方命令行工具,自动认证

常用工作流


# 完整克隆到本地
git clone https://github.com/user/repo.git
cd repo# 创建新分支
git checkout -b feature-branch# 开发完成后提交
git add .
git commit -m "完成新功能"
git push origin feature-branch
http://www.xdnf.cn/news/6459.html

相关文章:

  • 数学复习笔记 9
  • 自学嵌入式 day 18 - 数据结构 1
  • 嵌软面试每日一阅----FreeRTOS
  • SpringBoot实现简单的API代理服务器
  • Sumsub 活体检测与人证对比 Java Demo
  • pytorch训练可视化工具---TensorBoard
  • Linux 防火墙 firewalld 实战配置教程!
  • 将.pt文件执行图像比对
  • Java详解RabbitMQ工作模式之发布订阅模式
  • 具备AI功能的银河麒麟桌面操作系统已正式上市
  • 手搓传染病模型(SEI - SEIAR )
  • xp_cmdshell bcp 导出文件
  • 道通龙鱼系列-混合翼无人机:垂直起降+长时续航
  • 嵌入式自学第二十二天(5.15)
  • 02、基础入门-Spring生态圈
  • 云上玩转 Qwen3 系列之三:PAI-LangStudio x Hologres构建ChatBI数据分析Agent应用
  • 机器学习第十三讲:独热编码 → 把“红黄蓝“颜色变成001/010/100的数字格式
  • 数据结构之图的应用场景及其代码
  • MySQL 用户权限管理:从入门到精通
  • 26考研 | 王道 | 计算机组成原理 | 一、计算机系统概述
  • Java:跨越时代的编程语言传奇
  • 2025年黑客扫段攻击激增:如何构建智能防御体系保障业务安全?
  • Makefile与CMake
  • AI大模型应用:17个实用场景解锁未来
  • 软件设计师考试《综合知识》CPU考点分析(2019-2023年)——求三连
  • 让AI帮我写一个word转pdf的工具
  • 从《西游记》到微调大模型:一场“幻觉”与“认知”的对话20250515
  • 在 VMware 中挂载 U 盘并格式化为 ext4 文件系统的完整指南
  • 企业在蓝海市场有哪些推进目标?
  • 操作系统学习笔记第3章 内存管理(灰灰题库)