git仓库拆分工具git-filter-repo

需求:近期我们发现有两个没有太多开发交集的项目放在了一个代码仓库里,遂对其中的一个仓库进行了拆分,将目录放到了一系列子仓中。

本来准备使用git filter-branch命令,但是在运行时,它提示这个命令会慢,建议使用替代工具git-filter-repo.

一番搜索后,找到了工具及使用方法,非常好用,分享一下。

git-filter-repo官方github地址:

https://github.com/newren/git-filter-repo/

manual page:GitHub & BitBucket HTML Preview

参考文档:

这篇很全:https://josh.fail/2022/merging-git-repos-with-git-filter-repo/

抛砖系列之git仓库拆分工具git-filter-repo_git 拆分仓库_qq_43479892的博客-CSDN博客

将子文件夹拆分成新仓库 - GitHub Docs

 这里是filter-branch转换为filter-repo命令的表格:https://github.com/newren/git-filter-repo/blob/main/Documentation/converting-from-filter-branch.md

一、git-filter-repo安装

git-filter-repo 运行环境要求:

  • git >= 2.22.0 at a minimum; some features require git >= 2.24.0 or later
  • python3 >= 3.5

安装git-filter-repo:

git-filter-repo就是一个python脚本(将文件下载下来:https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo),并该文件放到你的系统路径下即可,我选择的路径是:/usr/local/bin/git-filter-repo

二、git-filter-repo使用

原仓库tree:

|-- moduleA
|   |-- subdir1
|   |-- subdir2
|   |-- subdir3
|   |-- subdir4

主要使用了git-filter-repo的两个命令。

1) git filter-repo --subdirectory-filter  moduleA/subdir1   # 将子目录moduleA/subdir1下的内容筛选到根路径下

2) git filter-repo --path moduleA/subdir2 --path  moduleA/subdir2       # 将子目录筛选出来,但路径保持不变

以上两个命令可以组合使用就可以达到你想要的分仓效果。

三、分仓后提交

在运行完git filter-repo命令后,使用如下命令进行推送

git push --set-upstream "ssh://新仓库git地址" master --force

四、示例

如我想把原仓库中的moduleA/subdir1下的内容放到一个新仓库中,则在当前仓库根下运行:

git filter-repo --subdirectory-filter moduleA/subdir1
git push --set-upstream "ssh://新git仓库地址" master --force

如我想把原仓库中的moduleA下的subdir2,subdir3放到一个新仓库中,则可以运行如下命令:

git filter-repo --subdirectory-filter moduleA
git filter-repo --path subdir2 --path subdir3
git push --set-upstream "ssh://新git仓库地址" master --force

将根目录移到某个子目录subdir中:

git filter-repo --path-rename :subdir/

总结

这个git-filter-repo工具还是很好用的,可以运行git-filter-repo --help 查看更多命令。

这个是帮助里给出的示例:

EXAMPLES

    To get a bunch of reports mentioning renames that have occurred in
    your repo and listing sizes of objects aggregated by any of path,
    directory, extension, or blob-id:
      git filter-repo --analyze

    (These reports can help you choose how to filter your repo; it can
    be useful to re-run this command after filtering to regenerate the
    report and verify the changes look correct.)

    To extract the history that touched just 'guides' and 'tools/releases':
      git filter-repo --path guides/ --path tools/releases

    To remove foo.zip and bar/baz/zips from every revision in history:
      git filter-repo --path foo.zip --path bar/baz/zips/ --invert-paths

    To replace the text 'password' with 'p455w0rd':
      git filter-repo --replace-text <(echo "password==>p455w0rd")

    To use the current version of the .mailmap file to update authors,
    committers, and taggers throughout history and make it permanent:
      git filter-repo --use-mailmap

    To extract the history of 'src/', rename all files to have a new leading
    directory 'my-module' (e.g. src/foo.java -> my-module/src/foo.java), and
    add a 'my-module-' prefix to all tags:
      git filter-repo --path src/ --to-subdirectory-filter my-module --tag-rename '':'my-module-'

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
download-git-repo是一个用于从GitHubGitLab等代码托管平台上下载代码仓库工具。如果在使用download-git-repo时遇到报错,可能有以下几种原因和解决方法。 1. 网络连接问题:下载代码仓库需要通过网络连接到代码托管平台,如果网络连接异常或不稳定,可能会导致下载失败。这时可以尝试检查网络连接是否正常,并重试下载操作。 2. 权限问题:有些代码仓库可能设置了访问权限,如果没有相应的权限,下载操作就会失败。在使用download-git-repo时,需要确保你有访问该代码仓库的权限,否则无法下载。可以尝试检查你的访问权限,并确认是否有权下载该代码仓库。 3. 参数配置错误:在使用download-git-repo时,需要提供正确的参数配置。如果参数配置错误,也会导致下载失败。可以检查一下传递给download-git-repo的参数是否正确,例如代码仓库的地址、分支、保存路径等。 4. download-git-repo工具自身问题:有时候,download-git-repo工具本身可能存在一些问题,例如版本不兼容或者有bug。可以尝试更新或升级download-git-repo工具,或者尝试使用其他下载工具来下载代码仓库。 总之,download-git-repo报错可能原因多种多样,需要根据具体错误提示和情况进行排查和解决。可以通过检查网络连接、权限、参数配置等方面来确定问题所在,并尝试相应的解决方法来解决报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值