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.
Let’s have a look at the git logs.
git log --format=oneline
How to Create a new Branch
As we can see, currently we have only the master branch. We can confirm it by running:
We can create a new branch by running the command:
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:
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
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
git commit -am "I added a new line to the firstfile.txt"
Let’s see the logs. Note that the HEAD points to the
Since we made this change, we can compare the two branches. The command is the following:
In this example, I will compare the “master” branch with the “first_branch”.
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
Note that the
is optional. If we skip it, it considers as
the working branch. We will rename the
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
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. Notice the
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.
# soft reset moving the HEAD two steps back git reset --soft HEAD^^ # soft reset moving the HEAD at a specific SHA git reset --soft
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.
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:
If we run the
git diff command, we will see that there are no changes anymore.
Finally, we can run the
git branch --merged
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:
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
>>>>>>> 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:
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.