结合多个git存储库 [英] Combining multiple git repositories

查看:85
本文介绍了结合多个git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的设置看起来像

  phd / code / 
phd / figures /
博士/论文/

由于历史原因,这些都有自己的git存储库。但我想将它们合并成一个简单的事情。例如,现在我可能会进行两组更改,并且必须执行类似于以下操作的操作:

  cd phd / code 
git commit
cd ../figures
git commit

它会是(现在)很高兴可以执行

  cd phd 
git commit

似乎有几种使用子模块或从我的子存储库中提取的方法,但这比我更复杂一些寻找。至少,我会很满意

  cd phd 
git init
git add [ [其他存储库中已存在的所有内容]]

但这看起来不像是单行。 我给出的解决方案在这里


  1. 首先做一个完整的phd目录备份:我不想承担责任为你失去多年的辛勤工作! ; - )

      $ cp -r phd phd-backup 
    phd / code 的内容移动到 phd / code / code ,并修复历史记录,使其看起来像它一直在那里(这使用git的 filter-branch 命令):

      $ cd phd / code 
    $ git filter -branch --index-filter \
    'git ls-files -s | seds-\t-& code / - |
    GIT_INDEX_FILE = $ GIT_INDEX_FILE.new \
    git update-index --index-info&&
    mv $ GIT_INDEX_FILE.new $ GIT_INDEX_FILE'HEAD


  2. phd / figures phd / thesis (只需替换 code 数字论文)。

    现在您的目录结构应该如下所示:

      phd 
    | _code
    | | _.git
    | | _code
    | | _(您的代码...)
    | _figures
    | | _.git
    | | _figures
    | | _(您的数字...)
    | _thesis
    | _.git
    | _thesis
    | _(您的论文...)
    $ b


  3. $ b

      $ cd phd 
    $ git init

    $ git pull code
    $ rm -rf code /代码
    $ rm -rf代码/git

    $ git拉数字 - 允许不相关的历史记录
    $ rm -rf数字/数字
    $ rm - rf figures / .git

    $ git pull论文 - 允许不相关历史
    $ rm -rf论文/论文
    $ rm -rf论文/ .git

    最后,您现在应该拥有自己想要的东西:

      phd 
    | _.git
    | _code
    | | _(您的代码...)
    | _figures
    | | _(您的数字...)
    | _thesis
    | _(您的论文...)


这个过程的一个好处是,它将使非版本化文件和目录就绪。



希望这会有帮助。




只要有一个警告字:如果你的 code 目录已经有一个代码子目录或文件,事情可能会非常错误(对于数字论文当然)。如果是这种情况,只需在完成整个过程之前重命名该目录或文件:

  $ cd phd / code 
$ git mv code code-repository-migration
$ git commit -m准备移植的代码目录

当程序结束时,添加最后一步:

  $ cd phd 
$ git mv code / code-repository-migration code / code
$ git commit -m代码目录迁移的最后一步

当然,如果代码子目录或文件没有版本化,只需使用 mv 而不是 git mv ,而忘记 git commit s。


Let's say I've got a setup that look something like

phd/code/
phd/figures/
phd/thesis/

For historical reasons, these all have their own git repositories. But I'd like to combine them into a single one to simplify things a little. For example, right now I might make two sets of changes and have to do something like

cd phd/code
git commit 
cd ../figures
git commit

It'd be (now) nice to just to perform

cd phd
git commit

There seems to be a couple of ways of doing this using submodules or pulling from my sub-repositories, but that's a little more complex than I'm looking for. At the very least, I'd be happy with

cd phd
git init
git add [[everything that's already in my other repositories]]

but that doesn't seem like a one-liner. Is there anything in git that can help me out?

解决方案

Here's a solution I gave here:

  1. First do a complete backup of your phd directory: I don't want to be held responsible for your losing years of hard work! ;-)

    $ cp -r phd phd-backup
    

  2. Move the content of phd/code to phd/code/code, and fix the history so that it looks like it has always been there (this uses git's filter-branch command):

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

  3. Same for the content of phd/figures and phd/thesis (just replace code with figures and thesis).

    Now your directory structure should look like this:

    phd
      |_code
      |    |_.git
      |    |_code
      |         |_(your code...)
      |_figures
      |    |_.git
      |    |_figures
      |         |_(your figures...)
      |_thesis
           |_.git
           |_thesis
                |_(your thesis...)
    

  4. Then create a git repository in the root directory, pull everything into it and remove the old repositories:

    $ cd phd
    $ git init
    
    $ git pull code
    $ rm -rf code/code
    $ rm -rf code/.git
    
    $ git pull figures --allow-unrelated-histories
    $ rm -rf figures/figures
    $ rm -rf figures/.git
    
    $ git pull thesis --allow-unrelated-histories
    $ rm -rf thesis/thesis
    $ rm -rf thesis/.git
    

    Finally, you should now have what you wanted:

    phd
      |_.git
      |_code
      |    |_(your code...)
      |_figures
      |    |_(your figures...)
      |_thesis
           |_(your thesis...)
    

One nice side to this procedure is that it will leave non-versioned files and directories in place.

Hope this helps.


Just one word of warning though: if your code directory already has a code subdirectory or file, things might go very wrong (same for figures and thesis of course). If that's the case, just rename that directory or file before going through this whole procedure:

$ cd phd/code
$ git mv code code-repository-migration
$ git commit -m "preparing the code directory for migration"

And when the procedure is finished, add this final step:

$ cd phd
$ git mv code/code-repository-migration code/code
$ git commit -m "final step for code directory migration"

Of course, if the code subdirectory or file is not versioned, just use mv instead of git mv, and forget about the git commits.

这篇关于结合多个git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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