Git 101

April 2, 2014

Here is a quick how-to on how to create a local Git repository and push to a remote repository.

For those that don’t know, Git is a source code management system that was initially developed by Linus Torvalds for the Linux Kernel.

First, open a new console session and check to see that Git is installed on your system.

$ git --version
git version 1.8.5.2.msysgit.0

If Git is not installed on your system, please see http://git-scm.com/book/en/Getting-Started-Installing-Git.

Once you have verified that Git is installed, the next step is to navigate to the root folder of your project and initialize a new repository.

$ git init
Initialized empty Git repository in d:/Development/ArchConnect/.git

Now that we have an empty Git repository we will want to add our file. First, we can check to see if there any files to add.

$ git status

On branch master

Initial commit

Untracked files:
(use "git add <file>..." to include in what will be committed)

ArchConnect.Web/
ArchConnect.sln
ArchConnect.v12.suo
packages/

nothing added to commit but untracked files present (use "git add" to track)

As you can see there are a number of untracked files so go ahead and add them to the repository.

$ git add .

Run the git status command again to see what Git has done.

$ git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file:   ArchConnect.Web/App_Start/RouteConfig.cs
new file:   ArchConnect.Web/ArchConnect.Web.csproj
new file:   ArchConnect.Web/ArchConnect.Web.csproj.user
new file:   ArchConnect.Web/Controllers/HomeController.cs
new file:   ArchConnect.Web/Global.asax
new file:   ArchConnect.Web/Global.asax.cs
new file:   ArchConnect.Web/Properties/AssemblyInfo.cs
new file:   ArchConnect.Web/Scripts/jquery-1.10.2.intellisense.js
new file:   ArchConnect.Web/Scripts/jquery-1.10.2.js
...

Next, commit the files to the local Git repository.

$ git commit -m "Initial Commit"
[master (root-commit) 855a778] Initial Commit

80 files changed, 79239 insertions(+)
create mode 100644 ArchConnect.Web/App_Start/RouteConfig.cs
create mode 100644 ArchConnect.Web/ArchConnect.Web.csproj
create mode 100644 ArchConnect.Web/ArchConnect.Web.csproj.user
create mode 100644 ArchConnect.Web/Controllers/HomeController.cs
create mode 100644 ArchConnect.Web/Global.asax
create mode 100644 ArchConnect.Web/Global.asax.cs
create mode 100644 ArchConnect.Web/Properties/AssemblyInfo.cs
create mode 100644 ArchConnect.Web/Scripts/jquery-1.10.2.intellisense.js
create mode 100644 ArchConnect.Web/Scripts/jquery-1.10.2.js
...

Now that all your code has been committed to your local repository, you will need to push your changes to a remote repository to keep them safe.

Usually your remote Git repository will be hosted on a server. The process of getting Git installed on a server is the same as on your workstation. (Alternatively, you can also create a GitHib account to host your remote Git repositories.)

Before you can push your code to your remote repository, you need to initialize a bare repository on the server.

$ git init ArchConnect.git --bare
Initialized empty Git repository in /home/ckooiker/git/repos/ArchConnect.git/

Back on your workstation add the remote repository.

$ git remote add origin username@myawesomeserver:/home/ckooiker/git/repos/ArchConnect.git

When you have your remote repository setup you can go ahead and push the changes.

$ git push origin master
Counting objects: 87, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (74/74), done.
Writing objects: 100% (87/87), 1.77 MiB | 0 bytes/s, done.
Total 87 (delta 3), reused 0 (delta 0)
To username@myawesomeserver:/home/ckooiker/git/repos/ArchConnect.git
* [new branch]      master -> master

And that’s it, our source code has been committed to remote repository!

For more information on Git and what it can all do checkout http://git-scm.com/book.