Predictive Hacks

Git Branches Tutorial

git branches

In this tutorial, we will talk about Git branches. Using simple examples, we will show you how to create, switch, rename, delete, reset, and merge branches. For this tutorial, I have created a git project, and you can follow along. Let’s have a look at my git project.

ls -ltr
Git Branches Tutorial 1

Let’s have a look at the git logs.

git log --format=oneline
Git Branches Tutorial 2

How to Create a new Branch

As we can see, currently we have only the master branch. We can confirm it by running:

git branch
Git Branches Tutorial 3

We can create a new branch by running the command:

git branch <branch_name>
Git Branches Tutorial 4

As you can see, I created a new branch called “first_branch” but currently, I am still in the master branch (green letters with the * ahead). Next, we will see how to switch branches.

How to Switch Branches

We can switch branches by running the command:

git checkout <branch_name>
Git Branches Tutorial 5

As we can see now, we are in the “first_branch” branch. Note that we can create and switch to the new branch in one line by running:

git checkout -b <branch_name>
Git Branches Tutorial 6

In the example above, I created a new branch called “second_branch” and I switched to it at the same time.

How to Compare Branches

We can also compare branches. For this example, we will commit some changes to the first_branch and then we will compare it with the master. Actually, I will add a new line to the file firstfile.txt.

git commit -am "I added a new line to the firstfile.txt"

Let’s see the logs. Note that the HEAD points to the first_branch now.

Git Branches Tutorial 7

Since we made this change, we can compare the two branches. The command is the following:

git diff <branch_name>...<another_branch_name>

In this example, I will compare the “master” branch with the “first_branch”.

Git Branches Tutorial 8

In green letters is the text that I have added to the “firstfile.txt”.

How to Rename a Branch

We can rename a branch by running the command. The -m flag comes from “move”.

git branch -m <oldname> <newname>

Note that the <oldname> is optional. If we skip it, it considers as <oldname> the working branch. We will rename the second_branch to renamed_branch.

Git Branches Tutorial 9

The second_branch became renamed_branch.

How to Delete a Branch

We can simply delete a branch by running the following command. The -d flag comes from “delete”.

git branch -d <branchname>

Some things that you need to know:

  • You cannot delete the branch that you are currently in. So, for example, if we want to delete the “renamed_branch” we need to checkout to another branch first.
  • You cannot delete branches that are not fully merged using the above command. However, if you are sure you want to delete it, you can run git branch -D <branchname>. Notice the -D flag.

How to Reset a Branch

By the term “reset” we mean to reset the changes to the state they had before a specific commit. It is like undoing changes, as we have explained in a previous post. There are 3 types of reset, the “soft“, the “mixed” and the “hard“. In StackOverflow you can find a good explanation of the 3 reset types. I will keep the explanation of Suresh Sharma.

  • reset –soft : History changed, HEAD changed, Working directory is not changed.
  • reset –mixed : History changed, HEAD changed, Working directory changed with unstaged data.
  • reset –hard : History changed, HEAD changed, Working directory is changed with lost data.

Some examples:

# soft reset moving the HEAD two steps back
git reset --soft HEAD^^

# soft reset moving the HEAD at a specific SHA
git reset --soft <SHA>

How to Merge Branches

In this section, we will show you how to merge branches. For this example, we will merge the first_branch with the master. First, we need to checkout to the master branch.

Git Branches Tutorial 10
Atlassian

Think about it like that the current branch (here the master) is the one that will “receive” the changes. Once we are in the master branch, we can run the command:

git merge <branchname>
Git Branches Tutorial 11

If we run the git diff command, we will see that there are no changes anymore.

Git Branches Tutorial 12

Finally, we can run the

git branch --merged
Git Branches Tutorial 13

Merge Conflicts

When multiple developers are working on the same projects, they will often work in isolated branches to avoid conflicts. The git merge command combines these branches and resolves any conflicting edits. When there is a conflict, you will see the following things in your file:

  • <<<<<<< HEAD
  • =======
  • >>>>>>> <branch_name>

The content between the <<<<<<< HEAD and the ======= is the content that exists in the master branch where the HEAD is pointing, and the content between the ======= and the >>>>>>> <branch_name> is the content in the merging branch.

How to Resolve Merge Conflicts

There are 2 main different approaches to resolve the merge conflicts such as:

Abort merge

We can simply type the command:

git merge --abort

Resolve the Conflicts Manually

We can resolve the conflicts by comparing the two files that they conflict and making the necessary changes so that to be an agreement between the two files. We can see the differences by typing:

git show --color-words

Then we have to remove all the text between the ======= and the >>>>>>> markers as well as the line <<<<<<< HEAD. Once you have cleaned the markers, you can add the previously conflicted file to the stage and then commit the changes.

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Join the inner circle

Join our 2.000+ members and receive exclusive
data science content 🧪