本文介绍了将git仓库中的一组文件拆分到他们自己的仓库中,保留相关的历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一次,我将自己的代码添加到了现有的git仓库中,并承诺付出了相当大的代价,而其他开发人员已经承诺回购仓库中的其他现有文件。 现在我想将我的代码拆分到自己的回购站中,但保留了我的特定文件的所有更改历史记录。 已经完成了分解代码的工作我正在查看 filter-branch 并执行 - index-filter - tree-filter rm 命令用于我不关心的文件。我不想使用 - subdirectory-filter ,因为它不适合保持我的代码为topdir的subdir(也是我们共享的一个subdir)。更为复杂的是,原始存储库中的一些文件随着时间的推移而移动了一些,并且有一些文件被创建然后被删除。这使得设计一个rm列表有点难...

。是否有人知道如何做到这一点?

通过使用 index-filter 树结构过滤器 $ b

然后应用反向逻辑,如 git ls-tree 传入(多个) grep -v 的管道 xargs 对于 git rm 确实可以删除所有与一组文件名/目录不匹配的文件。这是我用来分割我的特定文件的命令:

  git filter-branch \ 
--prune -empty \
--index-filter'
git ls-tree -z -r --name-only --full-tree $ GIT_COMMIT \
| grep -z -v^ src / pyfedpkg $\
| grep -z -v^ src / fedpkg\
| grep -z -v^ git-changelog\
| xargs -0 -r git rm --cached -r
'\
- \
--all


At one point I added my code to an existing git repo, and have committed to it quite a lot since, while the other developer has committed to the other existing files in the repo. Now I want to split my code off into its own repo, but preserve all the change history for my particular files.

Reading through what others have done for splitting code out I was looking at filter-branch and doing --index-filter or --tree-filter with rm commands for the files I don't care about. I don't want to use --subdirectory-filter as it is not appropriate for the subdir holding my code to be the topdir (also we shared one subdir). To complicate matters, some of the files from the original repository have moved around a bit over time, and there are some files that were created then deleted. This makes designing an rm list a bit... challenging.

I'm looking for a way to filter everything /except/ a list of files/directories. Does anybody know of a way to do this?

解决方案

Just to close the loop on this so it appears as answered.

By using index-filter or tree-filter and then applying reverse logic like git ls-tree piped into (multiple) grep -v's piped into xargs for git rm you can indeed remove everything that doesn't match a narrow set of file names/directories. Here is the command I used to split out my particular files:

git filter-branch \
    --prune-empty \
    --index-filter '
        git ls-tree -z -r --name-only --full-tree $GIT_COMMIT \
        | grep -z -v "^src/pyfedpkg$" \
        | grep -z -v "^src/fedpkg" \
        | grep -z -v "^git-changelog" \
        | xargs -0 -r git rm --cached -r
    ' \
    -- \
    --all

这篇关于将git仓库中的一组文件拆分到他们自己的仓库中,保留相关的历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 00:00