写了一个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放入缓存区
commit,就会报出问题
这个时候git status,就可以看见.gitignore被更改了,a.mp4被从缓存区移除了
如果你的项目中有中文路径,可以使用git config --global core.quotepath false使git正常输出中文项目
git config --global core.quotepath false