On the fifth day of Christmas, my true love gave me… five reasons to sing.
Today, we’re going to break free of the bonds of our own computers, and learn about sharing changes remotely with Git.
I’ve been showing how to use Git for version control of files by discussing it in terms of the song “The Twelve Days of Christmas”. If you want to go back and see what we’ve covered so far, here are the verses of the song up until now:
- The 12 Days of Git: Learn Git over the Holidays
- The 12 Days of Git, Day 2: Tracking Files with Git
- The 12 Days of Git, Day 3: Viewing Git History
- The 12 Days of Git, Day 4: Double-checking Changes with Git
Today, I’m going to show you how share your project—be it website, computer program, cookbook, whatever—with the wider world through Git. If you remember from Verse 1, Git is a distributed version control system. That means that wherever you copy a Git repository, that copy has a complete record of all the changes that have been made to it. You can share the copy over a local area network, a thumb drive, or any other way you can transfer files, but the most easiest and most common way is to set up another remote Git repository on an online service.
Setting up a remote Git repository
To begin, set up a free account on one of the two most popular remote Git hosting services, either GitHub or Bitbucket. Of the two, GitHub is by far more popular for open source projects and prides itself on creating communities of developers; however, in order to keep any of your Git projects private, you will have to upgrade to a paid account. Bitbucket, on the other hand, will let you have unlimited private repositories for free, but will only let you share with up to five (5) other users among all your projects, because they are geared more to the enterprise market. You may want to create accounts on both, one account on GitHub so that you can use and contribute to open-source projects, and one on Bitbucket so that you can keep your personal projects private.
After you have set up an account, click the link to Add a New Repository. This is a plus icon (+) in the menubar for GitHub, or a Create button for Bitbucket.
In the form that appears, fill in the Repository name, which is the only required field. Then click the button labelled Create repository.
Now, you will see some options for populating the remote repository from the command line. We’ll use the option for an existing project. You can copy the command to add this remote from the screen that GitHub or Bitbucket shows and paste it in your terminal. (Be sure your project is your current working directory.)
git remote add origin https://github.com/vjwilson/santa-tracker.git
This sets up a link between your local repository and the one you just created in your remote account, and it assigns it the name origin . To check that it is set up, type:
git remote -v
You should see something like this:
origin https://github.com/vjwilson/santa-tracker.git (fetch) origin https://github.com/vjwilson/santa-tracker.git (push)
Now, copy the next command suggested on the GitHub or Bitbucket setup screen, and paste that into your terminal.
git push -u origin master
The output of that command will look similar to this.
Counting objects: 4, done. Delta compression using up to 8 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (4/4), 501 bytes | 0 bytes/s, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/vjwilson/santa-tracker.git * [new branch] master -> master
Refresh your repository’s page on your remote service, and you will see your files there.
If you click on the link for Commits, you will see a visual representation of the output you would get with the
git log command. Clicking on an individual commit in that list will allow you to see the output similar to the output of
git diff between that commit and the commit before it.
From now on, after you have committed any new changesets to your local copy of the Git repository, you can send those changes to your remote copy just by typing the following in your terminal while in that repository’s folder. (By default, after you push once with the -u option, Git assumes you want to send your master branch to the remote labelled origin, so you can omit those two names from the command after the first time.
To see all the options available for the commands we’ve learned today, check them out in the online Git reference:
Don’t forget to check back tomorrow for the next verse, Making a Copy of a Git Repository.