0 votes
in Git by
How to resolve a conflict in Git?

3 Answers

0 votes
by
The following steps will resolve conflict in Git-

Identify the files that have caused the conflict.

Make the necessary changes in the files so that conflict does not arise again.

Add these files by the command git add.

Finally to commit the changed file using the command git commit
0 votes
by

What is a merge conflict?

In every situation where work can be parallelized, work will eventually overlap. Sometimes two developers will change the same line of code in two different ways; in such a case, Git can't tell which version is correct—that's something only a developer can decide.

If this happens, a developer will see the following error during a git merge:

Auto-merging [filename1]

CONFLICT (content): Merge conflict in [filename1]

Automatic merge failed; fix conflicts and then commit the result.

Resolving merge conflicts can take a minute or they can take days (if there are a lot of files that need to be fixed). It's recommended, and good coding practice, to sync your code multiple times a day by committing, pushing, pulling, and merging often.

How do you resolve a git merge conflict?

Git gives a clue to resolving conflicts in its error message. It says Merge conflict in [filename1], so you know there is a problem with that file. Then it says fix conflicts and then commit the result, so if you follow directions, edit the file, then commit it, everything should work fine. Let's see this in action.

Create a new Git repo, add a file, make a branch, make some conflicting edits, and see what it looks like.

Start with an empty directory and run git init:

$ ls -l

$ git init

Initialized empty Git repository in /home/bob/example/.git/

$

Now create a README file and commit the changes:

$ echo "This is a new README file" > README.md

$ cat README.md

This is a new README file

$ git add README.md

$ git commit -m "README file added"

1 file changed, 1 insertion(+)

create mode 100644 README.md

$ git status

On branch master

nothing to commit, working tree clean

$

Create a new branch:

$ git checkout -b "branch_to_create_merge_conflict"

Switched to a new branch 'branch_to_create_merge_conflict'

On the new branch:

$ git branch

* branch_to_create_merge_conflict

master

Make an edit:

This is a new README file

This is an edit on the branch

Now commit that edit:

$ vim README.md

$ git add README.md

$ git commit -m "Edits made to README on the branch"

[branch_to_create_merge_conflict 9c5e88a] Edits made to README on the branch

1 file changed, 2 insertions(+)

Return to the master branch, edit the README on line 3 with something different, and commit that.

Change to the master branch:

$ git checkout master

Switched to branch 'master'

Edit the README:

This is a new README file

This is an edit on the master branch

Commit the edit:

$ git add README.md

$ git commit -m "Edits made to README on the master branch"

[master 7ea2985] Edits made to README on the master branch

1 file changed, 2 insertions(+)

Merge the branch into master to see the error:

$ git branch

  branch_to_create_merge_conflict

* master

$ git merge branch_to_create_merge_conflict

Auto-merging README.md

CONFLICT (content): Merge conflict in README.md

Automatic merge failed; fix conflicts and then commit the result.

Now, go into the README file, as Git asks, to see what it looks like:

This is a new README file

<<<<<<< HEAD

This is an edit on the master branch

=======

This is an edit on the branch

>>>>>>> branch_to_create_merge_conflict

As you can see, Git added some syntax including seven "less than" characters, <<<<<<< and seven "greater than" characters, >>>>>>>, separated by seven equal signs, =======. These can be searched using your editor to quickly find where edits need to be made.

That there are two sections within this block:

The "less than" characters denote the current branch's edits (in this case, "HEAD," which is another word for your current branch), and the equal signs denote the end of the first section.

The second section is where the edits are from the attempted merge; it starts with the equal signs and ends with the "greater than" signs.

As a developer, you decide what stays and what goes. Mak

then close the file:

This is a new README file

This is an edit on the branch

As you can see, this keeps the branch's edits. You can run git status to see further instructions:

$ vim README.md

$ git status

On branch master

You have unmerged paths.

  (fix conflicts and run "git commit")

  (use "git merge --abort" to abort the merge)

Unmerged paths:

  (use "git add ..." to mark resolution)

  both modified: README.md

no changes added to commit (use "git add" and/or "git commit -a")

Notice that if you run into serious issues, you can abort the merge by running git merge --abort to abort the merge.

Follow the directions to add the file and then commit:

$ git add README.md

$ git status

On branch master

All conflicts fixed but you are still merging.

(use "git commit" to conclude merge)

Changes to be committed:

modified: README.md

$ git commit

[master 9937ca4] Merge branch 'branch_to_create_merge_conflict'

0 votes
by

If you want to resolve a conflict in Git then you need to edit the files for fixing the conflicting changes and then you can run “git add” to add the resolved files and after that you can run the ‘git commit’ for committing the repaired merge.

Related questions

0 votes
asked Sep 5, 2020 in Git by SakshiSharma
0 votes
asked May 15, 2023 in 2D Animation by Robindeniel
...