重置git仓库以精简Github和Vercel空间大小

本文最后更新于 2025年10月16日 凌晨

重置git仓库以精简Github和Vercel空间大小

一、精简项目大小

  1. 完整备份项目文件夹

    非常重要!在进行操作前一定要完整备份!

  2. 删除本地.git文件夹

    • 打开项目文件夹
    • 确保显示隐藏文件(查看 → 勾选”隐藏的项目”)
    • 右键删除 .git文件夹(永久删除,不进回收站)
  3. 初始化仓库

    1
    2
    3
    4
    5
    6
    # 确保在图库目录
    cd E:\Hexo\H█n██mg\im██.github.io
    cd e:

    # 创建全新的.git文件夹
    git init
  4. 添加 .vercelignore文件(项目根目录):

    1
    2
    .git
    # 添加其他非必要的大文件或目录
  5. 准备基础结构

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 # 应显示 im██
git config user.email # 应显示您的GitHub邮箱

# 如果不是,立即修正
# git config user.name "im██"
# git config user.email "█e█a█n█@█ail.com"

# 初始化仓库
git init
git add .
git commit -m "初始化仓库结构"
  1. 上传骨架仓库(强制覆盖)
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
# 应显示类似: commit [hash] (HEAD -> master) 和 "初始化仓库结构"
# 输入 q 退出log

# 连接远程仓库,@后面的github.com为.ssh中config文件里的Host行设置的别名,需自行替换
git remote add origin git@github.com:im██/im██.github.io.git
# 验证远程地址
git remote -v

# 强制推送(覆盖远程)
git push -f origin master

# 输出成功示例:
# Counting objects: 3, done.
# Writing objects: 100% (3/3), 210 bytes | 210.00 KiB/s, done.
# Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
# To https://github.com/im██/im██.github.io.git
# + 9da4a76...d34db7e master -> master (forced update)

# 设置上游跟踪(仅首次需要)
git branch --set-upstream-to=origin/master master

# 验证远程状态
git remote show origin
  1. 按年份分批推送(示例)
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

# 连接远程仓库,@后面的github.com为.ssh中config文件里的Host行设置的别名,需自行替换
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

# 添加首批内容(2020年图片)
# 只添加2020年
git add img/2020
git commit -m "添加2020年图片"
git push -f origin master

# 添加第二批(2021年图片)
# 重置暂存区
git reset .
# 只添加2021年
git add img/2021
git commit -m "添加2021年图片"
git push -f origin master

# 依次添加其他年份...
  1. 后续年份添加完成后
1
2
3
# 验证推送完成
git ls-tree -r HEAD --name-only
# 应显示所有文件
  1. 完成最后一步(清除残留状态)
1
2
3
4
5
6
7
8
9
10
11
12
# 1.重置本地状态
git reset --hard HEAD

# 2.与远程同步
git fetch origin

# 3.安全设置上游
git branch --set-upstream-to=origin/master master

# 4.验证状态
git status
# 应显示: Your branch is up to date with 'origin/master'
  1. 若部分图片无法访问
1
2
3
4
5
6
7
8
9
# 1.只重新推送问题年份(如2025年)
git add img/2025
git commit -m "重新推送2025年更新"
git push origin master

# 2.文件级修复(单个文件)
git update-index --add --cacheinfo 100644 $(git hash-object -w 路径/文件名.jpg) 路径/文件名.jpg
git commit -m "修复单文件"
git push origin master

二、使用 SSH 链接

  1. 验证 GitHub 连接状态
1
2
3
4
5
# 测试 GitHub 的 HTTPS 连接
curl -v https://github.com

# 测试 SSH 连接
ssh -T git@github.com

如果提示 Connection timed outCould not resolve host 则确认是网络问题。

  1. 使用 SSH 协议(推荐)
1
2
# 生成 SSH 密钥
ssh-keygen -t imgr2net -C "█e█a█n█@█ail.com"

会在 .ssh 文件夹下生成 im██_id (私钥)和 im██_id.pub (公钥)两个文件。

  1. 添加到 GitHub
  • 复制公钥内容
  • 登录GitHub -> Settings -> SSH and GPG keys -> New SSH key
  • 粘贴公钥内容,命名如 “im██-work-key”
  1. 在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文件)

  1. 检查当前密钥权限
1
2
3
ssh -T git@github.com
# 应当显示:Hi im██! You've successfully authenticated...
# 如果显示 █e█a█n█ 则说明错误密钥被使用
  1. 使用 SSH 协议代替 HTTPS
1
2
3
4
5
6
7
8
# 1.移除现有的 origin
git remote rm origin

# 2.添加 SSH 地址的 origin,@后面的github.com为.ssh中config文件里的Host行设置的别名,需自行替换
git remote add origin git@github.com:im██/im██.github.io.git

# 3.查看地址
git remote -v

既然 SSH 连接正常,直接改用 SSH 协议进行操作。

  1. 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. 令牌创建步骤
  • Token name (令牌名称)
1
输入一个有意义的名称(如:"im██-image-repo")
  • Expiration (过期时间)
1
选择 "无到期" 或自定义日期(建议选择较长时间)
  • Resource owner (资源所有者)
1
选择您的账号 "im██"
  • 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. 令牌创建成功后
1
2
3
4
5
6
7
8
9
# 1.创建个人访问令牌 (Personal Access Token)
# 登录 GitHub → Settings → Developer settings → Personal access tokens
# 勾选 repo 权限 → 生成并复制令牌(如 ghp_xxxxxxxx)

# 2.修改远程地址,@后面的github.com为.ssh中config文件里的Host行设置的别名,需自行替换
git remote set-url origin https://im██:ghp_xxxxxxxx@github.com/im██/im██.github.io.git

# 3.简化推送(跳过 SSH)
git push -f origin master

四、诊断工具

  1. 验证端口连接
1
2
3
4
5
6
7
8
# 测试端口 443 是否开放
tnc ssh.github.com -Port 443

# 测试代理可用性
curl -v https://github.com --proxy http://127.0.0.1:23457

# 检查实际使用的端口
git push -u origin master -v
  1. 修复 SSL 证书路径
1
2
3
4
5
6
# 1. 设置 Git 使用 Windows 自带的证书库
git config --global http.sslBackend schannel

# 2. 验证配置
git config --global --get http.sslBackend
# 应该输出: schannel


捐赠一元,支持一下!  

注:捐赠时如在留言中注明网名或昵称,即可被列入到感谢名单中。否则,会以佚名身份列入名单。


重置git仓库以精简Github和Vercel空间大小
https://rsreland.net/blog/202510152329/
作者
百二一
发布于
2025年10月15日
许可协议