【问题标题】:How extract 2 files as a git submodule?如何将 2 个文件提取为 git 子模块?
【发布时间】:2018-03-13 11:36:25
【问题描述】:

我有两个文件想发送它们自己的新存储库,但我也想保留它们自己的历史记录,因此新存储库已经有一些历史记录。

我尝试搜索并找到了这些线程:

  1. Splitting a set of files within a git repo into their own repository, preserving relevant history
  2. How to split a git repository while preserving subdirectories?
  3. Create a submodule repository from a folder and keep its git commit history

但我无法确切地弄清楚如何编写命令。我尝试根据上述答案运行它:

git clone repo
git remote rm origin
cd repo
git filter-branch --tree-filter 'git rm --cached --ignore-unmatch "filename1" "filename2"' -- --all

我希望清理当前存储库并仅保留这两个文件及其历史记录,然后我可以将其作为新的子模块推送,但在运行命令后,我的所有文件都保留在存储库中。

【问题讨论】:

  • git subtree 是(不仅是专用的)执行此操作的方法。 我会在今天晚些时候尝试回答。
  • @mab:你的尝试怎么样? ;-)

标签: git split git-submodules git-filter-branch revision-history


【解决方案1】:

我设法从历史记录中删除了所有文件,除了我需要的两个文件。我必须查看整个历史,找出所有重命名的文件的名称,并详细说明一个大文件和文件夹名称列表。然后我运行这些命令:

git clone repo
git remote rm origin
cd repo
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch "folder1" "file1" "etc" -r -f' \
--prune-empty --tag-name-filter cat -- --all
git remote add origin new_bare_empty_remote
git push -u origin master

或者,使用它来删除所有不属于"whitelist"的文件


git filter-branch -f --prune-empty --index-filter 'git ls-files -z | grep -zv "the.whitelist" | xargs -0 git rm --cached --ignore-unmatch'

https://gist.github.com/ssp/1663093 如何从 git 存储库中提取单个文件及其历史记录

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 2011-05-09
    • 2014-09-26
    • 2023-03-03
    • 2015-09-13
    • 2023-03-29
    • 2017-04-11
    • 2017-02-23
    • 2017-07-23
    相关资源
    最近更新 更多