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

前端 未结 3 861
傲寒
傲寒 2020-12-02 08:31

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

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


        
相关标签:
3条回答
  • 2020-12-02 09:03

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

    0 讨论(0)
  • 2020-12-02 09:04

    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.

    0 讨论(0)
  • 2020-12-02 09:10

    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.

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