Install and Init
First you need to install git. Instructions for mac users are here If you are using Ubuntu or any form of Debian Linux then you just need to type the following into a terminal prompt:
~>sudo aptitude install git-core
Once you've installed Git, navigate to your project directory, which in this example is called git-example-project:
~>git-example-project$ git init
This command sets git up for that folder. It will now track all the changes you make.
Adding and Committing
Now make a couple of changes to your project - change a few lines of code.
~>git-example-project$ git add .
This will add all the updated files, ready to be committed.
~>git-example-project$ git commit -m "Make notes here about your changes"
This will commit the changes to the repository.
The whole 2-stage process of adding then committing can get a bit annoying, but it allows you to add multiple changes before you actually make a commit. If you want to bypass this you can use the -a flag like so:
~>git-example-project$ git commit -a -m "some message here"
This will add and commit any changes all in one go.
Note - if you delete a file. You need to remove it from your git repository with this command.
~>git-example-project$ git rm filename.htm
Branching
For me, this is the killer feature of Git. You start with a default branch called 'master'. To create a development branch just type:
~>git-example-project$ git branch development
To switch to this branch you need to type:
~>git-example-project$ git checkout development
Now if you make any changes, they will only affect the development branch, not the master branch.
Try changing a couple of lines, add them, then commit them. Now change to the master branch:
~>git-example-project$ git checkout master
You'll see the changes are not there! Go back to the development branch:
~>git-example-project$ git checkout development
And your changes are back!
The obvious use of branching is to separate the master and development branch of code, but because it is so quick and easy, you can use it to test out new features. Say I want to try using some rgba colours in my development branch. First I'd make sure I was on the development branch by typing:
~>git-example-project$ git branch
This will list all the branches and have a * next to the current branch. If it is not development, just checkout:
~>git-example-project$ git checkout development
Now create a new branch from here:
~>git-example-project$ git branch rgba
Change over to that branch:
~>git-example-project$ git checkout rgba
Now make some changes using rgba colours. They will only affect this new branch. If you like these changes and want them in your development branch then you use merge.
~>git-example-project$ git checkout development
~>git-example-project$ git merge rgba
Now the changes you made in the rgba file will be in the development file. You don't need the rgba branch now because it was only used for testing, so you can delete it using the -d flag:
~>git-example-project$ git branch -d rgba
When your development branch has been tested and is ready for the big time then you would just merge them to the master branch. Remember to first checkout the master branch, then merge in the development branch. Some people like to have a staging and live branch to manage the different stages of a site's development.
Tags
You can tag a project at any time using the following command.
~>git-example-project$ git tag v1.2 "Version 1.2 of my secret project"
This lets you create snapshots of certain points in development - make sure you have added and committed any changes before tagging. If you want to see all of your tags, just type:
~>git-example-project$ git tag
If you want to revert to a previous version then you create a new branch for it. Say you want to go back to version 1 and it was tagged as v1, you would type:
~>git-example-project$ git checkout v1 -b version1
This will automatically create a new branch called version1 that will be an exact copy of the project when it was tagged at version 1.
Remote Repositories
You can keep a remote repository on a hosting service like github.
First you need to sign up for a free account. Set up a project on github and it will give you all the instructions about how to add a remote repository. This allows you to type:
~>git-example-project$ git push
And this will push all your changes to the gitbhub repository. This is useful for backup in the cloud and also sharing open source projects. For client work you would need a paid for github account.
You can also start using other people's code from github by cloning their repositories and using the pull command to pull in any changes they make. They also have the option to pull in any of your changes. This makes git really powerful as a way of collaborating on projects, but I haven't had much experience of this.