我可以将存储库的 .git 目录移至其父目录吗?

发布于 2024-07-15 08:44:25 字数 423 浏览 5 评论 0原文

我有两个子目录,每个子目录都有一个存储库,因此:

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

并且想将它们组合成一个存储库,因此,我假设目录结构如下:

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

这可能吗?

目前也有几个人在他们的机器上拥有该存储库。 这会让生活变得多么复杂?

塔。

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

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假扮的天使 2024-07-22 08:44:25

您可以执行您所描述的操作,如下所示:

  1. ABC 的内容移动到 ABC/ 子目录,并修复历史记录,使其看起来始终如此去过那里:

    $ cd /path/to/ABC 
      $ git 过滤器分支 --index-filter \ 
          'git ls-文件-s |   sed "s-\t-&ABC/-" | 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 过滤器分支 --index-filter \ 
          'git ls-文件-s |   sed "s-\t-&DEF/-" | 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 /路径/到/PPP 
      $ git 初始化 
      $ git pull /路径/到/ABC 
      $ git pull /路径/到/DEF 
      

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

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

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

You can do what you are describing like this:

  1. Move the content of ABC to 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 PPP repository and pull both ABC and DEF into 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_code and PPP/DEF/your_code, along with all the history.

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-branch commands come from the man page. :-)

日久见人心 2024-07-22 08:44:25

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

如果您只想将它​​们分组,那么 git-submodule 将执行您想要的操作:您最终会得到三个存储库,其中顶级存储库链接到当前两个存储库。

作为指导:

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

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

使用子模块将避免存储库现有副本出现问题,因为历史记录不会改变。 合并它们将创造一个新的历史,并且从现有分支工作的人们将更难合并他们的更改。

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.

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).

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.

善良天后 2024-07-22 08:44:25

(2022) 答案:考虑 git filter-repo 而不是 git filter-branch

filter-repo 速度要快得多,是此类情况的推荐方法。

示例用例:

  1. 合并 repoA/develop -> repoB/develop 并进入文件夹 /android

示例命令(重命名 baseDir 中的所有提交并将它们移动到 /android/path/to/file

filter-repo --path-rename :android/ --force

文档:https://github.com/newren/git-filter-repo

(2022) answer: consider git filter-repo over git filter-branch.

filter-repo is much faster and is the recommended approach for something like this.

Example use case:

  1. Merging repoA/develop -> repoB/develop and into the folder /android

Example command (which renames all commits from the baseDir and moves them into the /android/path/to/file

filter-repo --path-rename :android/ --force

Documentation: https://github.com/newren/git-filter-repo

夕嗳→ 2024-07-22 08:44:25

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文