Accessing a Git Repository through multiple Remotes

To access an internally hosted Git repository (in this example hosted with GitBlit) from remote one option is to create a tunnel with OpenSSH.

$ ssh -L localhost:12345:gitblit.server:12345 -i ~/.ssh/tunnel_rsa tunnel@remote-gateway

A convenient way is to store the Git credentials in .ssh/config in your local user’s home directory:

Host gitblit_tunnel
  Hostname localhost
  Port 12345
  User fluffi
  IdentityFile ~/.ssh/gitblit_rsa

With the tunnel established all you need to do is to add a second remote to your local working copy:

$ git remote add gitblit_tunnel ssh://gitblit_tunnel/repositories/My-Project.git

Verify the new configuration - list all Git remote repositories:

$ git remote -v
gitblit_tunnel  ssh://gitblit_tunnel/repositories/My-Project.git (fetch)
gitblit_tunnel  ssh://gitblit_tunnel/repositories/My-Project.git (push)
origin  ssh://gitblit.server:12345/meadows/repositories/My-Project.git (fetch)
origin  ssh://gitblit.server:12345/meadows/repositories/My-Project.git (push)

Git it done ...😎

Checkout a branch from a remote repository is also discussed in How do I check out a remote Git branch?. In our example, grabbing the branch sample could be done as follows:

$ git fetch gitblit_tunnel
$ git checkout -b sample gitblit_tunnel/sample

If you keep the same name you can use the shorthand git checkout -t gitblit_tunnel/sample.