Git Basics

Git can be a very complicated thing. Someone once told me that we mere humans have a very difficult time with it at first. I myself have had a tremendous<nowiki>[ly difficult]</nowiki> time learning how to use Git (many thanks to marktraceur for all the help). It is an incredibly robust and so a very complicated solution. What source code management system isn’t though (especially one that is command line)? This document should serve as a very high level view of how to use Git. It will not cover advanced functionality such as cherry-picking, merging, rebasing, etc. If something is not documented here, please see the Git docs or suggest it on the discussion page.

Working with Branches

Branches in Git look are like tree branches. The Git repository itself is the trunk and the branches are the various projects in the repository. Typically (hopefully) these projects are related to each other. In the case of a development project with a frequently changing database schema that you wanted to back up, the repository would have two branches: the files branch where the code files are stored, and the database branch where the database dumps are stored.

Viewing Branches

Viewing branches is simple. Type git branch and you should see output similar to the following:

$ git branch

* database

To use a different branch, a the checkout command is required. In this case, we will switch from the database branch to the master branch.

Note:Some decompression happens here so if the branch to be checked out is very large, this will likely take a few seconds.

$ git checkout master

Checking out files: 100% (6110/6110), done.
Switched to branch 'master'


Git does not have commitmentphobia. In fact, it loves commits as if it were its only purpose in life.

In most if not all source code management software, a commit is essentially a set of changes to be merged into the master repository.

To create a commit, there are several steps that need to take place.

Firstly, the changed files to be pushed to the repository need to be added. For this, we use the git add command.

$ git add ./ex1.blah
$ git add ./example2.blah

One handy bit for this is the -A switch. If used, git will recursively add all files in the specified directory that have been changed for the commit. This is very handy if many files were changed.

$ git add -A .

Once the changes files are set up for commit, we just need one more step. Run git commit and you will be taken to a text editor (likely vi - specified in the repository configuration) to add comments on your commit so you and other developers know what was changed in your commit in case something is broken or someone wants to revert.

This piece is key if you are using the git repository as a code repository rather than a versioning repository for backups. Please write in meaningful comments.

There is actually one more piece to committing a change if you have a remote repository on another box or a different location on the local box. So other developers can pull the repository and get your changes, you need to push your changes to the remote repository. Please see the Pushing Changes to a Remote Repository section for more information on this. To do this, we use the git push command.


All of this commit and commit log business is a bit worthless if we can’t look at logs. To look at the logs we use the git log command. This will open up your system’s pager (typically less is the one used) to view the logs for the current branch. If you wish to view the logs on a different branch, you can either check out that branch, or you can type git log BranchName.

A handy option for the git log command is the --name-status switch. If you use this switch, git will list all of the commit logs along with all of the files affected and what was done (modified, deleted, created, renamed) in each individual commit.

Remote Repositories

Git is a distributed code versioning system which means that every person that has pulled the repository has a complete copy of the original. This is really great for working remotely because you don’t have to be online and able to talk to the remote repository to see change history.

Adding a Remote Repository

Git needs several things to add a remote repository. Firstly, it needs a local alias for the remote repository. It also needs a username to log in to the repo with, as well as the ip address or hostname of the repository, and the path to the actual repo directory on the remote server. With that, to add a remote repository the command looks somewhat like this:

git remote add origin

Now, let’s break down what that all means since it seems a tad complicated.

git remote add origin gitman :repos/CleverProjectName

This is the command to work with remote servers in git.

Tells git we are adding a remote

The local alias for the remote. Origin is typically used here.

The username to log in to the remote server with.

This is the server where the repo is stored

This is the path to the actual repository directory. Since it does not start with a / it starts in the home directory of gitman (~/).