Welcome
The Nomad blog is where we share news of our projects, thoughts on web development and a weekly round up of things on the Internet that have caught our eye - Tales from the Internet
Tag Cloud
2011 accessibility adobe shadow agencies amazon apple arduino backups bears blog boo book review browsers business cablegate captcha charity clients code igniter code snippets community conference contracting cooperation csharp css css3 development dojo dyson eeuk expression engine free full frontal gaming git honey pots html5 installation internet internet explorer ipad iphone java javascript jquery linux little printer ludum dare micro frameworks mobile mono monotouch multi-touch mvc news nomad opengl organic osx passwords performance php process pseudo selectors rdf recovery remote repository responsive design review s3 security seo silverstripe spam spime sublime text 2 svn technology tfi tools trust truth underscore url shortening usability version control wii wikileaks windows
Blog » Version control with GIT - Creating a local repository
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
#
# 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.
Comments
No one has commented on this page yet.
RSS feed for comments on this page | RSS feed for all comments