Resolving Merge Conflicts While Using Git and GitHub

LAV JOSHI
QuikNapp
Published in
3 min readSep 25, 2020

Introduction:

A Quick Overview: Hope you all are familiar with Git and GitHub. Basically, Git is a distributed version control system designed to handle versions of projects and coordinating work on those files among multiple people using branches and GitHub is a platform that provides hosting for software development using version control with Git.

It serves two main purposes : Code Backup and Code versioning. One can save every step of their project and can get back to it at any stage.

Understanding Basic Git Commands:

git init : To initialize a git repository for a new or existing project.

git init

git status : To check the status of files you’ve changed in your working directory, i.e., what all has changed since your last commit.

git status

git add . : adds changes to stage/index in your working directory.

git add .

git commit : commits your changes and sets it to new commit object for your remote.

git commit -m “A short message”

git push / git pull : Push or Pull your changes to remote. If you have added and committed your changes and you want to push them. Or if your remote has updated and you want those latest changes. Syntax :

git pull <:remote:> <:branch:> and git push <:remote:> <:branch:>

git branch : Lists out all the branches.

git branch

git checkout : Switch to different branches.

git checkout <:branch:>

git fetch :

git fetch <remote> <:branch name:>

git merge : Merge two branches you were working on.Switch to branch you want to merge everything in.

 git merge <:branch_you_want_to_merge:>

When Merge Conflicts arises:

What is merge conflict: A merge conflict usually occurs when your current branch and the branch you want to merge into the current branch have diverged i.e. in both the branches which you want to merged have changes in the same location.

Now, when Git merges the other branch into your current branch, it looks at the differences between the base commit and the current revision, and at the differences between the base commit and the other branch’s latest commit. When there are unambiguous differences (i.e. only one side changed a certain piece of code), the changes are applied.

Merge conflicts occur when there are disagreeing changes. In that case, your conflicted file will have so-called conflict markers:

One can know the, where the conflicts are you can run the following command

git diff

Between the <<<<<<< and =======, you will find the version as per the changes in your current branch, relative to the base commit i.e. they are changes in the branch in which you want to merge.

Between the ======= and >>>>>>>, you will find the version according to the other branch, relative to the base commit i.e. they are changes in the branch from which you are merging to your current branch.

For convenience, after the <<<<<<< and >>>>>>> markers, you will see hints as to which commit that part of the conflict stems from, HEAD of course is the current revision.

To resolve the conflicts, you have to decide what the end result should be. This is not something you can do without thinking, otherwise, Git would have done it for you.

Committing the resolution:

After you resolved all the conflicts, add the resolved file contents:

git add .

and then commit the merge:

git commit -m "Message"

Using Command Line Interface(CLI) commands:

In this section, I’m making a short video in which I had explained how to resolve merge conflicts from Command-Line.

Hope you are all clear with the merge conflicts that you will face while working in your projects.

--

--