A little over ten years after its initial release, git has become the golden standard of version control systems. Of course, its popularity has probably received a well-deserved boost by popular services like Github, but I would argue that regardless of which hosting service you use – if you even use one; one of the beauties with git is that you don’t need one at all! – git is in itself powerful enough to be worth mastering.
In this post, I’ll share some tips and tricks that I’ve found very useful when learning and using git myself, as well as give a few suggestions for further reading and interactive learning.
But before we start, let’s briefly explore some reasons of trying in the first place…
What’s so good about git, and why do I have to be so good at it?
(Why can’t i use SVN/hg/CVS/Dropbox?)
Git is, in its essence, incredibly non-destructive. There are, in fact, very few ways to do something that can’t be undone; most of them mean undoing something which you haven’t told git about, meaning you basically didn’t give git the chance to keep track of it for you in the first place. This means, that it’s incredibly hard to mess something up in a way that can’t be salvaged as long as you have enough git-fu to find your way back. (And that’s why you’re reading this blog post!)
Git is also distributed. This sounds mostly like a buzz-word, but it opens up a host of possibilities for how to organize your code and your workflows. It also makes it really easy to host git yourself – you can have the remote in a different folder on your computer, on your own file server or in your own cloud service, or even not have one at all. This makes git useful even in places where most other versioning systems would be overkill. How about making sure you don’t mess up your system configuration, by simply
cd ~/.config && git init?
If you want to read more about git before you start, do check out the about page on the official git website.
Git is a quite complex system, and it will take a while to learn to make efficient use of it. Here are a few things to keep in mind along the road – hopefully, they can help to make learning both more efficient and more fun!
Don’t be afraid to use the terminal!
GUIs might be great, but I’ve also seen them become a burden, especially on git newbies. Most GUIs don’t stop at providing a graphical user experience – they also add abstractions over git actions that make common tasks more convenient, but at the same time make more difficult to actually understand what happens in your repository. By running the terminal commands yourself, you’ll build a deeper understanding of how git fits together. (And when you have that, you’re in power of your GUI of choice, instead of the opposite!)
Commit often, branch more often!
Local branching in git is really cheap. Basically, a branch in git is just a pointer to a commit, so having one hundred branches with one commit in each is about as space-consuming as having one branch with a hundred commits. Especially while learning git, this can be used to make sure you always have a reference point to get back to, should you make a mistake – just checkout a temp branch whenever you’re about to do something you’re not quite sure how you would undo.
Inspect your repo often!
Since I encourage you to do much of your early work in git in the terminal, I also want to encourage you to inspect the state of the repository often. This will not only help you plan your next move – it will also increase your understanding of what your last move really did. To help you with this, I’ll give you a few handy commands:
# Show a colorful and compact overview of the branches; usage `git lg` git config --global alias.lg 'log --oneline --decorate --graph --all' # Show what's been added to the staging area, but not yet committed; usage `git diffc` # Basically, shows what `git diff HEAD^..HEAD` will look like *after* next `git commit` git config --global alias.diffc 'diff --cached' # `git diffc` is of course best used in conjunction with regular `git diff`
Make a lot of mistakes (then fix them)!
Since git is so non-destructive, it is very friendly to trial-and-error learning. You can undo (almost) anything, which lets you experiment and then get back to where you started if you’re not happy with the result.
Learn to read the manual!
Git has an excellent manual, available in many forms. You can read it online, or in your terminal – either using e.g.
man git commit or
git help commit (if your terminal doesn’t support
man, the latter will open the corresponding help page in your browser). The manual is littered with instructive and illustrated examples of excellent quality.
Learn to read the reflog!
git reflog is one of the most handy commands for fixing something when you broke it. Basically, it lets you figure out things like “where was
HEAD before I made these three last, and in retrospect not so smart, moves?” so that even if you do something that you don’t know how to inverse, you can just jump back in time.
Of course there is always a lot of stuff you can read to become better at something. This list is by no means exhaustive, but it’s a start and there are quite a lot of links in these resources that will give you more material, should this not be enough.
- As noted, the official documentation is top-class. There are guides and tutorials on all levels, the reference manual has examples everywhere, and there are both web pages, an e book and videos. Seriously, do check it out.
- If you’re just getting started with git, Roger Dudler‘s Git – the simple guide is for you. It neatly summarizes the most important tasks and commands in the git terminal, and there’s a cheat sheet (pdf) to print out and keep by your desk.
- Learn Git Branching by Peter Cottle is a fantastic, interactive guide to git with focus on manipulating branches. It’s built as a small game with 18 levels from “Introduction to git commits” to “Rebasing over 9000 times!”, and runs in your browser.
- Git Immersion is a guided tour through git fundamentals, starting with installation of git on your system, and then walking you through building a small Ruby app as an excuse for having something to make changes to. It’s covers more ground than Learn Git Branching, but it’s also a little more cumbersome to work with.
- Finally, Github has a pretty good help section with instructions that are often applicable even if you’re not using their hosting service.