Git for the Nervous Developer
Join the DZone community and get the full member experience.Join For Free
i’ve been meaning to put together an article on git for quite a while but i’ve always ended up postponing it for various reasons, one of which being that there is already so much material describing git inside and out that i wasn’t quite sure i could add anything to the subject.
after thinking about this a bit more, i decided to go ahead anyway but to give this article a slightly different spin than what you might have read elsewhere. for this reason, you won’t find in this post any elaborate discussion of git’s branching model, command line switches or graphical tools. i do include at the end of this article a list of references that covers all the technical aspects of git that i’m glossing over today.
this article is meant for people who are interested in git, either personally or as a corporation, but wondering what making the jump will really mean and what to expect.
what you won’t find in this post:
- fanboy opinions (“git is so good that it cured my asthma”).
- hater opinions (“git’s command line syntax is so arcane that it will make your brain leak through your ears”).
what you will find in this post is the perspective of someone who:
- … was forced to switch to git
- … has become reasonably comfortable with it (although by no means an expert)
- … but still remembers the pain it took to get there.
state of the union
it’s hard to argue with the fact that git has gained a lot of momentum these past years and that it’s slowly eclipsing other more popular version control systems (vcs), such as subversion or perforce. just a few days ago, the subversion team released their roadmap which promptly generated discussions wondering whether subversion wasn’t dying (i think it’s a bit exaggerated and the subversion team is making the right decision by focusing on the non distributed aspect of their vcs).
what to expect
switching to git can be very easy or absolutely traumatizing, depending on how you approach it. individual users will usually have a pleasant experience since they can start small and expand their knowledge from there, but corporations have to take a big jump, retrain entire teams of developers, adjust their tools and their expectations and be ready to give up on a few benefits that they don’t think they can live without.
as a user
this is undoubtedly the best way to start with git. start by picking a hosting service, which can be either remote (e.g. github ) or local (your own machine), and from that point on, you can pretty much get by with just a few commands:
- git add [file] to tell git to track a new file.
- git commit -a -m "commit message" to commit the changes you have made to your local repository.
- git push to push your changes to the “server”.
- additionally, you will probably want to learn about git clone or git pull to download a repository to your machine.
you can go very far with these simple commands, which mimic almost exactly the work flow of a non-distributed vcs. this is the best way to get comfortable with git until you get bored, at which point you can start exploring some of its advanced features.
as a team
switching an entire team or organization from a regular vcs to git is a complex project that will require managing not just the technical complexity but also the users’ expectations. i could write a lot of pages on this topic alone, but for now, i’ll just give you a few quick tips:
- the transition will work much better if several of your employees are already familiar with git and can help you evangelize the idea and provide support to reticent users.
- be very clear on the features that they will have to learn to live without (e.g. monotony, see below) and emphasize the personal benefits (e.g. branch juggling, see below as well). you won’t get a lot of credit by telling them that they can inspect each other’s repositories, but they will most likely appreciate the fact that moving between several lines of code is trivial.
you will find plenty of definitions for what a “distributed” version control system is, but let me just sum it up for you and share a dirty little secret at the same time:
- a dvcs doesn’t have a central server.
- i bet that most people using a dvcs are still using a central server.
let me rephrase this: i think the dvcs aspect of git is not its main strength, but it does provide a lot of benefits which are really what get people hooked to git.
the one thing you need to know when you are using a dvcs such as git is that whenever you work on a depot on your computer, you are carrying the entire repository with you. i’m simplifying this a little bit, but it’s the general idea. in contrast, centralized vcs will usually allow you to create “views” or “clients” on your local machine, but the only place where the full repository is stored is on the server (and hopefully, its backups).
carrying a full repository on your machines sounds like a crazy idea but it comes with a lot of benefits (“cheap branching” and “easy offline work” are two benefits that developers will immediately appreciate) and, more importantly, it works, mostly because git is extremely efficient in the way it stores the representation of that repository (the fact that hard drives are cheap doesn’t hurt, of course).
since every user carries a full repository on their machine, everyone is a potential server, which means it’s very easy for developer a to “pull” a change from developer b in order to inspect it and then discard it or merge it in their own repository. this sounds very useful, but in practice, i think that most regular git users will not take advantage of that feature, preferring to push to and pull from a central server. and by “regular git user”, i mean “everyone except the linux kernel team”.
taming the beast
git’s user interface is probably its greatest weakness. it’s inconsistent, counter-intuitive and it often reappropriates verbs and nouns that have a clear meaning in the vcs world and it makes them mean something else.
i’ve been trying to come up with a lot of ways to rationalize this bizarre and arcane user interface, but in the end, all i can tell you is: “deal with it”.
i know, it’s sad, but there is really no other way. here is an analogy that might help, though: to me, learning git is very similar to learning a foreign language. natural languages are notoriously hard to learn for adults because their organic growth has resulted in all kinds of inconsistencies and oddities. at the end of the day, the only way to learn a foreign language is to memorize, memorize and memorize. as years go by, practice make memory regurgitation more automatic and the use of that language requires less and less conscious effort, but the learning curve is something that just can’t be avoided.
git is very similar in this respect. don’t try to understand why checkout suddenly means something completely different from what you were used to or to explain the various switches of the reset command: memorize the recipes and just apply them. and don’t be shy about asking seasoned git users, they are used to getting these questions and they will give you instant answers that will save you a lot of time hunting down the documentation.
however, there is hope for those of us who want to do more than regurgitating git recipes: if you are serious about becoming comfortable with git, you should definitely spend some time trying to understand its internals. i’m referring to the graphs, objects, commits, heads and branches that form the backbone of git. as opposed to its ugly user interface, git’s underlying infrastructure is very solid and extremely well designed. on top of that, it’s actually not that hard to understand, and once you do, a whole new world will open up.
once you understand these internals, you will be able to solve problems conceptually by simply visualizing the operations that need to be done on that underlying structure (moving the head there, branching here, rebasing this, merging that, etc…). once you have solved that problem conceptually, applying the awkward git user interface to actually do the work will look much easier, albeit still not very natural.
the end of monotony
centralized vcs such as subversion and perforce offer a very popular feature: monotonic revisions. whenever a new change list is committed to the main repository, it receives a unique number that’s guaranteed to be greater than the previous submission. this makes it very easy to compare files and commits chronologically and to determine which one is the most recent.
unfortunately, you will have to give up this feature with a dvcs. because of its distributed nature, it’s not possible (or rather, “practical”/”easy”) to offer such a monotonic number. there is no denying that this feels like a huge step backward, especially for large teams.
however, the absence of such a feature reflects the more fundamental reality that these numbers are not always accurate, and that the bigger and more active and branched out a project is, the more meaningless they become. when branch 2.1 receives commit #720 and branch 2.0.4 has #742, what do these numbers actually tell you about these change lists?
the good news is that there are ways to make up for the absence of monotony, and the main difference is that the tagging will be performed by humans, which is what a lot of vcs-based organizations are already doing anyway.
when you are trying to make the case for git in front of a skeptical crowd, it’s important to provide anecdotes that developers can relate to. i promised that this post would avoid content that can be easily found elsewhere, but i have to talk a little bit about branching because for me, understanding this was the first time that i actually acknowledged a very tangible advantage of git over other source control systems.
the daily routine of a software developer typically involves a lot of multitasking. as much as you want to focus your work on “feature a” and you don’t want to be interrupted, there are many reasons why you might be forced to put your work on hold and start working on “bug b” or “feature c” before being able to resume work on “feature a”.
switching back and forth between tasks has always been painful with regular vcs but git makes it very smooth. that’s all i’m going to say about this topic, but if you want to find out more, most of the links that i supply at the end of this article will show you how to achieve this goal very early on in the tutorial.
gerrit is a web based application that allows you to perform code reviews for git repositories. it’s basically a server that acts as a gatekeeper: developers push their git changes to it, other developers review them and when these changes are approved, gerrit pushes them to the main repository on behalf of the original submitter. if you’re curious to see gerrit in action, here is the android open source gerrit .
the reason i mention gerrit (besides the fact that i contribute to it in my 20%) is that it represents an interesting compromise where a dvcs is used as a centralized vcs. you might wonder if using a dvcs with a central server is not a bit paradoxical, and i can’t argue that it is in a certain way, but i see this as having the best of both worlds.
the gerrit server acts as a canonical repository from where developers can clone their own repositories but all the facilities offered by a dvcs (direct push/pull between developers) are still possible and fully compatible with this model. for example, you could pull a change from a fellow developer, makes a few additional changes and then push that change to gerrit so it will be committed to the canonical repository. after that, both developers can pull and merge in order to reconcile their local repository with the central one.
first of all, i’d like to apologize for the amorphous nature of this post. i tried to structure it in a way that would show some linearity and consistency, but this is basically a big brain dump of points that have been twirling in my head for a while.
looking back, it took me a while to warm up to git (quite a while), but now that i’m here, i really enjoy using it. as i hinted throughout this essay, the benefit will probably not be very apparent for very small teams that mostly commit and push (although you will probably find value in branch juggling even if you’re the only one on your project), but as the team and the number of committers and releases grows, you will find that git provides an undeniable increase in productivity.
- pro git . this is a free pdf book written by scott chacon, a git expert who happens to be also very good at explaining git concepts very clearly.
- the git community book . another book, written by the entire git community, so not as consistent as chacon’s book but with quite a few useful tips as well
- git user’s manual , written by linus torvalds and a few other authors.
- git magic . a small and digestible overview of the main concepts.
- git ready . a web site dedicated to providing git recipes to solve common problems. a great time killer when you feel like you’d like to learn something about git but you’re not sure what.
- git documentation . a misleading name that might lead you to think this is the authoritative documentation for git, which it’s not. it’s more like a hub that points to other git materials.
- embrace the git index . a good overview of one of git’s core components.
- smacking git around . another document by scott chacon, this time showing advanced tricks.
- git from the bottom up . save this for the end and only if you want to become a git expert. this book explains how git stores its objects and dives deep into the git internals. you don’t really need to know any of this to be a happy git user.
- git manuals . unix manuals usually represent the worst possible way to document tools, but the git manuals are a pleasant exception in this ocean of mediocrity because they do something right: they show you examples for every single command. see for example the manual for checkout (scroll to the bottom).
Opinions expressed by DZone contributors are their own.