Skip to content

17 Nov 2011 by Lee

Version control with GIT - Creating a local repository

Creating your first Git repository

This one is going to be short and sweet. We're going to create our first local Git repository (repo).

Fire up your Terminal and go to the directory you wish to use as your repo. In this case I've create a directory called 'my_project'.

cd ~/Sites/my_project

Now we're located in the directory we wish to use as our repo, lets go ahead and create it:

git init

And that's it, you should have recieved the following:

Initialized empty Git repository in /Users/macbook-pro/Sites/my_project/.git/

So, we now have our new empty repo. So lets add a few files to it. Firstly, create some files:

touch index.html
mkdir css
touch css/screen.css
mkdir js

We've created an index.html at the root of our repository, and a new directory for our stylesheets, with a single file in there called screen.css, we've also created an empty directory for any future javascript we may need to add.

Lets go ahead and add all of that to our repository:

git add *

We're using a wildcard (*) to add everything to the repo here, but you could have quite easily added each file individually. Lets run a status to see what we've got:

git status

Should return:

# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: css/screen.css
# new file: index.html
#

We can see what files are going to be committed. At this point we refer to the files as being 'staged', in that they'll be committed at our next commit. In Git, we stage, and then commit, as opposed to SVN's straight commit.

Also notice that there's no mention of our 'js' directory in the commit. This is because Git tracks files, and not directories. In this instance, if you want to include the empty directory into the commit, you could do this little trick:

touch js/.gitignore

This will create a Git Ignore file, informing Git of which files to ignore, and more importantly NOT ignore in this case. Open, up the .gitignore file with your favourite text editor, and enter the following:

!.gitignore

We're telling Git to NOT ignore it's own 'ignore' file in a roundabout way. So, now run another status check:

git status

# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: css/screen.css
# new file: index.html
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# js/

And you'll see the empty directory has now been added to the repository (strictly speaking, it hasn't, but the ignore file has).

Before we commit we need to stage the 'js' directory and its contents:

git add js

Now all our files are staged, we can go ahead and commit:

git commit -m "Initial commit"

If you've used SVN before, you'll see how familiar this is. We use the '-m' flag to tell Git we'll be adding a message to the commit which will show in the repositories history log. Running this command should show the following:

[master (root-commit) 89a7059] Initial commit
1 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 css/screen.css
create mode 100644 index.html
create mode 100644 js/.gitignore

And there you have it, your first commit. If you run a status check again you can confirm everything has been added successfully:

git status

# On branch master nothing to commit (working directory clean)

Next instalment will explain how to set up a remote repository and link our local to our remote.

Lee Powell

lee

I have over five years development experience, predominantly with client side technology but also more recently with server side languages such as PHP and Python. I've run teams and performed client facing roles as well as being a programmer. I am passionate about accessibility, standards and all things web.

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments

Leave a comment