CARVIEW |
Article

About the Author
Alec Clews
Alec Clews is principal consultant at Voga Consulting Services where he specializes in SCM and Application Life-cycle Management. You can connect to him on LinkedIn, have a conversation with him on Twitter or identi.ca, and find his code at GitHub.
Git: Your New Best Friend
Introduction
This article introduces version control and Git without assuming you have any prior knowledge or programming experience. Because of its introductory nature, certain details are simplified or omitted and the use of the Git Graphical User Interface (Git GUI) is emphasized. Afterwards the reader should be able to use Git for basic version control and know where to locate further information.
Version control is the process of recording the history of changes to files as they are modified. Users can go back in time and get old versions and identify where changes were introduced (people sometimes refer to version control tools as time machines). This means that it’s easier to:
- protect against changes—accidental or otherwise—and be able to access a known good version of a file
- track down problems and make fixes to previous versions of files
- allow more than one person to modify project files simultaneously (programmers refer to this as parallel development)
- retrieve an older set of files (if requested by a customer or manager, for example)
The first version control (VC) tool, SCCS, was written in 1972 and since that time there have been major advances in the way VC tools are used. Git represents the current state of the art in that it is distributed. A distributed VC tool gives each user a complete history of all changes to the files on which they work. So, for instance, it’s possible to look at previous versions of files while flying across the Pacific (depending on your laptop battery too, of course). Later, when you touch down, all changes can be merged together over the computer network for release or further work. This may sound complicated, but the tool makes it relatively easy and error free.
Other well-known VC tools—for example Subversion—are classed as centralized and provide only a single place, the repository, into which users store their changes on a regular basis. In this model users work without a personal copy of the change history; they only have the set of the files they’re currently working with.
Git runs on Windows, Mac OS X, Linux and UNIX. It was developed by Linus Torvalds, the lead developer of Linux, in 2005 to answer the needs of the development community. As well as a powerful tool for the individual developer, it provides a powerful model for cooperation. Community source code sharing sites like GitHub and Gitorious make full use of its capabilities.
Concepts
Git maintains a database of all previous file versions in a repository (repo in geek speak) and it is usually located in a directory called .git. In UNIX-style systems (like Mac OS X and Linux) names that begin with a . are hidden by default so the repo is usually invisible during normal work (except on Windows). As well as the contents of the files and directory versions the repo contains additional housekeeping information about current work, other remote repositories we’re sharing with, current settings, and so on. In Git we have a separate repo for each of our projects.
In addition to the repo, each project will have a set of files that we’re working on and editing. As we complete changes to our working files we add the changes to the repo; this is called committing changes. In actual fact we’re not fully committed to our changes as we can always retrieve old versions and make changes to the changes. We can also access a summary of our work against previous versions to see what we’ve done so far. The set of files we’re currently working on is called the working copy. We can easily change the working copy to a different version whenever we want.
Installing Git
Before progressing any further let’s install Git and set up a small test repo to work with.
If you use Linux then installing should be a simple matter of using your package manager to install git (and possibly Git GUI if it is a separate package). For Mac OS X either use the Git OSX Installer or you can use Mac Ports. Windows users who use Cygwin can use the Git from Cygwin; just use the Cygwin package manger to install the appropriate packages. Otherwise Windows users should use the the native Windows Git package: msysgit (you’ll want to download the file labelled “Full installer if you want to use official Git” and avoid the file labelled “Full installer (self-contained) if you want to hack on Git” and accept all the installation defaults).
Starting to Use Git
The quickest way to get started is to use the Git GUI client. If you use Windows you’ll find Git GUI on your Start menu, otherwise you can type the command git gui
at the command prompt. You’ll see a screen similar to this:
Git GUI startup screen (View larger image in a new window.)
Select Create New Repository and enter the name of a new directory. Git will create the directory and initialize an empty repo. Alternatively, create a project directory, and then within the new directory enter the following from the command prompt:
$ git init
If you’re on Mac OS X or Linux you can just use the standard terminal. If you’re on Windows and you’ve installed the msysGit package, then you’ll find the Git Bash item on your Start menu. This will launch a special command prompt for using Git. Exclude the $
, that represents the command prompt, but enter in everything after it.
You should then see a message like the following:
Initialized empty Git repository in /path/to/your/directory/.git/
If you look in the project directory you should see a directory named .git
(on Mac OS X or Linux you will need to use the command ls -A
to show the hidden .git
directory) In your newly created project directory make some new text files using your favorite editor. You now have some changes that are not under version control, in Git these are called unstaged. In Git GUI they look should like this:
Unstaged Changes (View larger image in a new window.)
You can retrieve the same information from the command line with the command git status
.
git status (View larger image in a new window.)
Now for a little diversion before we add our files to the repository: the Git index.
If you liked this article, share the love:
Learn more with easy-to-understand SitePoint books
Sponsored Links
Popular Articles
Most Popular This Month
Guru Lists
So You Want To Be A Ruby Guru?
Try this list, compiled by vgarcia
Topics
Before You Code
Design and Layout
- Accessibility
- Design Principles
- Design Practice
- Design Tips & Tricks
- Flash Tutorials
- Software Tutorials
- Usability and Information Architecture
Client-side Coding
- Adobe AIR Tutorials
- HTML & XHTML Tutorials
- CSS Tutorials
- JavaScript & Ajax Tutorials
- XML, XSLT & Web Services
- Flex Tutorials
Server-side Coding
- Apache & IIS Configuration
- ASP & .NET Tutorials
- CGI & Perl Tutorials
- ColdFusion Tutorials
- Java and J2EE
- PHP & MySQL News & Interviews
- PHP & MySQL Tutorials
- PHP & MySQL Reviews and Apps
- Ruby & Rails
- Server Side Essentials
Site Strategy
Site Marketing
Sell Your Services
SitePoint Jobs
» We're Hiring!
Follow SitePoint on...
The contents of this webpage are copyright © 1998-2009 SitePoint Pty. Ltd. All Rights Reserved.