Skip to content

GitHub 使用指南

本文介绍如何在智川云实例中高效使用 GitHub 进行代码管理。

Git 基础配置

设置用户信息

bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

查看配置

bash
git config --list

克隆仓库

基本克隆

bash
git clone https://github.com/username/repo.git

克隆到指定目录

bash
git clone https://github.com/username/repo.git /root/rivermind-data/projects/repo

浅克隆(节省空间和时间)

bash
# 只克隆最近一次提交
git clone --depth 1 https://github.com/username/repo.git

# 克隆指定分支
git clone --depth 1 -b main https://github.com/username/repo.git

加速访问

由于网络原因,直接访问 GitHub 可能较慢或不稳定。推荐使用 GitHub 代理服务加速访问。

可用代理服务

可在 ghproxy.link 站点查看并选择当前可用的加速域名。更多说明请参考 学术资源加速

以下为示例(实际可用域名请以网站显示为准):

代理地址说明
https://ghfast.top示例域名,请查看网站

支持的功能

  • 终端命令行:git clonewgetcurl 等工具下载
  • 支持 raw.githubusercontent.comgist.github.comgist.githubusercontent.com 文件下载

注意

不支持 SSH Key 方式 git clone 下载,请使用 HTTPS 方式

git clone 加速

bash
# 基本用法
git clone https://ghfast.top/https://github.com/username/repo.git

# 示例
git clone https://ghfast.top/https://github.com/stilleshan/dockerfiles

克隆私有仓库

克隆私有仓库需要在 GitHub Personal access tokens 申请 Token 配合使用:

bash
git clone https://user:your_token@ghfast.top/https://github.com/your_name/your_private_repo

wget & curl 加速

bash
# wget 下载仓库压缩包
wget https://ghfast.top/https://github.com/stilleshan/dockerfiles/archive/master.zip

# wget 下载 Raw 文件
wget https://ghfast.top/https://raw.githubusercontent.com/stilleshan/dockerfiles/main/README.md

# curl 下载仓库压缩包
curl -O https://ghfast.top/https://github.com/stilleshan/dockerfiles/archive/master.zip

# curl 下载 Raw 文件
curl -O https://ghfast.top/https://raw.githubusercontent.com/stilleshan/dockerfiles/main/README.md

支持的链接类型

类型示例链接
Raw 文件https://raw.githubusercontent.com/stilleshan/dockerfiles/main/README.md
分支源码https://github.com/stilleshan/dockerfiles/archive/master.zip
Releases 源码https://github.com/fatedier/frp/archive/refs/tags/v0.54.0.zip
Releases 文件https://github.com/fatedier/frp/releases/download/v0.54.0/frp_0.54.0_linux_amd64.tar.gz

配置 Git 代理

如果你有自己的代理服务器,也可以配置 Git 使用代理:

bash
# 设置代理
git config --global http.proxy http://proxy:port
git config --global https.proxy https://proxy:port

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

SSH 密钥配置

使用 SSH 方式可以免密码操作私有仓库。

生成密钥

bash
ssh-keygen -t ed25519 -C "your.email@example.com"

按提示操作,默认保存到 ~/.ssh/id_ed25519

添加到 GitHub

bash
# 查看公钥
cat ~/.ssh/id_ed25519.pub

复制输出内容,到 GitHub → Settings → SSH and GPG keys → New SSH key 添加。

测试连接

bash
ssh -T git@github.com

使用 SSH 克隆

bash
git clone git@github.com:username/repo.git

常用操作

拉取更新

bash
git pull origin main

提交更改

bash
git add .
git commit -m "描述你的更改"
git push origin main

切换分支

bash
# 查看分支
git branch -a

# 切换分支
git checkout branch-name

# 创建并切换
git checkout -b new-branch

查看状态

bash
git status
git log --oneline -10

下载 Release 文件

使用 wget

bash
# 直接下载
wget https://github.com/user/repo/releases/download/v1.0/file.zip

# 加速下载
wget https://ghfast.top/https://github.com/user/repo/releases/download/v1.0/file.zip

使用 curl

bash
curl -L -O https://github.com/user/repo/releases/download/v1.0/file.zip

下载单个文件

Raw 文件

bash
# 直接下载
wget https://raw.githubusercontent.com/user/repo/main/path/to/file.py

# 加速下载
wget https://ghfast.top/https://raw.githubusercontent.com/user/repo/main/path/to/file.py

大文件处理

Git LFS

如果仓库使用了 Git LFS 管理大文件:

bash
# 安装 Git LFS
apt-get install git-lfs

# 初始化
git lfs install

# 克隆时自动下载 LFS 文件
git clone https://github.com/user/repo.git

# 或单独拉取 LFS 文件
git lfs pull

跳过 LFS 文件

bash
GIT_LFS_SKIP_SMUDGE=1 git clone https://github.com/user/repo.git

常见问题

克隆速度慢

  • 使用浅克隆 --depth 1
  • 使用代理加速
  • 只克隆需要的分支

权限被拒绝

  • 检查 SSH 密钥是否正确配置
  • 确认仓库访问权限
  • 尝试使用 HTTPS + Token 方式

文件太大无法推送

  • 使用 Git LFS 管理大文件
  • 将大文件添加到 .gitignore
  • 清理 Git 历史中的大文件

智算无疆 川流不息