Git is really not that hard to learn, yet it is a powerful tool to manage version control of your work. I learned the basics recently studying the Ruby on Rails tutorial. From there Pro Git is an interesting follow-up. This post shows some basic Git to get started.

New here? You might want to subscribe to my blog by email or RSS.

featured image

According to the Pro Git book, Git was designed to meet the following goals:

  • It should be fast;
  • It should have a simple design;
  • It should have strong support for non-linear development (thousands of parallel branches);
  • It should be fully distributed;
  • Git should be able to handle large projects like the Linux kernel efficiently (speed and data size).

These are indeed advantages to use Git over other version control systems. It is very fast, mainly because it works against local repositories (vs. remote syncing for every operation).

Initial config

After installing Git there are some initial configs you should make:

  • Set your name: $ git config --global user.name "Fname Lname"
  • Set your email: $ git config --global user.email [email protected]
  • To make optional aliases: $ git config --global alias.co checkout
  • Choose your favorite text editor, mate in my case: $ git config --global core.editor "mate -w"
  • Pick a diff tool: $ git config --global merge.tool vimdiff
  • Color Git console outputs: $ git config --global color.branch auto ; git config --global color.diff auto ; git config --global color.status auto

The basics - and maybe all you need to know for now (?)

It requires only a few basic concepts to successfully work with Git (the following commands should be executed in the working directory) :

  • Put your project under git control: $ git init ; this creates a .git directory in your project folder.
  • Add files to be tracked (staged files): $ git add
  • If you use $ git add . , the . (dot) adds all files at once.

    If you modify a file after you run git add, you have to run git add again to stage the latest version of the file. You can exclude files by filling in the .gitignore file in the working directory (for example to exlude logfiles in a Ruby on Rails project fill it with: .bundle, db/*.sqlite3, log/*.log, tmp/**/*)

  • Commit changes. Git takes a snapshot now: $ git commit -m "initial commit"
  • Clone a (remote) git project: $ git clone git://github.com/schacon/grit.git mygrit (where mygrit is the local directory you want to copy the project to) - $ git pull/ push / fetch / clone has much more to it, check out the Pro Git book for more info.
  • Get info about files, commits, and changes: $ git status / $ git diff / $ git log (gitk for graphical log viewing). The commands have a lot of switches, Pro Git shows a useful example for filtering in large projects: $ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" --before="2008-11-01" --no-merges -- t/ -> pretty powerful!

The real power: Git Branches

Branches allow to diverge from the main line of development. This allows developers to work on bug fixes and new features without interfering with the main (live) branch (master). Later you can merge branches with each other or with the master branch. The basics are quite simple:

  • Open up a new branch: $ git checkout -b testing ; $ git branch shows you all branches in the project, the single star before one of the branches is the active branch, in this case "testing" became the active branch with the "checkout" switch.
  • Commit changes like normal: $ git commit -a -m "commit message of thing(s) done in branch". Again, if you have added any new files to the project since the last commit, you still have to run "git add" first.
  • When done with your branch, switch back to the master with: $ git checkout master
  • If happy with the results, merge the branch into the main one (master): $ git merge testing
  • If the branch is not needed anymore, delete it: $ git branch -d testing

This is the easiest example of branching, there are more advanced options possible like 3-way merging (merging various branches at once) and rebasing, again see Pro Git for more info.

And this concludes the basics of Git, which matches the first 4 chapters of Pro Git and the end of Chapter 1 of the Ruby on Rails tutorial. I think these notes will cover me 95% in the following web projects I develop (alone). With multiple developers I might need to write a follow-up post with more advanced concepts !

Set up your GitHub account

One thing is missing from this basic Git tutorial, setting up GitHub! You typically want to push your code to GitHub to have a backup and to allow for collaborations with other developers. Check the "SSH keys" part when creating your account. After creating a new repository in the GitHub GUI, pushing your code is easy:

  • The following command configures the push location of your local working directory: $ git remote add origin [email protected]:username/first_app.git
  • $ git push origin master ; pushes the code to the remote location. Again you need to have the SSH key working to do this.
  • From here on code changes sync to the remote server each time you run $ git push

Heroku cloud deployment

The Ruby on Rails tutorial introduced me to Heroku as well. I copy the steps here to push your code to Heroku:

  • Install the corresponding Ruby Gem: $ [sudo] gem install heroku
  • Sign up for Heroku and configure your SSH keys, then run $ heroku keys:add
  • To create a subdomain on the Heroku servers, use: $ heroku create --stack cedar
  • Heroku return something like "Created http://severe-fire-61.heroku.com/ | [email protected]:severe-fire-61.git ... Git remote heroku added" and that is it!

You can also use Heroku also when developing Facebook Apps! See this nice tutorial.

For more info, check out the Git site's documentation as well


Bob Belderbos

Software Developer, Pythonista, Data Geek, Student of Life. About me