Github rocks. I started using it about a year ago and it has already saved my ass, I mean my code, from two computer crashes. Github is a service that lets you back-up and control versions of all documents associated to a project.
Git was created to support the Linux project. It is extremely powerful, it was built to enable collaboration between multiple users and even lets users create more than one concurrent version (or branch in git lingo) of the project. I use only a small part of the functionality offered by git,
I am currently working to set-up Github on my computer so that I can restore the code I lost during my most recent computer crash. Since I am having to re-learn the not-so-simple process of installing Github, I decided to create this simple tutorial to document my learnings.
My motives are only partially altruistic, I know at some point in the near future I will either need to restore my own code again and I’ve already had a few people ask for help to set-up GitHub on their computers. Here are the official installation instructions from GitHub.
Time Requirements: the answer to this question depends on what software you have loaded on your computer. If you have none of the required software it can take up to 5 hours (downloading Xcode can takes several hours). If you have all of the required software it can take you as little as 30 minutes.
Software Requirements: before you can install Github you need to have MacPorts loaded on your computer, but before you can do that you need to install Xcode. So start off by downloading both. You can download MacPorts from the link below. You will need to purchase Xcode from the app store on your mac. I recommend that you download the latest version of both to ensure that they will work together.
Note: Xcode takes several hours to download because it is huge (over 4GB). You can also get Xcode from your Mac install disc, though it probably won’t be the latest version and this could potentially cause issues.
Installing Software: once you have downloaded Xcode and MacPorts you are ready to get started. Github is installed by running a Unix command on the Terminal utility (more about the Terminal utility below the installation instructions). If you have Xcode and MacPorts installed in your machine skip to step 2.
- Install Xcode and then MacPorts to set-up your computer
- Open the Terminal utility, which can be found in the directory “Applications/Utilities”
- Type “sudo port install git-core” into the Terminal and press enter
- When prompted enter your password and press return (password will not show up)
- Wait for the installation process to end, which can take more than an hour depending on your internet connection
Terminal is an application that is included on all Macs. It provides users with access to the Unix kernel, so that they can run Unix commands and scripts. In order to use GitHub you will have to familiarize yourself with some basic Terminal commands. You will need to keep the Terminal utility open for the rest of the setup. Here is a link to one of my recent post with an overview of basic Unix concepts and commands.
Setting Up Github: once you have installed git you need to set your name and email address. This information will be used to sign files when you save them (or commit in git lingo) to a git repository on your computer, or upload them (or push in git lingo) to a git repository online. To set your name and email use the commands below on the Terminal.
git config –global user.name “your name”
git config –global user.email “your_email@email_server.com”
Downloading from a Repository: To download code from an existing repository you first need to get the project’s git URL. This URL can begin with “ssh://”, “http://”, “git://” or just a Github username. Then type the command below into Terminal utility and press enter. Git will create a new directory with the name of the project, and download all of the project files into that directory.
git clone http://www.sample_url.com/sample_folder/sample.git
Creating a New Local Repository: To create a repository from an existing project you need to navigate that project’s directory using terminal.
In the Terminal utility you can navigate through directories (or folders) using the “cd” command. Typing “cd folder_name” takes you to a folder called folder_name that exists within the current folder; typing “cd ..” takes you to the folder that contains the current one. You can also use the “ls” command to get a list of all the folders that are contained in your current directory. Once you have reached to the appropriate directory just type:
Git will reply with “Initialized empty Git repository in .git/” and you will notice that a new hidden folder labelled .git has been added to your project’s directory.
Backing up to Local Git Repository: Once you have a git directory set-up it is easy to back-up your project, and restore it when needed. Here are the basic overview of the process and commands that you will need to use.
First you need to add your files to the staging area using the “git add” command. You can add a single file to the staging area using “git add filename”, or you can add the entire project to the staging area using “git add .”
Once you have added files to the staging area you will need to use the “git commit” command to add the files to your local repository. This command will move all files on the staging area to the repository. When typing this command you can use the -m modifier to add a comment about the updates that have been made to the project. Here is an example:
git commit -m “this area can feature information about updates to the code”
Online Repositories and Advanced Functionality: With this basic knowledge you can create local repositories for version control. This is all I am going to cover today. However, to take advantage of the full benefits provided by git you will need to learn how to restore your code to a previous version and how get set-up to use an online repository. Here are some helpful links for you take these next steps: