DEV Community

Cover image for Git Merge
Pravesh Vyas
Pravesh Vyas

Posted on

Git Merge

Git Merge, It is one of those concepts that newbies Barely use, These are Majorly used in cases where we need to development on a Experimental feature or Want to try something which we don't like to ship. The Idea of Merging two separate branches to one always fascinated me, But I always use to think How would Merge Conflicts be resolved in a case Where things don't add Up or we have a file which has two different version on two separate branches. Lets see How Merging Works in Git.

According to definition

Git merge will combine multiple sequences of commits into one unified history. In the most frequent use cases, git merge is used to combine two branches. The following examples in this document will focus on this branch merging pattern. In these scenarios, git merge takes two commit pointers, usually the branch tips, and will find a common base commit between them. Once Git finds a common base commit it will create a new "merge commit" that combines the changes of each queued merge commit sequence.

Unmerged Git Branches
Merged branches

In the above image we can see that there is a Common base between the Two Points we want to Merge, What happens here is Git Diffs Both the Branches Against there Common Ancestor and Calculates Changes based on it.

Types Of Merges

  1. Fast Forward Merge.

    It occurs when there's a direct path between the current branch tip and the Current Tip. It Basically merges the history of Both the Tips.

    03-04 Fast forward merge.svg

  2. 3 way merge

    This Happens when there's no direct path between the Main and Current Branch. The Name comes from the Fact that git uses three commits to get make the merge commit it combines the source, feature and common base to come to a new merged commit .

05-06 Fast forward merge.svg

In a three way there could conflicts to files changes in the both the Tips

Conflict Resolution

To resolve them what we need to do is change the files and Use the Command

Git Add 
Enter fullscreen mode Exit fullscreen mode

and then

GIt Commit -m "SomeThing here" 
Enter fullscreen mode Exit fullscreen mode

To get the Tips Merges

and it will create the Merged Branch.

Various Commands that can help in resolution of conflicts.

git log β€”merge
Enter fullscreen mode Exit fullscreen mode

It helps in producing the list of command that are causing the conflicts

git Diff
Enter fullscreen mode Exit fullscreen mode

It helps in finding the difference between files between two States

Git merge β€”abort

Enter fullscreen mode Exit fullscreen mode

It helps by ending the merge process returns the repo into the original state as it was before merging happened.

That Sums up how merging Works Across Branches.

Top comments (0)