Merge git repository in subdirectory

后端 未结 7 1324
有刺的猬
有刺的猬 2020-11-30 19:32

I\'d like to merge a remote git repository in my working git repository as a subdirectory of it. I\'d like the resulting repository to contain the merged history of the two

7条回答
  •  鱼传尺愫
    2020-11-30 20:02

    I found the following solution workable for me. First I go into project B, create a new branch in which already all files will be moved to the new sub directory. I then push this new branch to origin. Next I go to project A, add and fetch the remote of B, then I checkout the moved branch, I go back into master and merge:

    # in local copy of project B
    git checkout -b prepare_move
    mkdir subdir
    git mv  subdir/
    git commit -m 'move files to subdir'
    git push origin prepare_move
    
    # in local copy of project A
    git remote add -f B_origin 
    git checkout -b from_B B_origin/prepare_move
    git checkout master
    git merge from_B
    

    If I go to sub directory subdir, I can use git log --follow and still have the history.

    I'm not a git expert, so I cannot comment whether this is a particularly good solution or if it has caveats, but so far it seems all fine.

提交回复
热议问题