Adding a Git Remote Repository
Up to this point, we’ve only been working with Git on our local machine. We’ve been making our changes and our commits and we have a nice Git repository on our local machine. But now I want to work with somebody 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 repository. The way this works is the remote repository lives on a server or computer somewhere and I still make all my changes and my commits locally. The other people working on the project do the same on their local computers.
Let’s walk through the steps:
After connecting to a remote repository, we continue working on our project locally, committing changes. Once we’re ready to send those changes to the remote repository, we do a
git-push to we push our commits up to the remote repository, where they are then stored. This remote repository is just another copy of the same Git repository that we all have on our machines. The server doesn’t have anything special on it, other than that we’re pushing and pulling from it.
The pull is when I want to pull down an updated version of the Git repository that has the commits other people have pushed up.
That’s the basics of how a Git remote repository works. Let’s go ahead and set one up for this project that we’re working on right now.
Using Github as a Remote Repository #
I’ve set up a remote repository on GitHub to house our code from the same project we worked on earlier in the course.
I’m using Github because GitHub is a really simple place to create a remote repository. I went ahead and created a public repository we can use for this. There are a lot of other places that you can use for remote repositories, including setting up your own. But for the purposes of this course we are going to use Github because it’s simple to get started.
To connect to the remote repository from our local repository, 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 working on a sample repository with no remotes (like I am) then you should also see this command return nothing. We just get a new command 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. Depending on how you have your remote repository 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 example URL, not a working 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 origin and the location is the Github URL.
Origin is typically the main remote repository for a project. Sometimes you’ll have multiple remotes that you push to for various workflows, like as a way of getting something onto a staging server. 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 repository, we run:
$ git push origin master
If we don’t specify the branch we want to push, Git will just push all of the available branches. Since we don’t necessarily always want to do that, we’ll be explicit about the branch we’re pushing.
I’m using SSH and I have my public and private SSH keys set up locally, and my public key shared with Github so the connection could happen.
Once the push is complete we are ready to rock! Our repository files should now be up on the remote repository on Github.
Did You Know? #
You can also add a local repository as a remote repository. I know that sounds counterintuitive (and it isn’t very effective for collaboration or backup!) but it is possible.
To do that we only need to specify a file path to the
.git file of another repository. Let’s start there.
Somewhere else in my file system I initialize a new repository. Instead of initializing a normal “working” repository, I initialize a bare repository whose purpose 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 original
learn-ruby repository. 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 created a new remote repository called
local. I did this because we already created a remote named
origin in the last section. 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 committed to the
local remote repository.