Git Crash Course

May 03, 2012

Our current team has decided to adopt Git and some members aren't familiar with it.  I wanted to put together a short introduction to the Git command line for these team members.  The goal is to make team members productive immediately, while learning the nuances of Git over time.  My introduction assumes that you have Git installed and setup.  For installation instructions, go to http://git-scm.com/.  For instructions on configuring Git, I recommend Github's tutorials for Windows, OS X and Linux.

Cloning a Repository

First, you need to get a copy of the source code on your machine from a repository.  With Git, you do this by cloning the repository locally.  Since Git is a distributed version control system (DVCS), it will copy the whole source code repository locally.  For more on DVCS, check out this.

To clone a remote repository, you will need to use the Git clone command.  The following is example of clone a public GitHub repository:

$> git clone git@github.com:jmcgarr/java-bdd-examples.git

Checking in Code

Now that you have the code, you can begin making changes.  Once you are ready to check in these changes, there are a few steps you need to take to get the changes into a remote repository:

$> git status -S
$> git add . -A
$> git commit -m "Add a commit message here"
$> git push origin master

Line 1 issues a status command, telling you which files have changed locally.  The add command on line 2 tells Git to stage all changed files so that they can be committed to your local repository.  The commit command on line 3 will commit the changes you added to your local repository.  Line 4 is where you will push the changes in your local repository to the remote repository.

Note: Your fellow team members will not see your changes until you push them to the remote repository.  When you commit, you are only commit the change to your local copy of the repository

Getting the Latest Version

Prior to pushing your changes to a remote repository, you may want to grab the latest code from the repository.  To get the latest remote changes, use the following commands:

$> git fetch origin
$> git merge master

Line 1 will sync your local Git repository with the remote Git repository.  Your working copy of the code will not be updated until you issue a merge command (line 2).  Git provides a way of combining these two commands into one, by using the pull command:

$> git pull origin master

This command will fetch the remote repository changes, and merge them into the master branch.

Learn More

Git is a powerful version control tool and it is extremely useful.  This tutorial should get you started immediately.  But be warned, that you should take the time to learn more about Git and how it works.  Here are some useful resources for learning more about Git:

Share this:


comments powered by Disqus