Git by example (a little tutorial)


Often I find well done tutotials on Git, some of more valuables (IMHO) are those which use a “by example” approach.

I believe offering a little tutorial example-oriented could help. Please, try it out on your keyboard the examples given here you can find the bash file). A little disclaimer: only Git basic commands and concepts are present (no networking or collaborating with Git are present).

# create a new application with Rails
rails silly_blog

# put it under version control with Git
cd silly_blog
git init
git add .
git commit -a -m "first commit"

# we'are in "master" branch
git branch

# do some modifications (eg: add a new model)
ruby script/generate model Post title:string body:text
git add .
git commit -a -m "added post model"

# the log reflects all the commit(ed) modifictions
git log

Let’s add some “experimental” feature:

# create a new branch..
git branch experimental
# ..and entering in it
git checkout experimental
# add a new model
ruby script/generate model Rating value:integer post:references
git add .
git commit -a -m "added rating model"
# go back to "master" branch
git checkout master

Now we are happy with new modifications, and so we’d like to import them in “master” branch (now the Rating model is present in “master” too):

git merge experimental

Now, without do a commit let’s modify the code in “experimental” branch:

git checkout experimental

# modify the Rating model
cat "class Rating < ActiveRecord::Base; belongs_to :post; end" > app/models/rating.rb
git checkout master
git status

Notice how the modification tainted the “master” branch too:

# go back in "experimental" branch and do a commit
git checkout experimental
git commit -a -m "now ratings belong to posts"
git checkout master
git status
grep "belongs_to" app/models/rating.rb
# modifications stay in "experimental" branch, to import them:
git merge experimental

Now let’s add a new file (TODO) in “master” branch:

echo "#List of next actions to accomplish" > TODO
git add .
git commit -a -m "added an empty list of next actions"
git checkout experimental
ls -l TODO

Notice that here (“experimental”) the file TODO doesn’t exists:

# import the most recent modifications
git pull . master

Now let’s intentionally generate a conflict and see how to resolve it:

# add an action (in "experimental")
echo "* write unit tests" >> TODO
git commit -a -m "added some action"

# add an action (in "master")
git checkout master
echo "* buy milk" >> TODO
git commit -a -m "remember the milk"

# now the two versions of TODO are in conflict
git checkout experimental
git pull . master
# when merging a conflict happens!

# modify/rewrite the TODO file
echo "# List of next actions to accomplish"  > TODO
echo ""                                     >> TODO
echo "* write unit test"                    >> TODO
echo "* buy milk"                           >> TODO
git commit -a -m "resolved conflicts on next things to accomplish"

git checkout master
git merge experimental
# everything aligned

That’s all folks! Let’s clean up:

cd ..
rm -fr silly_blog


blog comments powered by Disqus