从文件夹创建子模块存储库并保留其git提交历史记录 [英] Create a submodule repository from a folder and keep its git commit history
问题描述
我有一个Web应用程序,以特定的方式探索其他Web应用程序。它在 demos
文件夹中包含一些网页演示,其中一个演示现在应该有它自己的存储库。我想为此演示应用程序创建一个单独的存储库,并将其作为子包 子模块,而不会丢失其提交历史记录。
是否可以保留文件中的提交历史记录存储库的文件夹并从中创建一个存储库,并将其用作子模块 b 请参阅本答案末尾的注释(最后一段),以便使用npm快速替代git子模块;) 在下面的答案中,您将知道如何从存储库中提取文件夹,并从中创建一个git存储库,然后将其作为 submodule i而不是文件夹。 灵感来自Gerg Bayer的文章将文件从一个Git库移到另一个,保留历史记录 ,我们有这样的东西: 在下面的步骤中,我将引用 最后,我们会像这样: 获取要分割的存储库的新副本。 当前文件夹将会作为新的存储库,以便删除当前的远程服务器。 提取所需文件夹的历史记录并提交它 您现在应该拥有一个git仓库,其中包含 创建您的在线资源库并推送您的新资源库! 您可能需要设置 我们希望从 转到新文件夹和 用您想删除的文件夹替换 创建子模块从 验证一切是否按预期工作, code> push 完成所有这些后,我意识到我的情况是更适合使用 npm 来管理我自己的依赖关系。我们可以指定git网址和版本,请参阅包.json git urls as dependencies 。 如果您这样做,您想要用作需求的存储库必须是 npm模块,所以它必须包含一个 您可能会发现使用 npm 和使用git url管理依赖关系: I have a web application that explores other web applications in a particular way. It contains some web demos in a 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? 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: In the steps bellow, I will refer this At the end, we will have something like this:
Get a fresh copy of the repository to split.
The current folder will be the new repository so remove the current remote.
Extract history of the desired folder and commit it You should now have a git repository with the files from Create your online repository and push your new repository! You may need to set the
We want to delete traces (files and commit history) of This is based on Removing sensitive data from github. Go to a new folder and Replace
Create a submodule from Verify if everything worked as expected and
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 You may find it easier to use npm and manage dependencies with git urls: 这篇关于从文件夹创建子模块存储库并保留其git提交历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< 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
目录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
git push origin master
注意
package.json
文件,否则你会得到这个错误:错误:ENOENT,打开'tmp.tgz- unpack / package.json'
。
tldr(替代解决方案)
npm init
运行 npm install --save git://github.com/user/project.git#commitish
你在哪里安装您的依赖关系
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.Detailed Solution
<git repository A>
someFolders
someFiles
someLib <-- we want this to be a new repo and a git submodule!
some files
someLib
as <directory 1>
.<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
git clone <git repository A url>
cd <git repository A directory>
Step 2
git remote rm origin
Step 3
git filter-branch --subdirectory-filter <directory 1> -- --all
directory 1
in your repo's root with all related commit history. Step 4
git remote add origin <git repository B url>
git push
upstream
branch for your first pushgit push --set-upstream origin master
Clean
<git repository A>
(optional, see comments)<git repository B>
from <git repository A>
so history for this folder is only there once.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
<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)
<git repository B>
into <git repository A>
git submodule add <git repository B url>
git submodule update
git commit
push
git push origin master
Note
package.json
file or you'll get this error: Error: ENOENT, open 'tmp.tgz-unpack/package.json'
.tldr (alternative solution)
npm init
inside both repositories npm install --save git://github.com/user/project.git#commit-ish
where you want your dependencies installed