Branches

A branch is a parallel version of your source code, which has been created at a specific point in time. Typically you can visualize branches like:

Source: https://blog.seibert-media.net/blog/2015/07/31/git-mit-branches-arbeiten-git-branch/

The base branch is usually the “master” branch. This branch is always the default branch, which is automatically created when you create a repository.

But this branch doesn’t have to be always present in every repository! Theoretically you can create a new branch after committing your first commit and just do everything on that new branch instead of the master ranch. But this is not very common because most of the time the “master” branch is the common ground where everything comes back together.

Most important commands

Show all available branches

git branch

You will also be shown which branch is currently active in your current environment. But to create new branches you will have to have at least 1 commit in your repository.

Create a new branch

git checkout -b <branch-name>

With this command you will create a new branch on the basis of your currently active branch. If you haven’t created any other branches in your repository it will always take the “master” branch as basis for your new branch.

Difference between 2 branches

git diff <branch1> <branch2>

Example

Let’s create a new, empty folder and init our GIT repository.

git init

After that we create a text file and commit it to the “master” branch.

echo "master" > source.txt
git add source.txt
git commit -m "master file created"

With that we have our first commit done which also can be seen in git log.
But also we “started” the master branch with this first commit.

Now we can create a new branch on the base of this branch and extend our txt file.

git checkout -b small-feature 
echo "small-feature" >> source.txt
git add source.txt
git commit -m "added small-feature"

With that our git log looks like this:

commit 8a77da4bbaf8a3abdc1634e702fbacefac8b4345 (HEAD -> small-feature)
Author: Kevin Pfeifer <kevin.pfeifer@sunlime.at>
Date:   Sat Jun 22 13:20:33 2019 +0200

    added small-feature

commit 8a0d423e08b93effc490cc343e5f0e08edebcc7a (master)
Author: Kevin Pfeifer <kevin.pfeifer@sunlime.at>
Date:   Sat Jun 22 13:19:43 2019 +0200

    master file created
(END)

The git diff between master and the small-feature branch looks like this:

diff --git a/source.txt b/source.txt
index 1f7391f..3921d35 100644
--- a/source.txt
+++ b/source.txt
@@ -1 +1,2 @@
 master
+small-feature

With that you know the basics of what GIT branches are, how to create them and compare branches.

Of course the above shown method is not very “nice” to look at. Therefore many code editors have integrated GIT-Plugins, which allow you to do the above in the GUI and show you a better visual representation of how branches differ.

Share this post

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.