On a daily basis probably each and every software developer spends a lot of time collaborating and communicating with other developers. There are several tools that simplify this communication (or at least should). One of those tools is a Version Control System. Not so long ago, a new tool appeared on the horizon, called Git. It quickly became a very popular tool in the open source community. Though the ideas behind Git are very basic, becoming a master is not easy! It takes some time, especially for someone who has had the "pleasure" of working with other systems like SVN or CVS previously. If you are starting with Git or switching from another tool, then this post is an aggregation of ideas, not a full-featured tutorial. If you are already an experienced user, perhaps you will consider sharing your knowledge in the comments.
Learn from the best sources
There are several places where you can read and learn about Git, but it is hard to know which resource is best. In my opinion the best source of Git knowledge is a free book called "Pro Git" (http://git-scm.com/book). The second is an online tutorial called Git Immersion. If you feel you missed something or want detail on something specific, then there is always the official documentation.
Pick a workflow and strategy
Git is a great tool that allows you to work in many different ways. The possibilities and flexibility that you can achieve with it are a great thing. If you want, you can even use it in exactly the same way that you were using SVN (but there is no real point in doing that). To simplify team communication and avoid conflicts, it is good to follow some rules and standards created by other developers. I would recommend you read about GitFlow and GitHub Flow. Then you will be able to pick a strategy that suits you (and your team).
Git is only one of the many tools that you are using, but you will be spending a lot of time with this tool, so it is worth taking the time to customise it to your own needs. Most of the useful options were described in the Pro Git book, and I have also included below some basics from my configuration. To use these, you will need to edit the config file placed in your home directory. For example:
$ vim ~/.gitconfig
You can then copy, paste and customise the following lines as you wish:
[color] diff = true [user] name = Name Surname email = email@example.com [alias] co = checkout fa = fetch --all ci = commit st = status br = branch last = log -1 HEAD unstage = reset HEAD amend = commit --amend staged = diff --cached unstaged = diff
Another great approach is to steal the best configurations provided by my colleague Alistair on his GitHub account github. Alistair has taught me a lot about Git, so trust me when I say it is worth following his way of using it.
Extra tip: configure your Bash prompt to display current branch and other related information, using the instructions in asemanfar.com/Current-Git-Branch-in-Bash-Prompt.
If you like to be rewarded for your work you might be interested in a project called Git Achievements. This project allows you to receive points for using Git on a daily basis, and you can challenge your friends to do the same! To get you started, my result http://cakper.github.com/git-achievements is waiting to be beaten by you ;)
Just do it
The best way to learn is to begin - right now! Create a testing repository, play with it and don't be scared to break something. The skills you learn by practising now will be very useful in the future. Not everything will be easy from the beginning, but to help you along your way here are some tips for solving common problems:
To edit the most recent commit message:
$ git commit --amend -m "new commit message"
To revert an uncommitted file to the previous version:
$ git checkout HEAD path/to/file
To delete your last commit:
$ git reset --hard HEAD~1
To create a new branch from the current one (and move all uncommitted changes):
$ git checkout -b branch-name
To check out a remote branch from the origin repo:
$ git checkout -b branch-name origin/branch-name
If you want to learn some more serious stuff, a great way to do that is to join an open source project on GitHub. In my opinion it is the best place to acquire skills from other people, and also a good opportunity to use your own skills to contribute to a project used by many people.
I hope this short post will be a shortcut and allow you to kick off your experience with Git in a painless way. Having good Git skills will improve the way you work, and in time I think you will come to share my impression: this is such an easy and beautiful tool.
Leave your tips and share your experiences in the comments.