本文最后更新于 2025年10月16日 凌晨
重置git仓库以精简Github和Vercel空间大小
一、精简项目大小
完整备份项目文件夹
非常重要!在进行操作前一定要完整备份!
删除本地.git文件夹:
- 打开项目文件夹
- 确保显示隐藏文件(查看 → 勾选”隐藏的项目”)
- 右键删除
.git
文件夹(永久删除,不进回收站)
初始化仓库:
1 2 3 4 5 6
| cd E:\Hexo\H█n██mg\im██.github.io cd e:
git init
|
添加 .vercelignore
文件(项目根目录):
准备基础结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| mkdir im██.github.io-skeleton cd im██.github.io-skeleton
cd E:\Hexo\H█n██mg\im██.github.io-skeleton cd e:
mkdir img\2020 img\2021 img\2022 img\2023 img\2024 img\2025 mkdir img\2020\01 mkdir img\2020\02
git config user.name git config user.email
git init git add . git commit -m "初始化仓库结构"
|
- 上传骨架仓库(强制覆盖)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| git log
git remote add origin git@github.com:im██/im██.github.io.git
git remote -v
git push -f origin master
git branch --set-upstream-to=origin/master master
git remote show origin
|
- 按年份分批推送(示例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| cd ..\im██.github.io
git remote add origin git@github.com:im██/im██.github.io.git
git add .gitattributes git add .gitignore git add .vercelignore git add CNAME git add index.html git add README.md git commit -m "添加图库核心文件" git push -f origin master
git add img/2020 git commit -m "添加2020年图片" git push -f origin master
git reset .
git add img/2021 git commit -m "添加2021年图片" git push -f origin master
|
- 后续年份添加完成后
1 2 3
| git ls-tree -r HEAD --name-only
|
- 完成最后一步(清除残留状态)
1 2 3 4 5 6 7 8 9 10 11 12
| git reset --hard HEAD
git fetch origin
git branch --set-upstream-to=origin/master master
git status
|
- 若部分图片无法访问
1 2 3 4 5 6 7 8 9
| git add img/2025 git commit -m "重新推送2025年更新" git push origin master
git update-index --add --cacheinfo 100644 $(git hash-object -w 路径/文件名.jpg) 路径/文件名.jpg git commit -m "修复单文件" git push origin master
|
二、使用 SSH 链接
- 验证 GitHub 连接状态
1 2 3 4 5
| curl -v https://github.com
ssh -T git@github.com
|
如果提示 Connection timed out
或 Could not resolve host
则确认是网络问题。
- 使用 SSH 协议(推荐)
1 2
| ssh-keygen -t imgr2net -C "█e█a█n█@█ail.com"
|
会在 .ssh 文件夹下生成 im██_id (私钥)和 im██_id.pub (公钥)两个文件。
- 添加到 GitHub
- 复制公钥内容
- 登录GitHub -> Settings -> SSH and GPG keys -> New SSH key
- 粘贴公钥内容,命名如 “im██-work-key”
- 在config文件中添加
1 2 3 4 5 6
| Host github.com HostName ssh.github.com Port 443 User git IdentityFile ~/.ssh/im██_id IdentitiesOnly yes
|
在 .ssh 目录创建配置文件(首次使用需创建config文件)
- 检查当前密钥权限
- 使用 SSH 协议代替 HTTPS
1 2 3 4 5 6 7 8
| git remote rm origin
git remote add origin git@github.com:im██/im██.github.io.git
git remote -v
|
既然 SSH 连接正常,直接改用 SSH 协议进行操作。
- SSH Config 隔离账户(额外操作)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # 账户1:imgr2net(图库专用) Host im██-github HostName github.com Port 443 User git IdentityFile ~/.ssh/im██_id IdentitiesOnly yes
# 账户2:█e█a█n█(其他项目) Host █e█a█n█-github HostName github.com Port 443 User git IdentityFile ~/.ssh/█e█a█n█_id IdentitiesOnly yes
|
如果需要管理两个github,则config添加以上内容。然后切换项目文件夹,在各自的文件夹下分别按第6步执行。
git remote set-url origin git@github.com:im██/im██.github.io.git 另外需要把 github.com 改成 Host 后面设置的别名,例如 im██-github 。
三、使用令牌链接
- 令牌创建步骤
1
| 输入一个有意义的名称(如:"im██-image-repo")
|
1
| 选择 "无到期" 或自定义日期(建议选择较长时间)
|
- Repository access (仓库访问权限)
1 2
| ✅ 选择 "Only select repositories" └ 在下方仓库列表中勾选 "im██/im██.github.io"
|
- Permissions (权限) 按最小权限原则配置
1 2 3 4 5 6 7
| 📁 Repository permissions: ▪ Contents: ✅ Read and write(必须!用于上传图片) ▪ Metadata: ✅ Read-only ▪ 其他权限: 保持未选中
📦 Account permissions: ▪ 所有权限: ❌ 不勾选(图库不需要账户级权限)
|
1
| 点击底部绿色 "Generate token" 按钮
|
- 令牌创建成功后
1 2 3 4 5 6 7 8 9
|
git remote set-url origin https://im██:ghp_xxxxxxxx@github.com/im██/im██.github.io.git
git push -f origin master
|
四、诊断工具
- 验证端口连接
1 2 3 4 5 6 7 8
| tnc ssh.github.com -Port 443
curl -v https://github.com --proxy http://127.0.0.1:23457
git push -u origin master -v
|
- 修复 SSL 证书路径
1 2 3 4 5 6
| git config --global http.sslBackend schannel
git config --global --get http.sslBackend
|