用git hooks解决github大文件报错,100M限制或50M限制|大文件|50M|git hooks|git

写了一个git hooks 文件,在commit大于设定size的文件的时候,拦截commit,并且把大文件名写入.gitignore ,同时从缓存区中移除大文件

进行版本控制的时候,经常会由于大文件导致上传github出问题,并且版本回退也比较麻烦。
实际上我们很少代码文件会超过50M,而往往是由于数据文件过大导致错误,这些数据文件往往我们可能都不需要进行版本控制

于是,不如直接通过脚本,默认不对这些大文件进行版本控制

代码 (文件名设置为pre-commit,防在.git/hooks目录下,注意文件名要一致,这涉及到git hooks的逻辑,不做过多解释)

#!/bin/python
import subprocess
import os


if __name__ == "__main__":
    files = subprocess.check_output(['git', 'diff', '--cached', '--name-status'], text=True, encoding='utf-8').split('\n')
    for file in files:
        if len(file.split("\t")[0]) > 0 and file.split("\t")[0] != 'D':
            file_path = file.split("\t")[-1]
            file_size = os.path.getsize(file_path)
            size_in_mb = file_size / (1024 * 1024)
            if size_in_mb >= 50:
                with open('.gitignore', 'a', encoding='utf-8') as f:
                    f.write(file_path + '\n')
                try:
                    # Remove the file from the Git index (staged area)
                    subprocess.check_output(['git', 'rm', '--cached', file_path])
                    print("Removed {} from stage".format(file_path))
                except subprocess.CalledProcessError as e:
                    print("Error removing file from stage: {}".format(e))
                
                try:
                    # Reset the file to unstage changes
                    subprocess.check_output(['git', 'reset', file_path])
                except subprocess.CalledProcessError as e:
                    print("Error while resetting file: {}".format(e))

                print("The file is larger than 50MB and has been added to .gitignore. Please confirm and recommit!")
                print(file_path + "\n")
                exit(1)

历史代码,废弃:

#!/bin/python
import subprocess
import os
if name == “main”:
files = subprocess.check_output([‘git’,‘diff’ ,‘–cached’ ,‘–name-status’], text=True,encoding = ‘utf-8’).split(‘\n’)
for file in files:
#print(file)
#print(file.split(“\t”))
if len(file.split(“\t”)[0])>0 and file.split(“\t”)[0] != ‘D’:
file_size = os.path.getsize(file.split(“\t”)[-1])
size_in_mb = file_size/(1024*1024)
if size_in_mb >=50:
with open(‘.gitignore’,‘a’,encoding=‘utf-8’) as f:
f.write(file+‘\n’)
try:
subprocess.check_output([‘git’,‘reset’,file])
print(“remove {} from stage”.format(file))
except Exception as e:
print(“fital”)
print(“The file is larger than 50M and has been put into.gitignore, please confirm!!! And recommit!!!”)
print(file+“\n”)
exit(1)

文件路径

演示

以一个大文件 a.mp4为例

a.mp4演示### 将a.mp4放入缓存区git add

commit,就会报出问题

在这里插入图片描述

这个时候git status,就可以看见.gitignore被更改了,a.mp4被从缓存区移除了

在这里插入图片描述

如果你的项目中有中文路径,可以使用git config --global core.quotepath false使git正常输出中文项目

git config --global core.quotepath false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值