Skip to content

GIT distributed version control system

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.

  • GitHub

  • BitBucket Cloud from Atlassian is another git repository hosting solution.

Stash has been renamed to Bitbucket Server, it is a self-hosted option.

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

When a repo is initially cloned, a remote named origin is tracked that will point to the original repo.

To list all the remote repositories, run

Terminal window
git remote

To see all the URLs of the remote repositories, run

Terminal window
git remote -v

To find out the URL of the origin repo, run

Terminal window
git config remote.origin.url

It will print out the URL.

To change the remote URL for the remote named origin:

Terminal window
git remote set-url origin git@example.com:magicmonster/example.git

This can be configured globally or per repository

To change it for the current repo, set the config values user.name and user.email

Terminal window
git config user.email email@example.com
git config user.name "Joe Bloggs"

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

Terminal window
git config --global user.email email@example.com

See migrating to git

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

gitolite, github, and bitbucket 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.

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

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.

Terminal window
git checkout feature/foo
git checkout -b feature/foofix
fatal: Failed to lock ref for update: Not a directory
Section titled “fatal: Failed to lock ref for update: Not a directory”
Terminal window
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.

If you are using a hosted solution, 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’.

Terminal window
git checkout -b quickfix origin/quickfix

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

Terminal window
git push origin feature/quickfix

The remote origin branch may have been removed, but your local branch still refers to it. This could happen when a pull request has been merged.

Terminal window
git remote prune origin

This will prune references to invalid remote origin branches.

If a branch has been merged, it makes sense to delete it. I do this for feature branches that have been merged into master.

First list the branches that have been merged

Terminal window
git branch --merged
Terminal window
git branch -d foo

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

Terminal window
git checkout quickfix
git reset --hard origin/quickfix

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

Terminal window
git checkout target
git merge source

where source and target are branch names.

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. Bitbucket can have hooks that checks that the name and email is valid within the organisation.

If you are interested in an export of only the source code, use the git archive subcommand.

To access a remote repo, use the remote option.

Terminal window
git archive --remote git@example.com:example/example.git --output example.tar master