From zero to git, episode 0: first things first

Written by . Originally posted on 06 Oct 2011.

So, you want to do version control with git on your files? But let’s admit it: git it’s not all that straight forward for a novice.

Lots of git explanations you can find on the internet start with a fair share of git internal concepts. I think this is not the one-size-fits-all solution, so I started to write this series of articles as an experiment in which you learn some practical command to get started and then you are exposed to the theory. Let’s see how much damage I can do :D.

This article shows you two occasional git commands to get you started. It assumes that:

  1. you already installed git

Once in a while: presenting yourself

First of all let’s present ourselves to git:

Type the following commands, editing the last part to fit your case

$ git config --global user.name "Your Name"
$ git config --global user.email you@example.com

All the previous needs to be done once, especially if you backup your setting files when changing computer, so there’s no real need to remember.

Once on new project: empty repository

The first thing to start working with git is getting some repository. For the purpose of this series of articles we will start from scratch.

Like the previous one, this operation is not really something you will do daily, but it happens more times than setting your name.

Using your terminal, get in the empty directory (create one if you haven’t one yet) that will hold your repository. Type:

$ git init
Initialized empty Git repository in <directory>/.git/

<directory> is the absolute path of your git repository. At that path a hidden .git directory is added. This is where the actual repository is stored, the files you will actually work with are outside the .git dir and are just your current working copy.

The .git directory is very important, you don’t want to mess with it without knowing what you are doing.

Bonus: two nice settings

I highly recommend this one, as it will make easier to interpret the command line output at a glance:

$ git config --global color.ui true

A second nice tip (specific for bash) is slightly more difficult to add, but is well worth it. Is used to show information about the state of your files to the bash prompt, you will appreciate this in the episode of this series about branches (branching, merging and tagging).

Open the ~/.bashrc file and add the lines below (appending them at the end should be fine).

#Checks if the current git branch is in a dirty state (uncommitted work)
function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s$(parse_git_dirty))")\$ '

If you don’t have ~/.bashrc and are on a unix operating system you may have to find the right place for this yourself (it’s probably ~/.bash_profile).

Done :D. Don’t miss the daily bare minimum git commands in the next article.

blog comments powered by Disqus