init

Setting up a repository

This example shows how to set up a local Git repository and then how to add an existing repository to GitHub.

Use the git init command to create a new repository. If you navigate to the existing project directory and execute the command it transforms the directory into a new Git repository. A new directory called .git is created.

To create a new empty repository use the command

git init <directory>

This will create a new directory named <directory> that contains the .git subdirectory.

Bare directory

Use the command

git init --bare <directory>

This will create a repository that doesn't have a working directory. This means files cannot be changed and commited. Central repositories should always be created as bare.

Configure Git

Next we need to set up the username and email address for the repository, this information will be included for all commits that are made.

Rich@RICH-PC ~/workspace/perl (master)

$ git config --global user.name "Richard Weeks"

Rich@RICH-PC ~/workspace/perl (master)

$ git config --global user.email "rich@controlcode.co.uk"

To verify the information use:

Rich@RICH-PC ~/workspace/perl (master)

$ git config --global user.name

Richard Weeks

The --global flag writes this setting into your global git config. If you remove that flag you can override the setting for your current repository. Use the following command to show all configuration or look in the ~/.gitconfig file or C:\Users\Rich\.gitconfig on Windows.

git config --list

The next step is to push our new repo to our remote which in this example we'll be using GitHub.