5436

How do I resolve merge conflicts in my Git repository?

7
  • 43
    The following blog post seems to give a very good example on how to handle merge conflict with Git that should get you going in the right direction. Handling and Avoiding Conflicts in Git Commented Oct 2, 2008 at 11:40
  • 4
    You can configure a merge tool (kdiff3 jebaird.com/2013/07/08/…) and then use git mergetool. When you're working in large developer teams you'll always encounter merge conflicts. Commented Apr 18, 2015 at 5:37
  • Don't forget that you can mitigate most merge conflicts by regularly merging downstream! Commented Jul 27, 2015 at 9:50
  • 3
    Also see git-tower.com/learn/git/ebook/command-line/tools-services/… Commented Oct 20, 2015 at 11:19
  • A niche, related question on resolving a conflict in just one file, from command line, using three-way merge with given strategy: stackoverflow.com/q/39126509/245966 Commented Aug 25, 2016 at 8:48

37 Answers 37

1
2
2

For those who are using Visual Studio (Visual Studio 2015 in my case)

  1. Close your project in Visual Studio. Especially in big projects, Visual Studio tends to freak out when merging using the UI.

  2. Do the merge in a command prompt.

    git checkout target_branch

    git merge source_branch

  3. Then open the project in Visual Studio and go to Team Explorer → Branch. Now there is a message that says Merge is pending and conflicting files are listed right below the message.

  4. Click the conflicting file and you will have the option to Merge, Compare, Take Source, and Take Target. The merge tool in Visual Studio is very easy to use.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using VS Code 2017 on a very large project and do not have a need to close the project. It handles it quite well :)
1

Vanilla Git

For merging the branch "cool" into the branch "main" do the following.

  1. git checkout cool
  2. git fetch origin
  3. git pull origin main

If there are merge conflicts, Git will show them. If possible, Git resolves them automatically. If not, it says so in the terminal.

To list the problem files in a clean way use this:

git diff --name-only --diff-filter=U --relative 

When you open one of these files (simply open them from the project). Git shows what part of the problem-content is yours and which isn't. <<<<< Head until ======= is yours. ======= until <<<<<< commit-hash is from the main branch.

  1. Go through all the problem files
  2. Inside a file, fix the conflict (take your content, or main-branch content, or merge them, ...)
  3. git add FILE_PATH (In IntelliJ for example, click the file in the project directory on the left of IntelliJ and hit control+shift+c to copy the file path)

After you did this with all problem files:

  1. git commit -m "Resolved merge conflicts"

  2. Optionally push: git push origin cool

Done.


Important: NEVER add all files to git while doing this (git add . or git add --all for example). This would bloat your MR with commits from main. Always only add single files as described above (the files where you fixed the merge conflict).

Comments

0

I follow the below process.

The process to fix a merge conflict:

  1. First, pull the latest from the destination branch to which you want to merge git pull origin develop

  2. As you get the latest from the destination, now resolve the conflict manually in an IDE by deleting those extra characters.

  3. Do a git add to add these edited files to the Git queue so that it can be commit and push to the same branch you are working on.

  4. As git add is done, do a git commit to commit the changes.

  5. Now push the changes to your working branch by git push origin HEAD

This is it and you will see it resolved in your pull request if you are using Bitbucket or GitHub.

Comments

-1

I like using WinMerge (free tool) that does both full entire directory tree comparison/merge and also individual file(s) comparison/merge of the full directory tree compare.

The Git merge conflict is telling you that your pull request will undo/lose/overwrite a coworker's changes, typically because your copy of the content wasn't recent enough.

Steps to resolve can be:

  • Take another new clone of the source to a newly named folder,
  • Use WinMerge to compare your content and the most recent content to understand the conflict,
  • For the file(s) changed by both yourself and your coworker that are causing the Git Merge conflict, look at the lines that your co-worker has added/changed/deleted as per compared to the code lines that you have added/changed/deleted.
  • Use the WinMerge left / right code section move arrows to ensure your coworker's work is in your copy of the file and you aren't clobbering their work.

I.e., no magic way to resolve Git merge conflicts other than manually looking at what each person has done to the same source file(s).

That is what I'm thinking.

Note: WinMerge creates .bak files .. and you don't want them copied to source control AzOps, TFS, etc., so if you are sure you have done the edit correctly, remove the .bak files.

Comments

-1

Well, all the answers already given seem to explain which tools you can use to detect merge conflicts or how to initiate a merge request...

The answer to your question however is both simple and frustrating. Merge conflicts are almost always to solve by hand manually. If you use a tool like e.g. GitLab, the GUI might help you to find differences in two code versions, but at the end of the day, you have to decide which line should be kept and which should be erased.

A simple example: Programmer A and programmer B both push the same - differently modified - file to a remote repository. Programmer A opens a merge request and GitLab highlights several lines of code where conflicts occur between the two versions. Now it is up to Programmer A and B to decide, who wrote better code in these specific lines. They have to make compromises.

Comments

-4

If you simply want to restore the remote master, then

git reset --hard origin/master 

WARNING: All local changes will be lost, see https://stackoverflow.com/a/8476004/11769765.

2 Comments

This is NOT what resolving merge conflicts mean!
But it overcomes the error message. Beginners may just want to undo their changes to "resolve merge conflicts". The warning is bold. Advanced users can fiddle with committing, pushing, fetching, adding, editing, merging, creating branches.
-5

If you do not use a tool to merge, first copy your code outside:

- `checkout master` - `git pull` / get new commit - `git checkout` to your branch - `git rebase master` 

It resolve conflict and you can copy your code.

1 Comment

Does that sequence always help? Please explain what and how it does and how that code answers the question.
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.