简体   繁体   English

根目录中的新git存储库,用于包含子目录中的现有存储库

[英]New git repository in root directory to subsume an exist repository in a sub-directory

I am working on a large code base that is not currently under any revision control (yes, really). 我正在开发一个目前没有任何版本控制的大型代码库(是的,真的)。 I have been working on one component in a deep directory and created a covert git repository to track my changes. 我一直在深度目录中的一个组件上工作,并创建了一个隐蔽的git存储库来跟踪我的更改。 I now want to add all the code to source control and make a repository for the whole directory tree. 我现在想要将所有代码添加到源代码控制中,并为整个目录树创建一个存储库。 I want the new, outer repository to include all the history for the files in the inner repository. 我希望新的外部存储库包含内部存储库中文件的所有历史记录。 I do not want separate projects or submodules; 我不想要单独的项目或子模块; I want to be as if the repository had been created in the root of the code tree from the start and the inner repository never existed. 我希望好像从一开始就在代码树的根目录中创建了存储库,而内部存储库从未存在过。 I want to delete the .git directory in the sub-directory but would prefer not to lose the revision history that it contains. 我想删除子目录中的.git目录,但不希望丢失它包含的修订历史记录。

Everything I have read on the topic is about merging existing repositories and that is not the case here or about maintaining submodules or remote projects, which I don't want. 我在这个主题上阅读的所有内容都是关于合并现有的存储库,这不是这里的情况,也不是关于维护子模块或远程项目,这是我不想要的。

Thank you. 谢谢。

Let's say your component is in /project-root/deep/path/to/component . 假设您的组件位于/project-root/deep/path/to/component The following commands will do what you want: 以下命令将执行您想要的操作:

cd /project-root/deep/path/to/component
git filter-branch --tree-filter 'mkdir -p deep/path/to/component; git mv file1 file2 file2 deep/path/to/component' HEAD
rm -rf deep
mv .git /project-root
git reset --hard

Then of course you have to add the files for the rest of the project just as normal. 当然,您必须像平常一样为项目的其余部分添加文件。

You could use filter-branch and move existing files in the current history into the subdirectory where they would be had the repository existed at project root from the beginning on: 您可以使用filter-branch并将当前历史记录中的现有文件移动到子目录中,在该子目录中,从一开始就存在于项目根目录中的存储库:

git filter-branch \
--tree-filter '
>/dev/null mkdir -p path/from/project/root/to/current/;
mv * path/from/project/root/to/current/;
# take care of special files, e.g. .gitignore and other hidden files
mv .gitignore path/from/project/root/to/current/;
'
--tag-filter cat \
--all

After this is finished, verify the resulting history. 完成此操作后,验证生成的历史记录。 If everything is dandy, move the .git directory into your project root; 如果一切都很花哨,请将.git目录移动到项目根目录中; you should not see any unstaged files. 你不应该看到任何未分级的文件。

I'm pretty sure there is a computationally cheaper way, using --index-filter and git's plumbing commands, but I don't know them by heart. 我很确定使用--index-filter和git的管道命令有一种计算上更便宜的方式,但我不清楚它们。

This will rewrite history, so don't use this method if your repository was shared between several developers. 这将重写历史记录,因此如果您的存储库是由多个开发人员共享的,请不要使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM