How do I resolve merge conflicts in my Git repository?
- 43The 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 Gitmwilliams– mwilliams2008-10-02 11:40:03 +00:00Commented Oct 2, 2008 at 11:40
- 4You 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.Grady G Cooper– Grady G Cooper2015-04-18 05:37:12 +00:00Commented Apr 18, 2015 at 5:37
- Don't forget that you can mitigate most merge conflicts by regularly merging downstream!Ant P– Ant P2015-07-27 09:50:33 +00:00Commented Jul 27, 2015 at 9:50
- 3Also see git-tower.com/learn/git/ebook/command-line/tools-services/…Pacerier– Pacerier2015-10-20 11:19:55 +00:00Commented 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/245966jakub.g– jakub.g2016-08-25 08:48:56 +00:00Commented Aug 25, 2016 at 8:48
37 Answers
For those who are using Visual Studio (Visual Studio 2015 in my case)
Close your project in Visual Studio. Especially in big projects, Visual Studio tends to freak out when merging using the UI.
Do the merge in a command prompt.
git checkout target_branch
git merge source_branch
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.
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.
1 Comment
Vanilla Git
For merging the branch "cool" into the branch "main" do the following.
git checkout coolgit fetch origingit 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.
- Go through all the problem files
- Inside a file, fix the conflict (take your content, or main-branch content, or merge them, ...)
git add FILE_PATH(In IntelliJ for example, click the file in the project directory on the left of IntelliJ and hitcontrol+shift+cto copy the file path)
After you did this with all problem files:
git commit -m "Resolved merge conflicts"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
I follow the below process.
The process to fix a merge conflict:
First, pull the latest from the destination branch to which you want to merge
git pull origin developAs you get the latest from the destination, now resolve the conflict manually in an IDE by deleting those extra characters.
Do a
git addto add these edited files to the Git queue so that it can becommitandpushto the same branch you are working on.As
git addis done, do agit committo commit the changes.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
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
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
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
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.