Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

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

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.

question from:https://stackoverflow.com/questions/614229/can-i-move-the-git-directory-for-a-repo-to-its-parent-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...