Combining multiple git repositories

后端 未结 13 1234
眼角桃花
眼角桃花 2020-11-22 15:34

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 g

相关标签:
13条回答
  • 2020-11-22 16:35

    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.

    0 讨论(0)
提交回复
热议问题