git is a version control system. It is important to remember throughout
this document that it is intended to keep a history of changes and a full
history of versions of your code, or other files. The fact that using git
provides backups at each stage should always be considered a convenient
side effect, not a deliberate feature.
If you have a repository online which you wish to download with git and use, you can execute the clone command:
$ git clone email@example.com:meridiangrp/brand.git brand
It is worth spending a moment to break this command down:
git clonepart runs the
gitprogram and tells it that you wish to
firstname.lastname@example.org:meridiangrp/brand.gitpart is a URI which specifies where the repository is located. It can either be an SSH type location, which will usually start
email@example.com, or an HTTPS type, which starts with
https://. SSH type is preferred, but requires an SSH key to be set up with both the machine accessing the repository and the git server.
brandpart specifies where to clone the repository to. It must be either an empty or non-existant folder.
.can be used to clone to the current directory.
This will download the repository ready for use.
Making a new repository
To make a new repository go into a folder with which you wish to start using git and run the command:
$ git init
Then add the remote (the git server) with the command:
$ git remote add origin firstname.lastname@example.org:meridiangrp/brand.git
The URI to use should be given to you by your git host.
Syncronising changes with the remote
You might not always be the only person making changes to your repository. To see if any changes have been made, run:
$ git fetch
If running this shows nothing, no changes have been made. Otherwise, it should suggest how many changes. If you wish to actually then download the changes to your copy of the repository, run:
$ git pull
If you have made changes to the repository that need to be uploaded to the remote, run:
$ git push
Note that if this is your first push, you may need to explicitly set the master branch on the 'origin' remote:
$ git push -u origin master
When making changes to files in a git repository, they start off unstaged. These will not be uploaded. When you have completed adding a feature to your repository, ie. something with making a version for, you can add the files you have changed, or remove them if you have deleted them:
$ git add file/path $ git rm file/path
Once you have staged all the changes with the above commands, you can commit your changes to the repository. Note that at this stage they are still saved locally on your computer:
$ git commit
You will be prompted to add a message explaining what your commit contains. You can then push.
How often should I make commits?
Do make commits for:
- A new feature that is complete and working
- A new asset or collection of assets
- A change or improvment to code
Do not make commits for:
- An incomplete feature
- An untested portion of code
- A collection of files which are completley unrelated
Moving on to more complex situations
Git is a powerful tool, and this only covers the very basics. It is worth spending some time to learn what git can do. It might be worth finding out about:
- Branching and Merging
- Amending commits
- Resolving merge conflicts
- Rolling back changes that have broken something