Preliminaries

In this tutorial we show you the basics of version control using the git command line tools and GitHub as a host for our remote repositories. We will assume that you have a project directory on your local machine that you wish to deploy to your remote machine and that you have installed Git on your local machine. Installing git is as simple as running apt-get install git or yum install git on Ubuntu and CentOS respectively.

Brief Intro to Git & Github

Git is a version control system developed by the creator of Linux himself, Linus Torvalds. It allows for multiple people to work on different versions and different parts of a project at the same time and allows those with appropriate access control to perform conflict resolution (if two people are working on the same file at the same time). With Git, you can commit changes (take a snapshot of the current project directory and save it), push changes to a remote server (update changes on the master repository containing the master project), create branches (make experimental changes without losing the progress you’ve already made), and many more. The full power of Git is much too complex for one tutorial, so we will focus on initializing a local git repository, making changes, commiting changes, and pushing changes to a remote repository hosted on the popular Git hosting site, Github.

First, you’ll want to create an account on GitHub. Proceed through the registration filling out the appropriate fields. When you’re prompted to choose a plan, you can choose ‘free’ to try out the service before you make any commitments. GitHub also has special student offers you can further investigate if you’re still in school.

Creating a Remote Repository

Once you’ve made an account, you’ll want to create a repository on the site. In the top right of the dashboard, you can see a plus sign which gives you options to create a new repository once you click it. A repository (or repo) is a complete history of your project from the instant that you first initialize the repository. You can either initialize an empty repository and begin working on your project, or you can create an empty repository and pull in an existing project that you’re working on. In this tutorial, we’ll be creating a repository named “test_project” which matches the project directory on our local machine, also named “test_project”. We recommend you give the same name to your remote repository as you do your local project folder.

Pushing Some Local Code

On my local machine, I have a directory located at “~/test_project” which contains a single file “README.md”.

Now I want to inform the git program that I would like to initialize this project folder as a git repository. We do this by running the command git init (git INITialize) in the ‘test_project’ folder.

git init

Now that git is aware that the “test_project” directory is a git repository, we can begin adding files and committing them. First, let’s get a little bit of information about the status of our project. We can get this information by running git status in the project folder.

git status is a handy tool for finding out what files you’ve made changes to, which files are untouched, and which files you’ve removed from your project but not from your repository. The first line indicates that we are on the ‘master’ branch of the project. If we decide at this point to take the project in a certain direction, we can run ‘git branch new_direction’ to create a new branch of the project named ‘new_direction’ and continue to work and make changes. The benefit of this is that you can always change between different branches, allowing you to work non-linearly.

Git has notified us in red that the file ‘README.md’ is untracked. In other words, we have not added the file README.md to the repository and git will not attempt to make any changes related to README.md to the project. We can add this file to our project by running git add README.md. In general, when you have many files you need to add at once, you can run git add . to tell git to add all files and folders in the current directory. After you have ran git add README.md type git status again.

Now you can see that git is aware of the file ‘README.md’ and is ready to commit the changes (save changes). You can commit by executing git commit -m "This is my first commit!". The flag ‘-m’ allows you to write a descriptive message outlining what changes you are committing. When working as a team, descriptive commit messages are invaluable, especially when multiple people are working on different pieces of a large project that each interact with another.

git commit -m "This is my first commit!"
[master (root-commit) 70e36ab] This is my first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

Now if you run git status:

git status
On branch master
nothing to commit, working directory clean

So you’ve committed your changes and you’re read to move on and keep coding away! We’re now going to try and sync our local repository to our remote repository on GitHub. We use the git remote add command to achieve this. Run the following:

git remote add origin https://github.com/your_user_name/your_repo_name.git
git push -u origin master

You should replace the ‘your_user_name’ and ‘your_repo_name’ placeholders with the link to your GitHub repository. With the first command, you have added a ‘remote’ which is a short name for a remote repository. The name of this remote is ‘origin’ and it is referenced by the proceeding GitHub URL. The second command pushes all commits to the ‘master’ branch to the ‘origin’ remote. The following is some example output.

git push -u origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 533 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://github.com/your_user_name/your_repo_name.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Now if we point our to the link of the GitHub repository we created earlier, we should see a file tree with our files. GitHub acts as a remote backup and versioning tool that will save all of your project files in the Cloud.

Cloning the Repository on a Remote Machine

Now that we have initialized the git repo on our local machine and pushed changes to the remote repo, we can clone the repository on our remote machine. We’ll begin by SSHing into our remote machine. You’ll have to install git on your remote machine using apt-get install git or yum install git depending on your Linux distribution.

We can clone the entire state of the remote repository onto our remote machine by running

git clone https://github.com/your_user_name/your_repo_name.git

Again, replace the placeholders with your personal details. Now we have the folder ‘test_project’ on our remote machine and it contains the README.md file we created! As a final example, we will make changes to the README on our remote machine, push the changes to our remote repository, and pull those changes onto our local machine to give you a feel for the git workflow.

Try running the commands:

echo "Making a change to the README" >> README.md
git status

What we’ve done is appended the line “Making a change to the README” to our README file and ran git status. What we see is that git is aware that we’ve made changes to the README.md file and informs us of the fact. Lets execute the same shell commands as before to add the README to be added to the list of files to be committed (git add .), commit the changes (git commit), and push the changes (git push origin master).

GitHub allows you to grant access permissions to different users to allow them to push and pull code appropriately. If we check out our repository on https://github.com/your_user_name/your_repo_name.git, we should see that our changes have been reflected. Now we can pull the changes again on our local machine by running git pull origin master and continue to work on the project, remembering to commit and push changes every now and then to make sure we don’t lose any of our work.

Final Words

You’ve played around with some of git’s functionality, but We’ve only barely scratched the surface of the number of features git and GitHub have for version control. We love git here at Stack Harbor, and we love to see services such as GitHub make use of cloud technology in order to create a more efficient workflow for developers. Check out our Community Section for more content on version control, server administration, and development tasks. From all of us at Stack Harbor, ahoy!