Recently I decided to have a look at using Git for my projects instead of Subversion. Git has many advantages over Subversion, the main one I was interested in is that it is a distributed VCS. This has the benefit that code can be committed and branched in your own local repository and it does not rely on network access. The only downside I can see to this is that you are perhaps less likely to have a backup of your code on a separate machine. Therefore I was interested in setting up Git so that I can still commit my changes to a centralised location for a backup. As a result, this post is intended to serve as a quick introduction to Git and a how-to for setting up a repository for pushing changes to.

Git usage

Set the name and email address which your commits will use with:

git config --global user.name "Your name"
git config --global user.email "Your email"

This gets stored in ~/.gitconfig. Removing the –global flag will apply the values to a specific project if in a git project directory.

To initialise a new git repository, go to the root directory of the project files and use:

git init

To add files to be committed use the following. This applies to both new files and modified files which should be committed, which differs to Subversion in which only new files need to be added and all modified files will be automatically added.

git add file.txt

To remove a file which has been added to the next commit:

git reset HEAD file.txt

To delete a file from the project, which will be removed during the next commit:

git rm file.txt

To diff the files which have not been added to the next commit:

git diff

To diff the files which have been added to the next commit:

git diff --cached

To commit changes to the repository:

git commit

To commit changes to the repository and automatically add any modified (but not new) files:

git commit -a

To view the log of all previous commits:

git log

To get a copy of a Git project:

git clone /path/to/project.git

Setting up a push repository

For this I found this link to be useful. Essentially you connect to the server you wish to push changes to via SSH or some other method, create a directory for the project and initialise it as a bare repository.

git --bare init /path/to/project.git

And then on the system you wish to push from, add a remote repository, in the following case it will be called “origin”:

git remote add origin ssh://myserver.com/path/to/project.git

Then pushes can be performed using:

git push origin master

It is important not to push to a repository with a working copy, as changes may get lost.

Resources

The following resources were used: