从文件夹创建子模块存储库并保留其git提交历史记录 [英] Create a submodule repository from a folder and keep its git commit history

查看:83
本文介绍了从文件夹创建子模块存储库并保留其git提交历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,以特定的方式探索其他Web应用程序。它在 demos 文件夹中包含一些网页演示,其中一个演示现在应该有它自己的存储库。我想为此演示应用程序创建一个单独的存储库,并将其作为子包 子模块,而不会丢失其提交历史记录。



是否可以保留文件中的提交历史记录存储库的文件夹并从中创建一个存储库,并将其用作子模块 b

请参阅本答案末尾的注释(最后一段),以便使用npm快速替代git子模块;)

在下面的答案中,您将知道如何从存储库中提取文件夹,并从中创建一个git存储库,然后将其作为 submodule i而不是文件夹。



灵感来自Gerg Bayer的文章将文件从一个Git库移到另一个,保留历史记录 ,我们有这样的东西:

 < git repository A> 
someFolders
someFiles
someLib< - 我们希望这是一个新的回购和一个git子模块!
一些文件

在下面的步骤中,我将引用 someLib <目录1>



最后,我们会像这样:

 < git repository A> 
someFolders
someFiles
@submodule - > < git repository B>

< git repository B>
someFolders
someFiles



从另一个文件夹创建一个新的git存储库存储库



第1步



获取要分割的存储库的新副本。

  git clone< git repository A url> 
cd< git repository目录>



第2步



当前文件夹将会作为新的存储库,以便删除当前的远程服务器。

  git remote rm origin 



第3步



提取所需文件夹的历史记录并提交它

  git filter-branch --subdirectory-filter< directory 1> -  --all 

您现在应该拥有一个git仓库,其中包含目录1 ,并在所有相关的提交历史记录的根目录中。



第4步



创建您的在线资源库并推送您的新资源库!

  git remote add origin< git repository B url> 
git push

您可能需要设置上游分支为你的第一次推送

  git push --set-upstream origin master 



清理< git repository A> (可选,请参阅注释) h2>

我们希望从< git repository B> 的跟踪(文件和提交历史记录) >< git repository A> 所以这个文件夹的历史记录只有一次。


$ b 这是基于从github上删除敏感数据

转到新文件夹和

  git clone< git repository A url> 
cd< git repository目录>
git filter-branch --force --index-filter'git rm --cached --ignore-unmatch< directory 1> -r'--prune-empty --tag-name-filter cat - --all

用您想删除的文件夹替换< directory 1> -r 会在指定目录内递归地执行:)。现在用 - force



<$>推到 origin / master p $ p> git push origin master --force



Boss Stage注意下面)



创建子模块< git repository B> < git repository A>

  git submodule add< git repository B url> 
git子模块更新
git commit

验证一切是否按预期工作, code> push

  git push origin master 



注意



完成所有这些后,我意识到我的情况是更适合使用 npm 来管理我自己的依赖关系。我们可以指定git网址和版本,请参阅包.json git urls as dependencies



如果您这样做,您想要用作需求的存储库必须是 npm模块,所以它必须包含一个 package.json 文件,否则你会得到这个错误:错误:ENOENT,打开'tmp.tgz- unpack / package.json'



tldr(替代解决方案)



您可能会发现使用 npm 使用git url管理依赖关系


  • 将文件夹移至新存储库

  • 在两个存储库内运行 npm init 运行 npm install --save git://github.com/user/project.git#commitish 你在哪里安装您的依赖关系

    I have a web application that explores other web applications in a particular way. It contains some web demos in a demos folder and one of the demo should now have it's own repository. I would like to create a separate repository for this demo application and make it a subpackage submodule from main repository without loosing its commit history.

    Is it possible to keep the commit history from the files in a repository's folder and create a repository from it and use it as a submodule instead?

    解决方案

    Detailed Solution

    See the note at the end of this answer (last paragraph) for a quick alternative to git submodules using npm ;)

    In the following answer, you will know how to extract a folder from a repository and make a git repository from it and then including it as a submodule instead of a folder.

    Inspired from Gerg Bayer's article Moving Files from one Git Repository to Another, Preserving History

    At the beginning, we have something like this:

    <git repository A>
        someFolders
        someFiles
        someLib <-- we want this to be a new repo and a git submodule!
            some files
    

    In the steps bellow, I will refer this someLib as <directory 1>.

    At the end, we will have something like this:

    <git repository A>
        someFolders
        someFiles
        @submodule --> <git repository B>
    
    <git repository B>
        someFolders
        someFiles
    

    Create a new git repository from a folder in an other repository

    Step 1

    Get a fresh copy of the repository to split.

    git clone <git repository A url>
    cd <git repository A directory>
    

    Step 2

    The current folder will be the new repository so remove the current remote.

    git remote rm origin
    

    Step 3

    Extract history of the desired folder and commit it

    git filter-branch --subdirectory-filter <directory 1> -- --all
    

    You should now have a git repository with the files from directory 1 in your repo's root with all related commit history.

    Step 4

    Create your online repository and push your new repository!

    git remote add origin <git repository B url>
    git push
    

    You may need to set the upstream branch for your first push

    git push --set-upstream origin master
    

    Clean <git repository A> (optional, see comments)

    We want to delete traces (files and commit history) of <git repository B> from <git repository A> so history for this folder is only there once.

    This is based on Removing sensitive data from github.

    Go to a new folder and

    git clone <git repository A url>
    cd <git repository A directory>
    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <directory 1> -r' --prune-empty --tag-name-filter cat -- --all
    

    Replace <directory 1> by the folder you want to remove. -r will do it recursively inside the specified directory :). Now push to origin/master with --force

    git push origin master --force
    

    Boss Stage (See Note below)

    Create a submodule from <git repository B> into <git repository A>

    git submodule add <git repository B url>
    git submodule update
    git commit
    

    Verify if everything worked as expected and push

    git push origin master
    

    Note

    After doing all of this, I realized in my case that it was more appropriate to use npm to manage my own dependencies instead. We can specify git urls and versions, see the package.json git urls as dependencies.

    If you do it this way, the repository you want to use as a requirement must be an npm module so it must contain a package.json file or you'll get this error: Error: ENOENT, open 'tmp.tgz-unpack/package.json'.

    tldr (alternative solution)

    You may find it easier to use npm and manage dependencies with git urls:

    • Move folder to a new repository
    • run npm init inside both repositories
    • run npm install --save git://github.com/user/project.git#commit-ish where you want your dependencies installed

    这篇关于从文件夹创建子模块存储库并保留其git提交历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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