sandeepkunkunuru
sandeepkunkunuru

Reputation: 6440

Configure two git remote repositories to two corresponding local branches

Rephrasing the question : how to configure a remote git repository as new branch of an existing local repository whose single branch is already linked to a different remote repository.

I have two git repositories(projects) hosted on two different sites (github and an intranet site).

They are similar to each other since they started from the same base folder. I want to combine these two projects into one single git project such that

Upvotes: 3

Views: 708

Answers (1)

sandeepkunkunuru
sandeepkunkunuru

Reputation: 6440

Looking at a previous Stackoverflow question I figured out a way to achieve this as follows

  • cd to local folder linked to github repo

    cd ~/projects/prj_a
    
  • update .git/config to restrict branch mapping such that local "master" branch points to github's "master" branch

    [remote "origin"]
    url = [email protected]:user/prj_a.git
    fetch = +refs/heads/master:refs/remotes/origin/master
    
  • Add new git remote

    git remote add intranet git@intranet_server:user/intranet_project.git
    
  • update .git/config such that new git remote is fetched into new remote-branch

    [remote "intranet"]
    url = git@intranet_server:user/intranet_project.git
    fetch = +refs/heads/master:refs/remotes/intranet/intranet_branch
    
  • Fetch branches from new remote repo

    git fetch intranet
    
  • checkout new remote's branch into a new local branch. This will also switch to new branch.

    git checkout -b intranet_branch intranet/intranet_branch
    
  • merge master branch which is pointing to github

    git merge master
    
  • Resolve merge conflicts and commit

  • push from intranet_branch to intranet_repo's master branch

    git push intranet intranet_branch
    

Test everything once more

    $ git checkout master
      Switched to branch 'master'
      Your branch is up-to-date with 'origin/master'.
    $ git pull origin master
      ....
      * branch            master     -> FETCH_HEAD
      Already up-to-date.
    $ git push origin master
      Everything up-to-date
    $ git checkout intranet_branch 
      Switched to branch 'intranet_branch'
      Your branch is up-to-date with 'intranet/intranet_branch'.
    $ git pull intranet intranet_branch 
      ...
      * branch           intranet_branch -> FETCH_HEAD
      Already up-to-date.
    $ git push intranet intranet_branch 
      Everything up-to-date

Upvotes: 2

Related Questions