How do you combine two git repos?

I have a main repo (repo 1) that I work with. I have another repo (repo 2) that needs to fit into the first one and I’m not sure how I could have them both in the same folder. The idea is that I have a standard codebase that I need in each project – yet each project is it’s own git repo.

/project
    /.git(repo 2)
    /.git(repo 1)
    /repo_2_sub
        /repo_2_sub_sub
            /repo_1_sub_sub
    /repo_1_sub
        /repo_1_sub_sub
        /repo_2_sub_sub

None of the files overlap – but some of the folder structures do. So sometimes certain folders from one repo will be in the other repo.

  • GIT - unable to remove specific branch from remote repository
  • How to remove all occurences of a file in index, from all git commits
  • LibGit2Sharp and TFS Git repository
  • git clone from GitHub RPC failed; result=56, HTTP code
  • Using git to track only some of a Unity project
  • New git repository in root directory to subsume an exist repository in a sub-directory
  • How can I work so these two repositories build the complete codebase?

    UPDATE

    Both git repos exist in the same root project folder level. They cannot be submodules since they cross back and forth through each other as shown above. They are not separate folders.

    UPDATE 2

    Wait, maybe this is easier than I thought. Can you clone a standard codebase repo and then create a new branch that is your project and then just keep merging that branch with the codebase repo each time it changes?

  • Proper version control workflow for a mixture of repositories
  • Difference between Git and Nexus?
  • How works Public-key cryptography on Github?
  • How to revert initial git commit?
  • Make a git-alias or similar that includes “branchfolder/” - part of the branch name
  • Understanding .gitignore and build directory
  • 3 Solutions collect form web for “How do you combine two git repos?”

    Wait, maybe this is easier than I
    thought. Can you clone a standard
    codebase repo and then create a new
    branch that is your project and then
    just keep merging that branch with the
    codebase repo each time it changes?

    You could do that. I have also a project where I’m doing it like this. Most of the code is the same for all subprojects. I create a new branch for every project to keep the files that only belong to that project separated. I regularly merge the master branch into the other branches.

    You could also create clones of the main repository. Then you would just merge the changes from the origin/master branch into the master branch in each of your projects.

    Edit: In order to have two .git directories, when you init them, there would be some extra work. When you init, do:

    git --git-dir=.git1 --work-tree=. init
    git --git-dir=.git2 --work-tree=. init
    

    Now you have two git dirs in the same directory. Not very pretty. At this point, make sure that you have .git1 and .git2 in both .gitignores, since they aren’t ignored by default. Additionally, every git command you run must also have the –git-dir argument to specify which repository you’re interacting with. You could setup aliases for that.

    Since they’re in the same base directory and submodules won’t work, you could put ‘.gitignore’ files inside both ‘.git/info/exclude’ directories. Putting them in .git/info/exclude makes sure that the other repo doesn’t pick up the exclusions.

    It may take some maintenance if the two repos differ by a finer granularity than folders, but if you specify in each .gitignore the files that belong only to the OTHER repo, it should work.

    See: gitignore

    Use git submodule

    Update:

    The standard code base that needs to be a part of all the repos has to be a seperate distinct git repository.

    git submodule add <repo_nick_name> <repo_path>
    

    will add that repo to this one. On cloning and pushing, only the .gitmodules file and the hash at the repo will be pushed/cloned.

    git submodule init
    

    and

    git submodule update
    

    respectively initializes and updates the remote submodule repos.

    For detailed info, check the git community book documentation on git submodules and/or scot chacon’s screencast on the same.

    Git Baby is a git and github fan, let's start git clone.