Git: pushing and pulling from multiple repos

Lets assume you’ve already cloned a remote repo and have been working with it. Now, someone has set up a second repo out there for the same codebase, and you’d like to interact with both.

*Please note: The following is based on the assumption that you have write privileges to the second repo, but don’t worry, you do essentially the same thing if you don’t and I’ll cover the differences at the end. *

  1. git remote add <local_name_for_remote_repo>
  2. git checkout -b <local_name_for_remote_repo>/master
    • could be any branch on that repo, not necessarily master

Now, your new local branch is now set up to track (push and pull to/from) the remote repo’s “master” branch. All your pre-existing branches will continue to track whatever they were tracking previously

A practical example:

git remote add repo2  git@github.com:masukomi/kudos.git
git checkout -b repo2_kudos -t repo2/master

Workflow

Let’s say you’ve decided to add a new feature in the kudos repo we just connected to.

  1. Checkout the repo2_kudos branch
  2. git pull to get the latest code from repo2/master
  3. git checkout -b adding_feature_x
    • this new topic branch is not yet tracking anything so if you attempt to push or pull it won’t work, because it won’t know what endpoint you want to interact with.
  4. Make your changes on this topic branch.
  5. When you’re ready run git push -u repo2 adding_feature_x
    • This will do three things:
      1. It will create a new branch in the repo2 repository named “adding_feature_x”
      2. It will push the contents of your adding_feature_x branch into the corresponding one on repo2.
      3. It will configure things so that your local adding_feature_x branch is now tracking the remote adding_feature_x branch so push and pull will work without additional arguments. You must pass the -u parameter for tracking to be set up here. If you fail to pass the -u parameter at this point then push and pull won’t work without additional parameters. You can, however, set up tracking after the fact with git branch -u repo2/adding_feature_x

Now that you’ve pushed your changes to the new branch on the remote repo you can initiate pull requests from your topic branch into the main branches (if you’re using github) or you can simple tell others about it so that they can interact with it.

Keep in mind that it’s best practice to regularly pull from your main branch into your topic branch (repo2_kudos into adding_feature_x in this case) so as to avoid large merge conflicts. Alternately you could pull from the corresponding main branch on the second repo (repo2/master in this case) since they should be equivalent.

When you don’t have write privileges

If you don’t have write privileges to the second repo things change slightly. The difference is in where you push your new topic branch (adding_feature_x) to. You can either push as a new branch on whatever repo your local repo was cloned from (if you’ve got write privileges to that) or you can create a third remote repo that you control, and push the branch to that. The third repo would be configured in the same way as the second, and you should be able to push to it in the same way we pushed to the second. Git makes it easy to flow code from one repo to another, or through multiple repos.