in

The Facts about Git Branches and Why they are so Important

GitHub
GitHub

The Tree of Git

In the world of Version Control Systems (VCS), branching is a common feature. It allows you to diverge from the main line of development and continue to work without interfering with that main line. However, in many VCS tools, branching is a somewhat expensive process, often requiring you to create a new copy of your source code directory, which can take a long time for large projects. But Git, the modern VCS, has revolutionized this process. Its branching model is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast.

Understanding Git Branches

To truly understand how Git does branching, we need to take a step back and examine how Git stores its data. Unlike many other VCSs, Git doesn’t store data as a series of changesets or differences, but instead as a series of snapshots. When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged. This object also contains the author’s name and email address, the message that you typed, and pointers to the commit or commits that directly came before this commit (its parent or parents).

The Essence of a Branch

A branch in Git is simply a lightweight movable pointer to one of these commits. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically.

Creating a New Branch

Creating a new branch in Git is as simple as running the git branch command followed by the name of the new branch. This creates a new pointer to the same commit you’re currently on. However, it’s important to note that the git branch command only creates a new branch — it doesn’t switch to that branch.

Switching Branches

To switch to an existing branch, you run the git checkout command. This moves the HEAD pointer to point to the new branch. Now, any changes you make will be on this new branch. When you commit again, the new branch pointer moves forward, but your master branch still points to the commit you were on when you ran git checkout to switch branches.

Diverging Branches

Your project history has now diverged. You created and switched to a branch, did some work on it, and then switched back to your main branch and did other work. Both of those changes are isolated in separate branches: you can switch back and forth between the branches and merge them together when you’re ready.

The Power of Branching

Because a branch in Git is actually a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline). This is in sharp contrast to the way most older VCS tools branch, which involves copying all of the project’s files into a second directory. This can take several seconds or even minutes, depending on the size of the project, whereas in Git the process is always instantaneous.

Branching Out

Git’s branching model is a powerful tool that can entirely change the way that you develop. It encourages workflows that branch and merge often, even multiple times in a day. Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

GitHub

10 Best Practices when using Git as a Game Developer

GitHub

Top 10 Interview Questions about Git and How to Ace the Anwers