DEV Community

folacode22
folacode22

Posted on

How to resolve merge Conflict in git ?

git image

Table of contents

  • what is git?
  • why git?
  • basic git command
  • type of git merge conflict
  • how to resolve merge conflict in git?
  • git commands to resolve conflicts
    • What is Git?

      Git is an open-source, distributed version control system (VCS) designed to handle everything from small to very large projects with speed and efficiency, which has a remote repository on the server-side and a local repository on the client-side. This means that the file or code is not present on a central server but there is a copy of the file stored on the client’s computer.

      why git?

      Simply put, Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories. If you have open-source projects that use Git, then GitHub is designed to help you better manage them.

      Basic Git Commands


- git config 
- git init
- git add . 
- git diff
- git commit
- git reset
- git status
- git merge
- git push
- git pull

What is a Git Merge Conflict?

A merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

Types of Git Merge Conflicts

There are two points when a merge can enter a conflict state:

  1. Starting the Merge Process If there are changes in the working directory’s stage area for the current project, merging won’t start. In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands.

  2. During the Merge Process The failure during the merge process indicates that there is a conflict between the local branch and the branch being merged. In this case, Git resolves as much as possible, but there are things that have to be resolved manually in the conflicted files. We will now go over resolving merge conflicts in Git.

How to Resolve Merge Conflicts in Git?

There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.

  1. The easiest way to resolve a conflicted file is to open it and make any necessary changes

  2. After editing the file, we can use the git add command to stage the new merged content.

  3. The final step is to create a new commit with the help of the git commit command.

  4. Git will create a new merge. commit to finalize the merge

Git Commands to Resolve Conflicts

  1. The git log --merge command helps to produce the list of commits that are causing the conflict. >
 git log --merge

Enter fullscreen mode Exit fullscreen mode
  1. The git diff command helps to identify the differences between the states repositories or files. >
git diff 
Enter fullscreen mode Exit fullscreen mode
  1. The git checkout command is used to undo the changes made to the file, or for changing branches >
 git checkout 
Enter fullscreen mode Exit fullscreen mode
  1. The git reset --mixed command is used to undo changes to the working directory and staging area >
 git reset --mixed 

Enter fullscreen mode Exit fullscreen mode
  1. The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began. >
git merge --abort

Enter fullscreen mode Exit fullscreen mode
  1. The git reset command is used at the time of merge conflict to reset the conflicted files to their original state >
git reset
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
thomasbnt profile image
Thomas Bnt

Hello !

Avoid titles in all caps, it makes the post seem aggressive for a nice content.

Collapse
 
folacode22 profile image
folacode22 • Edited

thanks, noted.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Merge conflicts are the bane of every developer's life.

Collapse
 
gerbilalways profile image
gerbilalways

Every programmer's worst nightmare is dealing with merge conflicts. mario games