Friday, November 1, 2013

Learning (with) Git

These weeks I'm in a training and  I knew we were going to do a lot of coding exercises and I wanted to be able to share the learning experience with my colleagues when I will be back in Barcelona, so I decided to kill two birds with one stone: I would learn Git.

Git has been around for a while and I had been using scarcely in the past, even I had some code published in GitHub, but when you don't practice, you forget. So I've started small.

I'm working in Windows but I use cygwin to get some Unix-like experience. I installed git in my computer and when I set up my code and initialized a local repository:
cd whereMyCodeIs
git init

A .git folder was created and all my versions would be stored there. I'm not planning on publishing the exercises to any external repository (yet). I created a .gitignore file to exclude from my repository my compilation files. (I struggled without .gitignore in my first repository. Lesson learnt).

Whenever I finished an exercise I would add the code and commit it.
git add .
git commit -m "Exercise 2. Solving the puzzle"

When I want to see which exercises (and commits) I've done so far I do:
git log --oneline

The first think you see the ID of the commit. With it you can retrieve that version, without the risk of losing anything:
git ckeckout 2e81eab

If you want to recover what you were working on:
git checkout master

This way I can review what was studied and modified in every exercise.
If I want to get a list of what files were modified between two exercides I get the commit IDs of the two and I do:
git --no-pager log --name-only --pretty='format:' --full-index 2e81eab..15cf40b

If I want how a particular file was modified between two changes I do:
git --no-pager diff 2e81eab..15cf40b pathToFile/theFileName.java

I know these are very basic commands, but they are helping me in my learning process. I've been following the Atlassian Tutorial.

Even better, read The Junior Developer’s Quick Start Guide to Git.