Using branches in your day-to-day work might very well prove to make you a better programmer or designer. Let's have a look at how branches work.
Branching can Change Your Life
This is quite a sensational headline, I know... But the truth is: it's not an exaggeration. Using branches in your day-to-day work might very well prove to make you a better programmer or designer.
First of all, if you're coming from another version control system, I kindly ask you to forget the things you already know about branching & merging. It's true that Git hasn't invented this particular wheel: many other VCS also offer branching. However, Git's concepts in this area are absolutely unique when it comes to ease of use and performance.
Now, let's look at why branches are so important.
Working in Contexts
In every project, there are always multiple different contexts where work happens. Each feature, bugfix, experiment, or alternative of your product is actually a context of its own: it can be seen as its own "topic", clearly separated from other topics.
This leaves you with an unlimited amount of different contexts. Most likely, you'll have at least one context for your "main" or "production" state, and another context for each feature, bugfix, experiment, etc.
In real-world projects, work always happens in multiple of these contexts in parallel:
While you're preparing 2 new variations of your website's design (context 1 & 2)...
you're also trying to fix an annoying bug (context 3).
On the side, you also update some content on your FAQ pages (context 4), while...
one of your teammates is working on a new feature for your shopping cart (context 5),...
and another colleague is experimenting with a whole new login functionality (context 6).
A World Without Branches
Not working in clearly separated contexts can (and sooner or later will) cause several problems:
What happens if your client likes variation 2 of the new design? In the meantime, a host of other changes have happened! How do you launch the new design while integrating all these other changes instead of losing them?
What happens if the shopping cart feature became obsolete because the client changed his mind? How do you get all that unwanted code (and only that code!) out?
What do you do if that new login functionality proves to be impossible to implement? It's already mingled with all of those other changes, being almost impossible to separate out!
How can you avoid losing track? Most likely, you shouldn't be bothered with all the topics from all of your colleagues.
Things will start to get very confusing when you try to handle multiple topics in a single context:
A tempting workaround might be to simply copy your complete project folder for each new context. But this only leaves you with other problems:
You circumvent your VCS, since those new folders won't be under version control.
Not being version controlled, you can't easily share & collaborate with others.
Integrating changes from one context into another (maybe your main context) is difficult and error-prone.
To make a long story short: if your goal is to work professionally, you'll have to find a way to deal with multiple contexts in a professional manner.
Branches to the Rescue
You might have already guessed it: branches are what we need to solve these problems. Because a branch represents exactly such a context in a project and helps you keep it separate from all other contexts.
All the changes you make at any time will only apply to the currently active branch; all other branches are left untouched. This gives you the freedom to both work on different things in parallel and, above all, to experiment - because you can't mess up! In case things go wrong you can always go back / undo / start fresh / switch contexts...
Luckily, branches in Git are cheap & easy. There's no reason not to create a new branch when you start working on a new topic, no matter how big or small it might be.
The Golden Rules of Version Control
No. 3: Use Branches Extensively
Branching is one of Git’s most powerful features – and this is not by accident: quick and easy branching was a central requirement from day one. Branches are the perfect tool to help you avoid mixing up different lines of development. You should use branches extensively in your development workflows: for new features, bug fixes, experiments, ideas…
Get our popular Git Cheat Sheet for free!
You'll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.