GIT distributed version control system

1. GIT overview

Git is a distributed version control system. Unlike Subversion, this means whole copies of the repository can exist in multiple locations. In practice there is usually the notion of a central repository.

2. GitHub

A lot of open source projects host their repository on GitHub.

3. Remote repositories

Remote repositories are git repositories that you can push changes to, or pull changes from. It is possible to have multiple remote repositories.

When you initially clone a repo to create your local repo, the local repo will have a remote repository named 'origin' that will point to the original repo.

To list all the remote repos, run "git remote".

$ git remote origin

To find out the URL of the 'origin' repo, run

$ git config remote.origin.url

It will print out the URL.

4. Setting your username and email

This can be configured globally or per repository.

To change it for the current repo, set the config values and

$ git config
$ git config "Joe Bloggs"

To change it for your user, use the --global option.

$ git config --global

5. Migrating from Subversion

See migrating to git.

6. Ignoring files

Add a .gitignore which contains a list of file patterns that git should ignore. See the gitignore documentation for detailed info.

7. SSH keys

gitolite, github, Atlassian Stash all support SSH keys. If you don't already have a keypair, create one using ssh-keygen, then you can use it for both authentication and identification.

8. Branching

Branching is different in git to subversion. You can have local only branches, and this allows for different workflows.

Branch Creation

Local branch creation

Switch to your source branch, then checkout and create your new branch. In the following example 'feature/foo' is the original branch, and 'feature/foofix' is the new branch.

git checkout feature/foo
git checkout -b feature/foofix
fatal: Failed to lock ref for update: Not a directory
error: unable to resolve reference refs/heads/feature/foo/fix: Not a directory
          fatal: Failed to lock ref for update: Not a directory

This happened when trying to create a branch named 'feature/foo/fix'. This is not allowed if a basename or parent branch called 'feature/foo' already exists.

Remote branch creation

On the remote, if you are using stash, you can do this via the UI.

After it has been created, check it out to your local. In the following example it is assumed your branch is named 'quickfix'.

git checkout -b quickfix origin/quickfix

To create a remote branch from an existing local branch named "feature/quickfix", you need to push it.

git push origin feature/quickfix

Reset branch

Sometimes you do not want to push your changes to the remote branch. You can reset your local branch with the following command:

git checkout quickfix
git reset --hard origin/quickfix

9. Merging

Switch to the target branch, then merge commits from the source branch.

git checkout target
git merge source

"source" and "target" are branch names

10. Pushing

There is a difference between author and committer. This can happen when you are merging commits authored by people on different branches. There may be special hooks added to the remote git repo that checks your pushes and may reject them. If you are using IntelliJ, use the command line instead to see the error message. e.g. Stash has hooks that checks that the name and email is valid within the organisation.