top of page

Making sense of Git and GitHub - IV [Merging and Conflict]

This part is in continuation of this post - Making sense of Git and GitHub - III [Reset, Branching]

If the branch is created, it needs to be merged with the parent branch(master). It is the master branch ultimately deployed. This is called git merging.

We will illustrate merging,


1)No further commits in the master branch once Branch1 is created.

2)This type of merging is called 'Fast forward merging.'

Hands-on: Fast-Forward merging

I hope you can create branches and commits on your own by now. Hence we will concentrate on merging.

You need to switch to the master branch now because we need to merge at the parent level.

The command is git merge <branch_name> --> git merge Branch1.

$ git merge Branch1
Updating 46e757c..0992851
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 a.txt
 create mode 100644 b.txt

Now give git log --oneline from the master branch,

$ git log --oneline
0992851 (HEAD -> master, Branch1) Branch1 second commit
988b839 Branch1 first commit
46e757c master second commit
3f09835 Master first commit

This is a simple git fast-forward merge and happens when there are no commits in the master branch.

Another scenario,

When you have commits in the master branch after Branch1 is created, it is called a three-way merge.

Hands-on: Three-way merge

Same setup but this time, there is one extra commit in the master branch.

Now give git merge Branch1 from the master branch.

It will open an editor like this,

Just press the escape key and type ':wq!' [command to save and quit]

Now you will get,

$ git merge Branch1
Merge made by the 'recursive' strategy.
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 a.txt
 create mode 100644 b.txt

This type of merging is called a 'three-way merge.' This is different from 'fast-forward-merge' because 'three-way-merge' creates a new commit called merge commit.

Give git log --oneline now,

$ git log --oneline
bb817f9 (HEAD -> master) Merge branch 'Branch1' --> New commit is created by git itself.
910f028 master third commit
d56617b (Branch1) Branch1 second commit
5dc103d Branch1 first commit
e2b8c5b master second commit
7486cd4 Master first commit
Resolving conflict in merges:

Conflict arises when the master and branch work on the same file. The corresponding file will be committed both in the master and branch. Therefore when you merge, conflict arises.

To merge, we need to resolve the conflict manually.

Hands-on: Merge conflict

For this, I am going to use VS editor for illustration. We will work on a single file for better understanding.

When you all set up above and try to merge, you will get,

You will get CONFLICT as a message. It prompts you to fix conflicts and commits the result.

In VS code, you get

Accept Current Change.

Accept Incoming Change.

Accept Both Changes.

Compare Change.

If you compare changes, you get,

We will give Accept both changes.

That's how we resolve the conflict manually.

Remember, we need to commit the changes to the repo.

Give git log --oneline,

$ git log --oneline
565d87c (HEAD -> master) Resolved to merge conflict
813d82d Third line from master1
621e707 (Branch1) Third line from Branch1
009477f Second line
ea358f0 First line

This time merge commit is done manually by us.

Auto-merging and commit do not happen because conflict arises.

Remember, auto-merge occurred in a 'three-way merge' with no conflict.

I hope this post gives you a broad idea of Merging and Conflict.

bottom of page