Recently I stumbled on a tutorial named Learn git in 30 minutes. While there is nothing wrong with that tutorial, it’s actually pretty accurate, and clear and easy to follow – thumbs up to the author about the writing – I have great concerns about how should we learn Git.
Git is not that easy.
Don’t get me wrong, Git is a great tool, perhaps the greatest developers’ tool since C language. Where I work at, we switched from Team Foundation Server to Git two years and a half ago, and I’ve never looked back – Git does things right where TFS did wrong. It really helped my life, as a developer, easier.
But everything comes at a cost.
As powerful and flexible as it is, Git is also complex and easy to mess up. It can be your best tool, but it can also be your worst nightmare, when something goes wrong.
And in a collaborative environment when you work in parallel with others on different branches, where merges are inevitable, something will always go wrong.
Merge conflicts, missing commits, rewritten history, etc. all kinds of nightmare can happen in Git. How to prevent those from happening, and even more important, how to fix when those happens? True story: I was the first one in my team to screwed our repository, and while I tried to fix my mistake, I made it worse. I’ve learned a lot since that day, but I never forget the cold in my back when I was desperately trying to fix our repository. That’s something I want to share with you today:
- There are two essential concepts in Git: commit and branch. It’s important to understand what a commit is, what does a commit hash mean, what is its parent commit and its children commits.
Try to understand what is a branch – what happen when you check out a branch, or create a branch, or rename one, or delete one. Try to understand a branch tree – how does your branch relate to its remote one, and how does it relate to other branches – what “downstream/upstream” mean (I personally prefer “ahead/behind”, as they are less fancy than downstream/upstream, and less fancy is good for me, but I know people use “downstream/upstream” terms a lot and it’s important to understand the ideas).
Branch tree is a reason I always recommend Git beginners to start with a Git client (And in those clients, I always recommend Git Extensions, as the client to beat). With the visualization, you can quickly grasp the idea how branches work in Git.
- Try to understand what is a merge is – how can it result in merge conflicts. Learn how to deal with conflicts – by using a good merge tool, like Beyond Compare.
- Try to understand the arguably most powerful and scary feature of Git – rewrite history. Know what a rebase is – and the difference between a rebase and a pull with rebase, and when you should use a pull with rebase. What does it do when you do a force push and why you should not do it, and when you need to do it, or the safer alternative force-with-lease. Learn how to disable force push in your repository, if it’s not already.
Learn that a Git commit is immutable and how can you get back a missing commit by git reflog. Understand what does amend commit do, and when should you use it.
- Have someone really good at Git to cover you up. This is very important, due to nature of Git, while it’s easy to clean a mess, it is also easy to make a bigger mess. When you do something wrong, ask for help, don’t try to fix it yourself when you are clueless about how to fix it.
If you simply have no one to back you up, try to practice on your own private repository first – it’s time to learn about fork.
In conclusion, the tutorial is a very nice write up – read it. But do not stop there, not with the illusion of you can use Git. Like other tools, it takes time to master Git – days, if not weeks to fully understand the concepts, what you can do, and what you should do.
Because Git is a tool. Essential tool, indeed. And you should be spending more time on thinking and writing code and solving code problems, not struggling around, solving Git problems.
Use Git with confident, not with accidents.
And may you, Save the day!
Shameless plug: I happen to be writing a book about Git: Git in easy steps, with examples in Git Extensions. Check it out at Leanpub.