已解决
git仓删除当前仓且保留嵌套子仓--类似保留特定文件目录
来自网友在路上 163863提问 提问时间:2023-10-22 00:01:32阅读次数: 63
最佳答案 问答题库638位专家为你答疑解惑
当前git若有损坏需删除重新下载,但其还含有子仓,不能直接删除整体目录。清理方法如下:
分如下两种场景
1、若是子仓当前没有进行任何操作,即子仓可以临时移动
这种比较简单,分如下几步:
step1:找到全部子仓路径(第2中情况中find_all_sub_git_path即为查找全部git仓路径的方法);
step2: 创建备份目录;
step3:依次将子仓保留目录层move到备份目录;
step4: 删除当前路径;
step5:将备份目录move当前目录。
2、若是子仓当前正在进行操作,如挑单等,即子仓不可移动
这种需正向找到全部需要删除的目录和文件,依次删除。代码实现如下
import os
import shutil
from pathlib import Pathdef remove_git_dir_and_retain_sub_git(dir_path):shutil.rmtree(os.path.join(dir_path, ".git")) # 需先删除,不然影响递归remove_list = []get_all_remove_list(dir_path, remove_list)for sub_remove in remove_list:if os.path.isdir(sub_remove):shutil.rmtree(sub_remove)else:os.remove(sub_remove)def get_all_remove_list(dir_path, remove_list):# 子仓跳过if os.path.exists(os.path.join(dir_path, ".git")): return# 不包含.git仓的目录可以直接删除if not find_all_sub_git_path(dir_path):remove_list.append(dir_path)return# 其它包含.git仓的目录需进行递归sub_path_list = os.listdir(dir_path)for sub_path in sub_path_list:sub_full_path = os.path.join(dir_path, sub_path)if os.path.isdir(sub_full_path):get_all_remove_list(sub_full_path, remove_list)else:remove_list.append(sub_full_path)def find_all_sub_git_path(dir_path):keywords = '**/.git/config'p = Path(base_dir)files = p.glob(keywords)file_list = list(files)file_list = [os.path.dirname(os.path.dirname(os.fspath(file_path))) for file_path in file_list]return file_list
查看全文
99%的人还看了
相似问题
- 重磅!1区、60年老牌期刊被踢?共5本被剔除!11月SCIE/SSCI期刊目录更新!
- vsto word 获取目录起始页和结束页,如目录起始位置为2、结束位置为3,返回2和3
- Linux文件目录以及文件类型
- 基于pytest-bdd的项目目录结构和命名规范
- 事关Django的静态资源目录设置(Django的setting.py中的三句静态资源(static)目录设置语句分别是什么作用?)
- iOS源码-工程目录讲解
- 报错资源不足,k8s使用containerd运行容器修改挂载点根目录换成/home
- 十三、Linux文件目录指令
- Linux(4):Linux文件与目录管理
- Linux(3):Linux 的文件权限与目录配置
猜你感兴趣
版权申明
本文"git仓删除当前仓且保留嵌套子仓--类似保留特定文件目录":http://eshow365.cn/6-21127-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: Rust 语法笔记
- 下一篇: 智能电表上的模块发热正常吗?