DEV Community

suryansh taragi
suryansh taragi

Posted on

MERGE CONFLICTS in GITHUB

MERGE CONFLICT

Certainly! In version control systems like Git, a merge conflict occurs when two branches have changes in the same part of a file, and Git is unable to automatically merge those changes. This can happen when two developers make changes to the same line or section of code independently. When you try to merge these branches, Git will notify you of the conflict, and it's up to you to resolve it.

Let's go through a simple example to illustrate a merge conflict and how to resolve it in a GitHub repository.

Certainly! In version control systems like Git, a merge conflict occurs when two branches have changes in the same part of a file, and Git is unable to automatically merge those changes. This can happen when two developers make changes to the same line or section of code independently. When you try to merge these branches, Git will notify you of the conflict, and it's up to you to resolve it.

Let's go through a simple example to illustrate a merge conflict and how to resolve it in a GitHub repository.

Example

Suppose you have a GitHub repository with a file called example.txt, and two branches: master and feature-branch. The example.txt file initially looks like this:

Hello, World!
This is an example file.
Enter fullscreen mode Exit fullscreen mode

Now, two developers make changes to the same line in example.txt, but in different branches.

In the master branch, the file is changed to:

Hello, GitHub!
This is an example file.
Enter fullscreen mode Exit fullscreen mode

In the feature-branch branch, the file is changed to:

Hello, World!
This is an updated example file.
Enter fullscreen mode Exit fullscreen mode

Now, someone tries to merge the feature-branch into master:

$ git checkout master
$ git merge feature-branch
Enter fullscreen mode Exit fullscreen mode

At this point, Git realizes there is a conflict. It might produce an error message, and if you open example.txt, you'll see something like:

<<<<<<< HEAD
Hello, GitHub!
=======
Hello, World!
This is an updated example file.
>>>>>>> feature-branch
Enter fullscreen mode Exit fullscreen mode

The lines between <<<<<<< HEAD and ======= are from the current branch (master), and the lines between ======= and >>>>>>> feature-branch are from the branch being merged (feature-branch). Now, you need to manually resolve this conflict.

RESOLVE IN GITHUB

to resolve the conflict, edit the file to decide what the final version should be. For example:

Hello, GitHub!
This is an updated example file.
Enter fullscreen mode Exit fullscreen mode

After resolving the conflict, you need to commit the changes:

$ git add example.txt
$ git commit -m "Merge branch 'feature-branch' into master"
Enter fullscreen mode Exit fullscreen mode

Now, the merge conflict is resolved, and you have successfully merged the changes from feature-branch into master.

Remember, conflicts are a natural part of collaboration, and it's essential to communicate with your team to ensure that conflicts are resolved in a way that maintains the integrity of the codebase.

RESOLVE IN VSCODE

Resolving a merge conflict involves combining changes from different branches in a way that makes sense. Here's an example of how you might resolve the conflict in a way that incorporates both changes:

Hello, GitHub!
This is an updated example file.
Enter fullscreen mode Exit fullscreen mode

In this resolution:

  1. "Hello, GitHub!" is taken from the master branch.
  2. "This is an updated example file." is taken from the feature-branch.

This resolution includes the changes from both branches to create a coherent and updated version of the file. Once you've made this decision, you would save the file and then commit the changes:

Visual Studio Code (VSCode) provides a user-friendly interface for resolving merge conflicts. Here's a step-by-step guide on how to resolve a merge conflict using VSCode:

  1. Open the File with Conflict:
    Open VSCode and navigate to the file with the conflict.

  2. Open Source Control (Git) View:
    Click on the Source Control icon in the activity bar on the side of the window. This will open the Source Control view.

    1. Navigate to the Changes Tab: In the Source Control view, switch to the "Changes" tab. You should see the file with a merge conflict listed.
    2. Review and Choose Changes: Click on the conflicted file to open it. You'll see the conflict markers (<<<<<<<, =======, and >>>>>>>) indicating the conflicting changes.
    3. Use the "Accept Incoming" or "Accept Current" Buttons: VSCode provides buttons in the gutter (to the left of the conflicting lines) to accept either the incoming (from the branch being merged) or current (from the branch you are merging into) changes. Click the buttons as needed.
    4. Manually Edit if Necessary: If you want to customize the resolution further or if the automatic resolution is not satisfactory, you can manually edit the file.
    5. Mark as Resolved: Once you're satisfied with the resolution, save the file.
    6. Commit the Changes: Go back to the Source Control view, stage the resolved file by clicking the "+" button next to it, and then commit the changes with a meaningful commit message.

Top comments (0)