Git

Git is an excellent version management software package designed to handle synchronising, merging, versioning and collaboration.

git This image is from Oliver Steele's site.

It takes a little bit of effort to learn but is very easy to maintain. Here are some tips on setting it up:

Setting up your local repository

Install

linux:

apt-get install git

or for OS X:

port install git-core

Set up local editor

I like to use Sublime Edit for my git editor, this can be done by these commands:

sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /bin/subl
git config --global core.editor "subl -w"

You may also want to install a graphical merge/diff tool. Meld is a good example of such a program on OS X and Linux.

To configure git to use Meld, first install it with Macports, then execute:

git config --global merge.tool meld

see here if you have any difficulties:

Set up local user

git config --global user.name "Your Name"
git config --global user.email "your@email"

If you forget to do these steps, they can be applied to a repository later with the following command:

git commit --amend --reset-author

Make local repository

cd my_project
git init
git add .
git commit -m "My initial commit message"

Set up remote repository

log in by SSH to the server

git config --global user.name "Your Name"
git config --global user.email "your@email"
cd my_project
git init --bare

Add the remote site

These commands are executed on the local machine

git remote add origin git@example.com:my_project.git

You can add as many remote repositories to sync to as you like, for instance:

git remote add origin2 user@example.com:/home/user/my_project.git

Push your project to the server

git push -u origin master

if you already had a repository there, you may need to force the push but be careful with this option, it will overwrite all file changes to the server

git push -u origin +master

You won't see your files in the remote repository, because this is a bare repository, only used for syncing changes. If you want a remote copy, just clone out of this directory

On remote:

git clone my_project.git

Basic Operations

Git is powerful but you only need a few commands for basic work

Commit and Push

After you've made some changes to your project files locally, run the following to push to your remote server (replace remote-server with the name you configured in git remote):

git commit -a
git push remote-server master

If you have created new files locally, you will need to tell git to track them before running commits or pushes:

git add ./newfile.txt

if you want to make sure, that all files that have been added or deleted are synced, use:

git add -A

but see here for more details

Basic Pull

git pull origin master

Pull ignoring local changes

Sometimes you may want to ignore local changes when you pull from the repository, this can be done by:

git fetch origin
git reset --hard origin/master

see here for more details:

Tweaks

Excluding Files

sometimes you'll want some files not to be tracked by git. A good example of this is the .DS_Store files that OS X creates as it updates each folder. These can vary between repositories and get in the way of git operations.

Here's an example of how you would prevent these from being tracked:

Remove existing files from the repository:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

Add the line

.DS_Store

to the file .gitignore, which can be found at the top level of your repository (or created if it isn't there already). Then

git add .gitignore
git commit -m '.DS_Store banished!'

Disabling Delta Compression

git reduces the size of network transfers by using delta compression, this can be seen when doing a transfer

Compressing objects: 25% (35456/138639)...

This can take a long time and use considerable system resources, so if bandwidth is fast and cheap and/or your files are relatively incompressible, you can disable delta compression. This can be done by setting the following option (for example to disable compression for objects larger than 1MB)

git config --add core.bigFileThreshold 1

Disabling Auto-packing

Sometimes with large repositories, the autopacking can take a lot of time, it can be disabled per repository with:

git config --unset gc.auto

References

  • http://www.gitguys.com/topics/shared-repositories-should-be-bare-repositories/
  • http://gitready.com/
  • http://osteele.com/posts/2008/05/my-git-workflow
  • http://git-scm.com/book/en/Git-Basics-Working-with-Remotes