Pages

6/06/2012

Git Version Control

When I first set out to use git, I had a hard time understanding how to use it. Because of this, it took me quite some time to get it set up. This post is an attempt to simplify the process for others, and hopefully make it easier to understand.

First off, the best resource I have found for git is the book "Pro Git" by Scott Chacon. It can be purchased in print, or read online for free (http://git-scm.com/book). The book does an excellent job of explaining how git works. You only need to read the first 3 or 4 chapters to get started. Chapter 5 is helpful if you want to sync your repository to a server. The rest of the book has good info, but isn't required to get you started.

Here is a short summary of how I got started with git. The first thing you need to know is that git is different from traditional version control systems. For example, with subversion, there is a central repository on the server. When you want to edit a file, you checkout the file, make changes, then check the file back in. With git, you have a local repository that you commit your changes to. If you want, you can push/pull changes to/from a remote repository.

For Ubuntu users, install git like so:
apt-get install git-core

Windows users can install msysGit (http://code.google.com/p/msysgit), or you could try the new Github for Windows.

Assuming your project is already setup (whether it's a C# project, a Java project, a collection of documents, etc...), navigate to the project directory and type:
git init

There you have it - a local git repository. You will see that this created a folder named ".git" inside your project directory. Should you ever want to delete your git repository, you can just delete the .git folder (be careful though!).

Now, add your source files to the version control with "git add":
git add . (to add all files)
or
git add filename (to add an individual file).

Finally, you need to commit your changes:
git commit -m "initial commit"

Now, as you go about making changes to the source code, simply add the changed files with "git add" and do another commit.

Of course, there are many more features. It would be very worthwhile to learn about branching and merging in git as well as how to revert to past commits - all of this is described in the Pro Git book mentioned above.

Enjoy your new life with git!

No comments:

Post a Comment