Merge multiple git repositories into one, keeping branches history

后端 未结 1 1374
后悔当初
后悔当初 2020-12-13 07:24

I have four separate projects. They have their own git repository. and the same name of branches for all projects.

 /project/
 /project/projA/
 /project/proj         


        
相关标签:
1条回答
  • 2020-12-13 08:16

    That solution is not so far from what you tried. This works only if your different projects have no common files (otherwise it can be difficult to solve conflicts)

    # create a new repo:
    git init all_projects
    cd all_project
    # to make it more easy to understand, let's create a new branch
    git checkout -b main
    
    # import 1st project
    git remote add projectA http://projectA
    git fetch --all --tags
    git checkout masterA projectA/master
    git rebase masterA main
    # move the main branch to the current state
    git branch main -f
    # Now your branch main is at the same state as your A project
    
    # import 2st project
    git remote add projectB http://projectB
    git fetch --all --tags
    git checkout masterB projectB/master
    git rebase masterB main
    # move the main branch to the current state
    git branch main -f
    # Now your branch main contains all commits from projectA and projectB
    
    # etc..
    

    The result will be a repository with 1st all commits from project A, then all commits from project B, even if the project B has some commits dated before the last commit of project A, but this should not be a problem (and the history tree will be easier to read)

    EDIT : Sorry I just notice this not solve your problem to get all remote branches. Maybe you can find a solution based on that question, with something like this:

    for i in $(git branch -r |grep projectA|awk -F'/' '{print $2}'); do
      git checkout $i projectA/$i
      git rebase $i main
    done
    

    but this would make your tree more complex because all branches will starts from the main commit ..

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