如何拆分git存储库并关注目录重命名? [英] How to split a git repository and follow directory renames?

查看:129
本文介绍了如何拆分git存储库并关注目录重命名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个很大的git仓库,它包含许多项目,每个项目都在自己的子目录中。我需要将它分成单独的存储库,每个项目都有自己的存储库。



我试过 git filter-branch --prune-empty --subdirectory-filter PROJECT master



然而,许多项目目录在他们的生活中经历了多次重命名,并且 git filter-branch 没有遵循重命名,所以有效地提取的回购在上次重命名之前没有任何历史记录。



如何从一个大的git仓库中有效地提取一个子目录,并将所有该目录的重命名回溯到过去?

感谢@Chronial,我可以根据自己的需要编写一个脚本来按摩我的git回购:

<$
> git filter-branch --prune-empty --index-filter'
#删除不需要的文件
git ls-files -z | egrep -zv^(NAME1 | NAME2 | NAME3)|
xargs -0 -r git rm --cached -q
#将文件移至根目录
git ls-files -s | sed -es-\ t \(NAME1 \ | NAME2 \ | NAME3 \)/ - \t-|
GIT_INDEX_FILE = $ GIT_INDEX_FILE.new \
git update-index --index-info&&
(test!-f$ GIT_INDEX_FILE.new\
|| mv -f$ GIT_INDEX_FILE.new$ GIT_INDEX_FILE)
'


$ b基本上这是这样的:


  1. 删除我需要的三个目录NAME1,NAME2或NAME3以外的所有文件 (其中一个项目在其生命周期中被重命名为NAME1 - > NAME2 - > NAME3)。


  2. 将这三个目录中的所有内容移动到存储库的根目录。

  3. 需要测试是否存在$ GIT_INDEX_FILE.new,因为将svn导入git会创建不带任何文件的提交(仅限目录提交)。只有在最初使用'git svn clone'创建回购时才需要。


I currently have a big git repository that contains many projects, each one in its own subdirectory. I need to split it into individual repositories, each project in its own repo.

I tried git filter-branch --prune-empty --subdirectory-filter PROJECT master

However, many project directories went through several renames in their lives, and git filter-branch does not follow renames, so effectively the extracted repo does not have any history prior to the last rename.

How can I effectively extract a subdirectory from one big git repo, and follow all that directory's renames back into the past?

解决方案

Thanks to @Chronial, I was able to cook a script to massage my git repo according to my needs:

git filter-branch --prune-empty --index-filter '
    # Delete files which are NOT needed
    git ls-files -z | egrep -zv  "^(NAME1|NAME2|NAME3)" | 
        xargs -0 -r git rm --cached -q             
    # Move files to root directory
    git ls-files -s | sed -e "s-\t\(NAME1\|NAME2\|NAME3\)/-\t-" |
        GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
        git update-index --index-info &&
        ( test ! -f "$GIT_INDEX_FILE.new" \
            || mv -f "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" )
'

Basically what this does is this:

  1. Deletes all files outside of the three directories NAME1, NAME2 or NAME3 that I need (one project was renamed NAME1 -> NAME2 -> NAME3 during its lifetime).

  2. Moves everything inside these three directories to the root of the repository.

  3. I needed to test if "$GIT_INDEX_FILE.new" exists since import of svn into git creates commits without any files (directory-only commits). Needed only if the repo was created with 'git svn clone' initially.

这篇关于如何拆分git存储库并关注目录重命名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆