我可以将 repo 的 .git 目录移动到它的父目录吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/614229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:36:27  来源:igfitidea点击:

Can I move the .git directory for a repo to it's parent directory?

gitmergedirectorysubdirectory

提问by user74159

I have two sub-directories each with a repo, thus :

我有两个子目录,每个子目录都有一个 repo,因此:

PPP/
 |--ABC/
 |   |--.git/
 |   |--AAA/
 |   |    BBB/
 |   |   CCC/
 |   
 |--DEF/
 |   |--.git/
 |   |--DDD/
 |   |--EEE/

And would like to combine them into one repo, so, I would assume the directory structure would be like this:

并且想将它们组合成一个 repo,所以,我假设目录结构是这样的:

PPP/
 |--.git/
 |--ABC/
 |   |--AAA/
 |   |--BBB/
 |   |--CCC/
 |   
 |--DEF/
 |   |--DDD/
 |   |--EEE/

Is this posible?

这可能吗?

Also currently several people have the repos on their machines. How much more complicated does that make life?

目前也有几个人在他们的机器上有回购。这让生活变得复杂多少?

Ta.

塔。

回答by MiniQuark

You can do what you are describing like this:

您可以像这样执行您所描述的操作:

  1. Move the content of ABCto an ABC/subdirectory, and fix the history so that it looks like it has always been there:

    $ cd /path/to/ABC
    $ git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t-&ABC/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
         git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD
    

    Now your directory structure is ABC/ABC/your_code

  2. Same for the content of DEF:

    $ cd /path/to/DEF
    $ git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t-&DEF/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
         git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD
    

    Now your directory structure is DEF/DEF/your_code

  3. Finally, create the PPPrepository and pull both ABCand DEFinto it:

    $ mkdir /path/to/PPP
    $ cd /path/to/PPP
    $ git init
    $ git pull /path/to/ABC
    $ git pull /path/to/DEF
    

    Now you have PPP/ABC/your_codeand PPP/DEF/your_code, along with all the history.

  1. 将 的内容移动ABCABC/子目录,并修复历史记录,使其看起来一直在那里:

    $ cd /path/to/ABC
    $ git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t-&ABC/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
         git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD
    

    现在你的目录结构是 ABC/ABC/your_code

  2. 内容相同DEF

    $ cd /path/to/DEF
    $ git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t-&DEF/-" |
         GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
         git update-index --index-info &&
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD
    

    现在你的目录结构是 DEF/DEF/your_code

  3. 最后,创建PPP资源库和拉都ABCDEF到它:

    $ mkdir /path/to/PPP
    $ cd /path/to/PPP
    $ git init
    $ git pull /path/to/ABC
    $ git pull /path/to/DEF
    

    现在您拥有PPP/ABC/your_codePPP/DEF/your_code以及所有历史记录。

You should probably ask you collegues to run the previous commands on their system, in order for everyone to be synchronized.

你可能应该让你的同事在他们的系统上运行以前的命令,以便每个人都同步。

Note:the funky filter-branchcommands come from the man page. :-)

注意:时髦的filter-branch命令来自手册页。:-)

回答by Thomas Leonard

Do you really need to merge the two existing repositories into one repository, or do you just want to group them?

您真的需要将两个现有存储库合并为一个存储库,还是只想将它们分组?

If you just want to group them, then git-submodule will do what you want: you'll end up with three repositories where the top-level one links to the current two.

如果你只是想对它们进行分组,那么 git-submodule 会做你想做的:你最终会得到三个存储库,其中顶级的一个链接到当前的两个。

As a guide:

作为指南:

  • You should merge them into a single repository if you're going to increase the coupling between them so that it no longer makes sense to use one version of repo A with a different version of repo B.

  • You should use submodules if they remain somewhat separate (it would sometimes makes sense to work on one in isolation), but you want the convenience of being able to work with them together (e.g. download both at once, checkpoint known-good states across both, etc).

  • 如果您要增加它们之间的耦合,您应该将它们合并到一个存储库中,以便将一个版本的 repo A 与不同版本的 repo B 一起使用不再有意义。

  • 您应该使用子模块,如果它们保持某种程度的独立(有时单独处理一个模块是有意义的),但您希望能够一起使用它们的便利(例如,同时下载两个,检查点已知良好状态跨两个, 等等)。

Using submodules will avoid problems with existing copies of the repositories, since the history doesn't change. Merging them will create a new history and it will be harder for people working from the existing branches to merge their changes.

使用子模块将避免存储库现有副本的问题,因为历史不会改变。合并它们将创建一个新的历史,对于在现有分支上工作的人来说,合并他们的更改将更加困难。

回答by claf

For me it seems hard to do the way you want it because the history of both project won't merge.

对我来说,按照你想要的方式做似乎很难,因为两个项目的历史不会合并。

My best advice would be to create a new repository to contain ABC and DEF, keeping old repo from these two to have history backup and start a fresh history with this new project.

我最好的建议是创建一个包含 ABC 和 DEF 的新存储库,保留这两个旧存储库以进行历史备份并开始新项目的新历史记录。