Getting started with git
Up until a few months ago, I never really needed to bother myself with a Version Control System (VCS). For freelance work, things were simple enough to the point where I’d 1) Develop a website locally and then 2) Upload code to the server, and that was pretty much the end of it. At the office our CMS took care of most things, and for things it didn’t cover, we’d adopted a system of making a backup of the last file and going from there.
A few things happened that changed my take on the necessities of version control.
- At work we upgraded our CMS, which didn’t preserve our versions when it launched, and it was hard to track exactly what changed when we’d make backups.
- Freelance clients started asking for updates and changes to their sites, and I didn’t want a bunch of backup files cluttering my directories. I also want to be able to start tracking what changes were made, rather than tracking simply that changes had been made.
So in the office we starting using CVS, and at home I decided to try Git. Because Git seems to have a steeper leaning curve for those not used to working with a command line, I want to document what I’ve learned with it. So if you’re in need of a VCS and you’re interested in Git, perhaps this will help. At the very least, it’ll serve as a reminder to me! Note: you may want to familiarize yourself with some of the basics of working with the command line or terminal before proceeding.
Getting Started
I used this great post over at Net.Tutsplus.com as a guide, and it helped me tremendously. After installing and configuring git, here’s how you set up one of your existing directories as a Git repository.
In my case, I have a version of WordPress installed locally that I use for development. I want to have each theme have it’s own repository.
The first thing you need to do is navigate to the directory that you want to set up as a repository. For me, that would be the theme’s directory in my local instal.
cd /wordpress/wp-content/themes/myTheme
Now that we’re in the directory that we want to track versions in, we need to initialize Git by typing:
git init
Once completed, you should see the following:
Initialized empty Git repository in /wordpress/wp-content/themes/myTheme/.git/
Okay, so now that we have the repository created, we need to actually commit some code to it. Do that by typing the following:
git add .
This will add all the files in your directory to the Git staging area. The “.” in the command means “get everything,” but you can specify what you want to add to the staging area by typing out the name of the file you’re interested in like so:
git add header.php
Now that we have all of our files in the staging area, we want to commit them to the repository. Remember that in this example we already have an entire code base that we want to add to the repository and that we want to modify later. Since this is our initial commit, we’ll want to indicate that in our comments. To commit with a comment on all files, type the following:
git commit -m "initial commit"
You should then see something like:
[master (root-commit) 466ce7c] initial commit 56 files changed, 5602 insertions(+), 0 deletions(-)
Which is followed by a list of all the files that were committed. At any time if you wonder what the status is of files in your repository, you can ask for the status:
git status
# On branch master nothing to commit (working directory clean)
Now that you have a committed repository, you can begin working on your code. Once you modify a file and save it, run the status command once again and you should see something like this:
# On branch master # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: top.php #
This means that Git is aware that you’ve changed something in your file. At whatever point you’re happy with the changes you’ve made to your code, you’ll need to add it to the staging area, and then commit it as illustrated above. There’s a simple way to do both of those things at once with this command:
git commit -am 'your message here'
The “-am” means that you’re both adding it and want to type out a message. This way you don’t have to run two separate commands to commit a change.
Well, we’ve now used most of the git commands that I’m so far familiar with. Obviously there are many more things you can do with Git, but at this point we’ve got our directory, we’ve added and then committed code, and we’re on our way to tracking our code versions!
As I become more familiar with Git, I’m sure I’ll write some more about it. Hope you enjoy Git!