Git Branches

So far we have worked only on one branch, which is by default called the "master" branch. It's the one you selected when returning to the latest version in the last post.

From this point on Git related things will be managed through Visual Studio Code, for efficiency.

What are Branches?

Since you always have a minimum of one branch to store your entire commit timeline, you might want to expand into a different direction, rather than forward all the time.

With Git you could imagine the master branch as being the trunk of the tree and then you create branches from it.

You can also create branches based on other branches.

Creating Branches

To create a new branch, click the commit id or name at the bottom left again and a dropdown should appear at the top of the window, select "Create a new branch" and then give it a name, like for example: "Paragraph idea".

After pressing "Enter" you will see that Visual Studio Code has already switched to the new branch automatically.

The beauty of branches is that any changes you now make while on your separate branch, rather than "master", will be commited only to that branch. The original file structure remains the same.

To switch back to the "master" branch, just click the branch name in the bottom left again and choose "master".

Now just so there are some actual changes to the text file in our new branch, let's add a dummy paragraph.

Usually, when the needed text is not known or it doesn't matter, developers generate what is known as Lorem Ipsum text.

So just create or generate a paragraph and add it into the text file anywhere you like and commit the changes while being on the separate branch.

Viewing Branches

Visual Studio Code doesn't have any out-of-the-box visualizing tools for the commit history, apart from the dropdown with the list of them in chronological order.

Visual Studio Code has plenty of Git extensions and visualizing the history in an easy to read format is also available, it's called Git Graph. So go to the Extensions tab in Visual Studio Code and install Git Graph.

After installing it, you can press F1 and type "View Git Graph" to open the extension.

You can see your branch if you open up Sourcetree too.

GitGraph

Now you should see something similiar to the above screenshot. You can see the 2 branches, which you can switch between by double-clicking. Aswell as all of the Git commit info you've already seen before.

Feel free to explore what this tool offers and what all the different buttons do. There's a lot of them, but you will usually use only a relatively small part of them.

Merging Branches

Now you have succesfuly added your new code/functionality/text or whatever it is you are version controlling, you probably would like to get those changes back to the main "master" branch.

To do so, you have to merge branches.

To merge branches, you have to switch back to the "master" branch and from the Git Graph extension, right-click the branch where you added the paragraph and select "Merge into current branch..." and withotu changing anything in the next popup, press "Yes, merge".

GitGraph2

Now Git branching should hopefully make more sense.

My recommendation is to try and break it as many times as possible before you have a deadline approaching, so your muscle memory knows what to do once problems arise in real world scenarios.