Github has become the go to place to get software, at least open source software. It has pretty much taken over from the previous download hub, SourceForge. One of the reasons for that is how easy it is to clone software from Github, but even more important, it's how easy it is to keep those files up to date with the changes in developing software.
Github is a collection of private and public repositories. As a matter of fact, it is the largest code repository in the world with over four million repositories at present count. You pay for private repositories, but all public repositories are free, which is another reason Github has become popular.
A repository is a collection, or database, of the versions of a project that you have been saved over the projects history. Each version is a snapshot of your file contents and file structure at that instance in time. Git keeps track of each snapshot in the repository. When you do a git clone, you copy the entire repository to your local machine from the time that the repository was created, and thus, you get the entire project history with the clone. Another reason people like to clone software from Github.
If you'd like to have a search box on Github to find projects, or you wanted to copy a private repository , you'll have to sign up for a free Github account on their web site, which I highly recommend.
What can you an do on Github? You can contribute code and project documentation to an open source project, this is done by getting a "fork" of the project. If you want to contribute some unofficial documentation, you can do this with Github Wikis. If you like a particular developer's work, you can follow his work on Github. And you can socialize and leave messages and comments with your favorite developers. If you just want to get a copy of an application's software and the project's repository, you do a git clone, which is the subject of this article.
Let's get started. Before you can use any git commands, you need to install Git on your local machine. Git runs on all platforms. If you've never used Git, this blog is a good place to get started. If your using Windows, I've written several articles here at GeekGumbo that will get Git installed, and ready to use on your local computer.
Once you have Git installed, you can check to see if it is running properly by running "git version" in the git command window. OK, I'll assume you've got Git all set up on your computer.
You can copy code from Github with any number of growing tools and graphical interfaces, for example, a dedicated bridge tool like hg-Git, an IDE utility like eGit, or a dedicated graphical application like SmartGit, or to me, the easiest and simplest way, by using the Git command line, which is what we'll use.
Let's pick a project to clone. For this example, I'm going to use the Zend Framework 2 project which is currently under development and in beta format. Go to the zf2 project page on Github here. There is a different URL for different ways to copy a project. Click on the "Git Read Only" menu button about 1/4 of the way down the page. Git clones are read only. The URL in the window will change to: git://github.com/zendframework/zf2.git. This is the URL we want to use with the git clone command.
Let's do the clone. Back on your local machine, in your git window go to the directory one above where you want your Zend Framework software directory to reside. Since I'm using WAMP, for me, that is the "/c/wamp/www" directory. The clone process automatically creates a sub-directory for the project, in this case, zf2, and populates the sub-directory with the project files. We'll end up with files in "/c/wamp/www/zf2." Let's do it. Type, no quotes needed:
git clone git://github.com/zendframework/zf2.git
This project is fairly large, let it work all the way through until the command prompt returns. Here's what it looks like:
That's it. Now that was tough, NOT! Remember, we didn't even log into the Github site to do the clone. Let's go to our new directory, zf2.
You end up with the project and its git repository installed in one fell swoop, nice. If we do a git branch, we see we have a master branch. When you do a git clone, it appears that only the master branch comes down when there may be other branches.
To see what other branches are available we type:
git branch -a
Here we see that there is one other branch available, release/3.0.0. To see this branch we'll have to create the branch in our local repository and then check it out, like so:
git branch release/3.0.0 git checkout release/3.0.0
We can also looks at the project history. Type:
We get this:
We have several previous branches that were merged back into master. That doesn't mean you can't go back to the old branches. You have your entire project history when you do a git clone. We're in Git, we can go back to any version of the project, but we probably don't want to do that in this case, since we're not developing the project, and just want to stay current. Let's go back to the master branch, and then do a git status. followed by a look at the .git/config file.
git status cat .git/config
It looks like this:
Our "origin" URL has been entered in our config file, and our "master" branch is directed to origin. This was done for us when we ran our original "git clone." These configuration settings also encompasses any other branches, like the "release/3.0.0" branch, as we'll see in a moment. The settings gives you a direct connection to the remote repository on Github, so you don't need to type git clone again to get an update of the project, all you need to do is type "git pull origin" for each branch. Let's do it.
git pull origin master git pull origin release/3.0.0
What's happened with "git pull origin" is that Git went up to the origin repository at the URL specified in our .git/config file, checked to see if any files have changed at "origin," our remote repository. If there were any new files, or any changes at all, it would have copied them down to your local machine, and then merged them into your master branch, and then your release/3.0.0 branch, bringing your project instantly up-to-date.
Since we now have two branches in our project, we need to specify in our git pull which branch we want to update. We did pull both branches, so the project is brought up-to-date. In this case, since we just cloned the files, git tells us the project is "Already up-to-date"
Pretty impressive. With one simple git pull command any time in the future, we keep up to date with a project under development. Now you can see why so many people like cloning projects from Github as opposed to downloading, unzipping, and coping files. The next time you want to copy some application to your computer, considering cloning it from Github instead.