Adding a Git Remote Repository

Up to this point, we’ve only been work­ing with Git on our local machine. We’ve been mak­ing our changes and our com­mits and we have a nice Git repos­i­to­ry on our local machine. But now I want to work with some­body else on this project. I want them to be able to see the code changes that I’ve made, and I want them to be able to make their own and have us share those. 

To do that we need to set up a Git remote repos­i­to­ry. The way this works is the remote repos­i­to­ry lives on a serv­er or com­put­er some­where and I still make all my changes and my com­mits local­ly. The oth­er peo­ple work­ing on the project do the same on their local computers. 

Let’s walk through the steps:


After con­nect­ing to a remote repos­i­to­ry, we con­tin­ue work­ing on our project local­ly, com­mit­ting changes. Once we’re ready to send those changes to the remote repos­i­to­ry, we do a git-push to we push our com­mits up to the remote repos­i­to­ry, where they are then stored. This remote repos­i­to­ry is just anoth­er copy of the same Git repos­i­to­ry that we all have on our machines. The serv­er doesn’t have any­thing spe­cial on it, oth­er than that we’re push­ing and pulling from it. 


The pull is when I want to pull down an updat­ed ver­sion of the Git repos­i­to­ry that has the com­mits oth­er peo­ple have pushed up. 

That’s the basics of how a Git remote repos­i­to­ry works. Let’s go ahead and set one up for this project that we’re work­ing on right now.

Using Github as a Remote Repos­i­to­ry #

I’ve set up a remote repos­i­to­ry on GitHub to house our code from the same project we worked on ear­li­er in the course.

I’m using Github because GitHub is a real­ly sim­ple place to cre­ate a remote repos­i­to­ry. I went ahead and cre­at­ed a pub­lic repos­i­to­ry we can use for this. There are a lot of oth­er places that you can use for remote repos­i­to­ries, includ­ing set­ting up your own. But for the pur­pos­es of this course we are going to use Github because it’s sim­ple to get started.

To con­nect to the remote repos­i­to­ry from our local repos­i­to­ry, we need to set up a remote in Git. 

Let’s first see if we have any remotes set up right now:

$ git remote

If you are work­ing on a sam­ple repos­i­to­ry with no remotes (like I am) then you should also see this com­mand return noth­ing. We just get a new com­mand prompt. This means that we don’t have any remotes set up yet. Let’s do that now.

Adding a New Remote Repository

To add a remote, we need to grab the remote URL first. Depend­ing on how you have your remote repos­i­to­ry set up, you can do this either via http or ssh or even as a direct file. We’re going to use SSH.

My remote URL from Github (this is only an exam­ple URL, not a work­ing one) is: [email protected]:ryanirelan/learn-git.git

Now let’s add the remote.

$ git remote add origin [email protected]:ryanirelan/learn-git.git

We added a new remote called ori­gin and the loca­tion is the Github URL.

Ori­gin is typ­i­cal­ly the main remote repos­i­to­ry for a project. Some­times you’ll have mul­ti­ple remotes that you push to for var­i­ous work­flows, like as a way of get­ting some­thing onto a stag­ing serv­er. But that’s a bit more advanced so we’ll stick with origin for now.

Now if we run git remote we see the remote added.

To push to the remote repos­i­to­ry, we run:

$ git push origin master

If we don’t spec­i­fy the branch we want to push, Git will just push all of the avail­able branch­es. Since we don’t nec­es­sar­i­ly always want to do that, we’ll be explic­it about the branch we’re pushing.

I’m using SSH and I have my pub­lic and pri­vate SSH keys set up local­ly, and my pub­lic key shared with Github so the con­nec­tion could happen.

Once the push is com­plete we are ready to rock! Our repos­i­to­ry files should now be up on the remote repos­i­to­ry on Github.

Did You Know? #

You can also add a local repos­i­to­ry as a remote repos­i­to­ry. I know that sounds coun­ter­in­tu­itive (and it isn’t very effec­tive for col­lab­o­ra­tion or back­up!) but it is possible.

To do that we only need to spec­i­fy a file path to the .git file of anoth­er repos­i­to­ry. Let’s start there. 

Some­where else in my file sys­tem I ini­tial­ize a new repos­i­to­ry. Instead of ini­tial­iz­ing a nor­mal work­ing” repos­i­to­ry, I ini­tial­ize a bare repos­i­to­ry whose pur­pose to be shared as a remote:

$ mkdir learn-ruby-remote
$ cd learn-ruby-remote
$ git init --bare .

Now I can add that remote to my orig­i­nal learn-ruby repos­i­to­ry. Instead of using a URL for the remote I’ll use the full file path.

$ cd learn-ruby
$ git remote local /Users/ryan/training/learn-ruby-remote

I cre­at­ed a new remote repos­i­to­ry called local. I did this because we already cre­at­ed a remote named origin in the last sec­tion. Now I should be able to make some changes and push them up to the new remote repository.

$ git push local master

And that will push any changes I’ve com­mit­ted to the local remote repository.