Combining multiple git repositories

后端 未结 13 1362
眼角桃花
眼角桃花 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:27

    @MiniQuark solution helped me a lot, but unfortunately it doesn't take into account tags which are in source repositories (At least in my case). Below is my improvement to @MiniQuark answer.

    1. First create directory which will contain composed repo and merged repos, create directory for each merged one.

      $ mkdir new_phd
      $ mkdir new_phd/code
      $ mkdir new_phd/figures
      $ mkdir new_phd/thesis

    2. Do a pull of each repository and fetch all tags. (Presenting instructions only for code sub-directory)

      $ cd new_phd/code
      $ git init
      $ git pull ../../original_phd/code master
      $ git fetch ../../original_phd/code refs/tags/*:refs/tags/*

    3. (This is improvement to point 2 in MiniQuark answer) Move the content of new_phd/code to new_phd/code/code and add code_ prefeix before each tag

      $ 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' --tag-name-filter 'sed "s-.*-code_&-"' HEAD

    4. After doing so there will be twice as many tags as it was before doing filter-branch. Old tags remain in repo and new tags with code_ prefix are added.

      $ git tag
      mytag1
      code_mytag1

      Remove old tags manually:

      $ ls .git/refs/tags/* | grep -v "/code_" | xargs rm

      Repeat point 2,3,4 for other subdirectories

    5. Now we have structure of directories as in @MiniQuark anwser point 3.

    6. Do as in point 4 of MiniQuark anwser, but after doing a pull and before removing .git dir, fetch tags:

      $ git fetch catalog refs/tags/*:refs/tags/*

      Continue..

    This is just another solution. Hope it helps someone, it helped me :)

提交回复
热议问题