一次 服务器Git gc 报错引发的 Swap 添加之旅
🧩 起因:一次简单的 git fetch + merge
在日常使用 Git 时,我尝试将远程分支合并到本地仓库中:
git fetch origin main:temp
git merge temp
操作看起来一切正常,但 Git 在合并结束后自动执行的后台 git gc
操作时,却报出了下面这个异常:
Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
warning: The last gc run reported the following. Please correct the root cause and remove .git/gc.log
Automatic cleanup will not be performed until the file is removed.
error: pack-objects died of signal 9
fatal: failed to run repack
🔍 排查过程
Step 1:查看 .git/gc.log
报错提示让我查看 .git/gc.log
文件:
cat .git/gc.log
输出很明确:
error: pack-objects died of signal 9
fatal: failed to run repack
signal 9
是 Linux 中的 SIGKILL
,通常意味着进程被系统强制终止,多数情况下是因为 内存不足(OOM)。
Step 2:查看内存状况
我使用 free -h
检查了当前内存:
free -h
输出如下:
Mem: 1.7Gi 803Mi 683Mi 60Mi 234Mi 715Mi
Swap: 0B 0B 0B
可以看到,系统没有配置 Swap,而物理内存也只有 1.7GB,在 Git 打包过程中很容易耗尽。
💡 解决方案:添加 Swap
为了避免 Git 被系统杀掉,我决定手动添加一个 1GB 的 Swap 文件。
🚀 添加步骤如下:
# 创建一个 1GB 的 swap 文件
fallocate -l 1G /swapfile
# 设置权限(很重要)
chmod 600 /swapfile
# 格式化为 swap 格式
mkswap /swapfile
# 启用 swap
swapon /swapfile
# 再次确认
free -h
确认输出:
Swap: 1.0Gi 0B 1.0Gi
Swap 成功启用!
✅ 再次尝试 git gc
启用 Swap 后,我再次运行:
git gc --prune=now
这次没有任何错误,顺利完成:
Enumerating objects: 6720, done.
Counting objects: 100% (6720/6720), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6452/6452), done.
Writing objects: 100% (6720/6720), done.
问题圆满解决!
🔁 可选:让 Swap 重启后自动挂载
如果想让这个 swap 永久生效,可以添加到 /etc/fstab
:
echo '/swapfile none swap sw 0 0' >> /etc/fstab
✍️ 总结
问题 | 原因 | 解决方法 |
---|---|---|
git gc 报 pack-objects died of signal 9 |
系统内存不足,无 swap,被 OOM 杀掉 | 添加 swap 文件,释放内存压力 |
虽然只是一个 Git 操作,却意外学会了 Swap 的添加方法,也算是收获了 Linux 运维知识的一小步。
本文作者: 永生
本文链接: https://yys.zone/detail/?id=407
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
发表评论
评论列表 (0 条评论)
暂无评论,快来抢沙发吧!