Blog

08
November 2013

Gavin Pickin

Git for Dummies - Your First Repo

Source Control

If you have been following along, you will have learned that the reasons you were not using Source Control (assuming you are reading this because you're not using it) were not really good reasons, and you've installed Git on Windows, Mac OSX, or Linux, and you're ready to create your first repo.

Git is a distributed system, so you have "local" repos on you and your developers machines, and then (usually) a remote repo.
We're going to start by creating the "local" repo on your machine.

Open up your Git Bash (on Windows) or Terminal (Mac) or Command Line (Linux).
These commands should be universal, because Git Bash uses Linux based commands, and Mac OSX and Linux are *Nix systems of course.

Just to make sure Git is installed, you can type

$ git version

You should see Git version 1.8.4.2 (depending on your install of course). Change directory until you are in your Websites dir. On my machine, I have all my websites in /www

Now, we're going to create a "new" repo from scratch. We will create a new repo for an existing website / project next.
So I'm going to create a new directory called "MyFirstRepo" and cd into that directory

$ mkdir MyFirstRepo
$ cd MyFirstRepo

To create a repo, all we have to do is Initialize Git in the directory, and Git creates a .git folder, and starts watching this directory and subdirectories.

$ git init

And tada, you're Initialized an empty Git repository.

Now, lets create and add a "readme" file, and make our first commit.

To quickly make a "readme" file, we can use a command line shortcut

$ echo "# This is my first repo's readme file" >> README.md

Now we have a readme file.
Is Git tracking our new file? To find out, lets check the status of our repo

$ git status

You can see the output of that command below.

The output shows us we're on the Master Branch, and we have an untracked File README.md. Git is even nice enough to show us how to add the file to the repo so Git will track it.

$ git add README.md
$ git status

Now we have added the file, and we run git status again, and now we see no files are not tracked, and we have a new change, ready for commit.

Once we are happy with our set of changes, we need to commit our changes to our repo. Commits allow you meaningful milestones in your code, with comments, and allow you to change back and forward "versions", which relate to these Commits.

To commit this change, and make a comment with the commit, we run this command... and run a git status after to check the status of our repo

$ git commit -m "This is my first commit to the repo, just a simple README file"
$ git status

EDIT - If this is the first time you have committed to any project on this machine, you might have to set a couple of global variables if Git cannot determine them on its own... your name, and email address... you can follow the commands you see in git, or you can go here for a simple walkthrough to add Global or Local settings for you Name and Email Address in Git.

Now, our changes have been commited, and the git status shows us that our repo is clean, there are no untracked files, no files waiting to be commited.

Great, we have our first local repo built, now we just need to push our changes to our remote Git repo.
To do that, we need to setup an account with Bit Bucket, setup SSH Keys for authentication from the command line, and change the configuration for our repo, so it knows where our remote repo is... so check back for those walk through blog entries soon.

Blog Search