Back to Top

Branching and Merging in Git

In this article, I am going to explain how you can define git branches, create and delete branches. Also, We will understand the purpose of the Head Pointer. The most important concept of git is merge branches so today I am going to explain all the ways, fast forward, 3 way merges and how to resolve merge conflicts.

If you want to get the basic idea about Basic Github commands, read it in my previous blog post.Here, I am going to give you a fantastic resource for diving into GitHub. Let’s jump right into an article.

First of all, let’s create a directory and create a testing repository using the git init command:

Now, I have one testing file first.php and few lines of code.

Let’s get this file staged into the first commit to this repository.

Commands to commit is:

Here, I have done my first commit to this repository. You can do as much commit as you require. Commit is a linear list of changes you have done so far.

If you run the git status, you will get to know that you are in the master branch. I didn’t create that branch. Git created it automatically in the new repository. Git gives the first branch name master.

Let’s understand Git branches.

What is a branch for?

Branches on Git allows us to work on different versions of the same files in parallel. Your edit on one branch can be independent of work on other branches. You can then decide to merge your changes into other branches.

Branches result in separation of versions of the same files. In this way, you can have branches for different purposes.

For Example, You can have a production branch, a development branch and branch to work on bug fixes.

How to implement Git Branch?

The implementation of Branch is quite simple. Once you start using it, you will love it to use.Let’s look at commit graph to see a visual representation of a branch.

Branching and Merging in Git

Here are my last two commits in the repository. Every commit has 40 hexadecimal sha-1 hash. The first 7 characters of those hashes are shown here. By Default, git created Master branch. A branch is just a pointer to the sha-1 hash. As per diagram, Master branch pointed to the latest commit.

As long as you are on a master branch, every time commit a code, branch moves one step up to latest commit. The way git knows which branch we are on is a special pointer called HEAD.

What is HEAD in Git?

Head is one type of pointer which normally points to a branch and if you have only one branch “Master branch”, it will point to it. Head usually points to a branch, not directly to the commit. It is sometimes called a symbolic pointer.

As per Git terminology, Head pointer tells what you have checked out from the remote.

How to check git commit history?

To check the commit history, you can use git log command. You can also add some options to get nice labeled commit graph.

Here is the command:

When you run this, you will see the commit graph.

As this is a very common command which you can use on daily basis. If you don’t want to remember the whole command, you can create a reusable command alias.

Here is the command to create alias:

With this command, you just type graph, it will show the commit graph output.

How to create Branches?

You can create branch using the following command:

The branches will be instantiated where the HEAD pointer is.You can use the following command to list out all of your branches:

In the list, you can see the asterisk next to branch which means you are at that branch and checkout from it. HEAD pointer is pointing to the branch which has an asterisk

To checkout from the branch, you can use the following command:

Once you check out the branch, a HEAD pointer will move to that branch. When you commit from the branch, the only branch will move up. You can commit multiple branches to different commit with the different content of the same file.

Whenever you check out the branch, git update the working tree and index to reflect the commit we made the change.

To get the information and modification in the working tree, you can run the below command

You can use the -a option in git commit to stage and commit any tracked files that have been modified.

Once your work is done in different branches, you can merge branches to the main branch.

What is Merge in git?

Branches allow you to create separations in your code. You can easily isolate your code and divide code amongst the team of developers. Once you are done with the task you wanted to do with the branch, you have to merge your all code into the single master branch so you can deploy all of your work together onto your server.

Here we are going to talk about two types of merges.

  1. Fast Forward merge
  2. 3 way merge

Fast Forward merge:

The fast forward merge in git is, git will move the master branch to another branch is. Master just has to catch up another branch changes. Even if there are multiple commits between the two branches, there is still fast-forward merge. There just needed a direct path.

To merge the branch, you can use the following command:

If there is a direct path from master branch to another branch, what git perform called as fast-forward merge.

If you want to see the changes done in the branch, you can use the following command:

This command shows what will change when merging into the master branch.

Here is one more command which is used to check your branch is merged or not:

This command will show that master branch is merged with how many other branches.

3 way merge

If there is no direct path from branch to master branch, git can’t do the fast forward merge and in this case, a 3-way merge will happen.

To merge another branch to master branch, you can’t just move the master pointer here.if you do that, you will lose the last changes done by another branch.

You need to merge all branches together into a new commit called a merge commit.

To make the merges, git will do the three commits:

Base commit between two branches started from
Last commit of each branch

If you are going to perform, merge commit you need to pass the commit message so git will open the area to write your message. You can accept git default message or write own, save and exit.

It says merge made by the recursive strategy. This 3 way merge will work out without any conflicts.

How to delete branches in git?

Sometimes you want to delete the branch you created and If you want to delete your branch, you can use the following command:

If you try to delete the branch using above command which is not merged, you will get the error message.

However, you want to forcefully delete the branch, you can use the D in the command.

What is conflict and how to resolve conflicts?

Here, I am going to show you how to resolve a basic merge conflict. Merge conflicts occur when you try to merge branches that have changes at same lines in the same file.

When you have done changes in one branch and there is no change in another branch, so git will not give you conflict. Git will assume you do have a change in the branch. Change vs no changes in the branch, change will win.

When you have a change in the same line, git can’t guess for you that which one you want in the merge commit.

When conflict occurs and you type git status command, git indicate that you are in the middle of the merge by “ you have unmerged paths”

Git gives you a backout plan that is if you don’t want to deal with conflict, you can run the following command:

After conflict, you can see set of equal signs(========) and set of greater than signs(>>>>>>>>). The set of equal signs(=========) separate out the state of the file in the two branches. You need to decide how you want to merge the file.

After completing you conflict changes, you have to commit your changes as a standard commit.

This is all about git branches and merges the code. This seems complex process in the first place but once you adopt it, you will love to follow the process. You can work with any numbers of developers team and easily deploy your code to the server.

Hope this article explains all about git branches and merge in the easiest manner. If you have any query or question, please ask me in the comment section.

Comments (3)

  1. Absolutely written content, appreciate it for entropy. Sophi Barnard Brosy

  2. I think this is among the most significant info for me. And i am glad reading your article. But wanna remark on few general things, The website style is ideal, the articles is really nice : D. Good job, cheers Trista Rutger Pickar

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.

Most Popular Posts

To prevent caching in web browser

Posted on 12 years ago

Bhumi

How does CI/CD works on AWS?

Posted on 7 years ago

Bhumi

LESS CSS – Nested Rules

Posted on 10 years ago

Bhumi