Multiple Remote Repositories with Git

Multiple Remote Repositories with Git

How-To

  • Check your remote repository info. In this example I have a project pushed to Github

    1$ git remote -v show
    2origin https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (fetch)
    3origin https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (push)
  • If you check your git configuration (PROJECT_NAME/.git/config), you will see:

    1[core]
    2 repositoryformatversion = 0
    3 filemode = true
    4 bare = false
    5 logallrefupdates = true
    6[remote "origin"]
    7 url = https://github.com/GandhiNN/iot-vernemq-ktig-stack.git
    8 fetch = +refs/heads/*:refs/remotes/origin/*
    9[branch "dev"]
    10 remote = origin
    11 merge = refs/heads/dev
    12[http]
    13 sslverify = false
  • Suppose that you want to add a remote git repository on your Bitbucket project.

    1$ git remote set-url origin --add https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git
  • To be able to do controlled Git workflow (e.g. just pull from Github remote or just push to Bitbucket remote), we add remote repository information using git remote.

    1$ git remote add bitbucket https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git
    2
    3$ git remote add github https://github.com/GandhiNN/iot-vernemq-ktig-stack.git
  • Now our project’s Git configuration contains the information of the new repositories just added.

    1[core]
    2 repositoryformatversion = 0
    3 filemode = true
    4 bare = false
    5 logallrefupdates = true
    6[remote "origin"]
    7 url = https://github.com/GandhiNN/iot-vernemq-ktig-stack.git
    8 fetch = +refs/heads/*:refs/remotes/origin/*
    9 url = https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git
    10[branch "dev"]
    11 remote = origin
    12 merge = refs/heads/dev
    13[http]
    14 sslverify = false
    15[remote "bitbucket"]
    16 url = https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git
    17 fetch = +refs/heads/*:refs/remotes/bitbucket/*
    18[remote "github"]
    19 url = https://github.com/GandhiNN/iot-vernemq-ktig-stack.git
    20 fetch = +refs/heads/*:refs/remotes/github/*
  • In the sequence of “origin” block, Github remote takes precedence over the Bitbucket one; If you run git pull, it will still pull from only Github. If you want to make Bitbucket as the first source, you can just rearrange the sequence of “url.”

    1$ git remote -v
    2bitbucket https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (fetch)
    3bitbucket https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (push)
    4github https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (fetch)
    5github https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (push)
    6origin https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (fetch)
    7origin https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (push)
    8origin https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (push)
  • If you want to push your updates to the Bitbucket remote, you can run git push -u bitbucket <branch_name>

    1$ git push -u bitbucket master
    2Username for 'https://<bitbucket_url>': <bitbucket_username>
    3Password for 'https://<bitbucket_username>@<domain>@<bitbucket_url>':
    4Enumerating objects: 32, done.
    5Counting objects: 100% (32/32), done.
    6Delta compression using up to 8 threads
    7Compressing objects: 100% (26/26), done.
    8Writing objects: 100% (32/32), 12.85 KiB | 4.28 MiB/s, done.
    9Total 32 (delta 1), reused 0 (delta 0)
  • You can do the same with your Github target.

    1$ git push -u github master
    2Username for 'https://github.com': GandhiNN
    3Password for 'https://GandhiNN@github.com':
    4Enumerating objects: 32, done.
    5Counting objects: 100% (32/32), done.
    6Delta compression using up to 8 threads
    7Compressing objects: 100% (26/26), done.
    8Writing objects: 100% (32/32), 12.85 KiB | 4.28 MiB/s, done.
    9Total 32 (delta 1), reused 0 (delta 0)
    10remote: Resolving deltas: 100% (1/1), done.
  • Let’s say that you want to set Bitbucket as the default remote for your git push / git pull activities, you need to add the Bitbucket URL as the remote “origin”

    1$ git remote add origin https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git
    1$ git remote -v
    2bitbucket https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (fetch)
    3bitbucket https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (push)
    4github https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (fetch)
    5github https://github.com/GandhiNN/iot-vernemq-ktig-stack.git (push)
    6origin https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (fetch)
    7origin https://<bitbucket_url>/scm/<bitbucket_project_name>/iot-vernemq-kafka-poc-stack.git (push)

Notes

  • To skip SSL verification in your git workflow on your project you can use:

    1$ git config --local http.sslverify false
  • To save credentials used by your git workflow on your project you can use:

    1$ git config --local credential.helper store