Reputation: 121840
How do I resolve merge conflicts in my Git repository?
Upvotes: 5424
Views: 3709980
Reputation: 4812
You can try GitLens for Visual Studio Code. The key features are:
I already like this feature:
And there are many features. You can check them here.
Upvotes: 13
Reputation: 45361
Try:
git mergetool
It opens a GUI that steps you through each conflict, and you get to choose how to merge. Sometimes it requires a bit of hand editing afterwards, but usually it's enough by itself. It is much better than doing the whole thing by hand certainly.
As per Josh Glover's comment:
[This command] doesn't necessarily open a GUI unless you install one. Running
git mergetool
for me resulted invimdiff
being used. You can install one of the following tools to use it instead:meld
,opendiff
,kdiff3
,tkdiff
,xxdiff
,tortoisemerge
,gvimdiff
,diffuse
,ecmerge
,p4merge
,araxis
,vimdiff
,emerge
.
Below is a sample procedure using vimdiff
to resolve merge conflicts, based on this link.
Run the following commands in your terminal
git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
This will set vimdiff
as the default merge tool.
Run the following command in your terminal
git mergetool
You will see a vimdiff
display in the following format:
╔═══════╦══════╦════════╗
║ ║ ║ ║
║ LOCAL ║ BASE ║ REMOTE ║
║ ║ ║ ║
╠═══════╩══════╩════════╣
║ ║
║ MERGED ║
║ ║
╚═══════════════════════╝
These 4 views are
You can navigate among these views using ctrl+w. You can directly reach the MERGED view using ctrl+w followed by j.
You can edit the MERGED view like this:
If you want to get changes from REMOTE
:diffg RE
If you want to get changes from BASE
:diffg BA
If you want to get changes from LOCAL
:diffg LO
Save, Exit, Commit, and Clean up
:wqa
save and exit from vi
git commit -m "message"
git clean
Remove extra files (e.g. *.orig
). Warning: It will remove all untracked files, if you won't pass any arguments.
Upvotes: 3437
Reputation: 52907
I understood what a merge conflict was, but when I saw the output of git diff
, it looked like nonsense to me at first:
git diff
++<<<<<<< HEAD
+ display full last name boolean in star table
++=======
+ users viewer.id/star.id, and conversation uses user.id
+
++>>>>>>> feat/rspec-tests-for-cancancan
But here is what helped me:
Everything between <<<<<<<
and =======
is what was in one file, and
Everything between =======
and >>>>>>>
is what was in the other file
So literally all you have to do is open the file with the merge conflicts and remove those lines from either branch (or just make them the same), and the merge
will immediately succeed. Problem solved!
Upvotes: 14
Reputation:
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.
Upvotes: 0
Reputation: 2298
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:
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.
Upvotes: 0
Reputation: 1321
I am using Microsoft's Visual Studio Code for resolving conflicts. It's very simple to use. I keep my project open in the workspace. It detects and highlights conflicts. Moreover, it gives GUI options to select whatever change I want to keep from HEAD or incoming.
Upvotes: 8
Reputation: 282
Try Visual Studio Code for editing if you aren't already.
After you try merging (and land up in merge conflicts), Visual Studio Code automatically detects the merge conflicts.
It can help you very well by showing the changes made to the original one and if you should accept incoming
or
current change
(meaning original one before merging)'.
It helped me and it can work for you too!
PS: It will work only if you've configured Git with with your code and Visual Studio Code.
Upvotes: 4
Reputation: 1583
This answer is to add an alternative for those Vim users like me that prefers to do everything within the editor.
Tpope came up with this great plugin for Vim called fugitive. Once installed, you can run :Gstatus
to check the files that have conflict and :Gdiff
to open Git in a three-way merge.
Once in the three-way merge, fugitive will let you get the changes of any of the branches you are merging in the following fashion:
:diffget //2
, get changes from original (HEAD) branch::diffget //3
, get changes from merging branch:Once you are finished merging the file, type :Gwrite
in the merged buffer.
Vimcasts released a great video explaining these steps in detail.
Upvotes: 8
Reputation: 21698
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 develop
As you get the latest from the destination, now resolve the conflict manually in an IDE by deleting those extra characters.
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.
As git add
is done, do a git commit
to 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.
Upvotes: 1
Reputation: 1081
If you are using IntelliJ IDEA as the IDE, try to merge the parent to your branch by:
git checkout <localbranch>
git merge origin/<remotebranch>
It will show all conflicts like this:
A_MBPro:test anu$ git merge origin/ Auto-merging src/test/java/com/.../TestClass.java CONFLICT (content): Merge conflict in src/test/java/com/.../TestClass.java
Now note that the file TestClass.java is shown in red in IntelliJ IDEA.
Also git status
will show:
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: src/test/java/com/.../TestClass.java
Open the file in IntelliJ IDEA. It will have sections with
<<<<<<< HEAD
public void testMethod() {
}
=======
public void testMethod() { ...
}
>>>>>>> origin/<remotebranch>
where HEAD is changes on your local branch and origin/<remotebranch> is changes from the remote branch. Here keep the stuff that you need and remove the stuff you don't need. After that, the normal steps should do. That is
git add TestClass.java
git commit -m "commit message"
git push
Upvotes: 4
Reputation: 1714
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.
Upvotes: 3
Reputation: 599
git fetch <br>
git checkout **your branch**<br>
git rebase master<br>
In this step you will try to fix the conflict using your preferred IDE.
You can follow this link to check how to fix the conflict in the file.
git add<br>
git rebase --continue<br>
git commit --amend<br>
git push origin HEAD:refs/drafts/master (push like a drafts)<br>
Now everything is fine and you will find your commit in Gerrit.
Upvotes: 6
Reputation: 1745
There are three steps:
Find which files cause conflicts by the command
git status
Check the files, in which you would find the conflicts marked like
<<<<<<<<head
blablabla
Change it to the way you want it, and then commit with the commands
git add solved_conflicts_files
git commit -m 'merge msg'
Upvotes: 34
Reputation: 46046
Merge conflicts could occur in different situations:
git fetch
and then git merge
git fetch
and then git rebase
git pull
(which is actually equal to one of the above-mentioned conditions)git stash pop
You need to install a merge tool which is compatible with Git to resolve the conflicts. I personally use KDiff3, and I've found it nice and handy. You can download its Windows version here:
https://sourceforge.net/projects/kdiff3/files/
BTW, if you install Git Extensions there is an option in its setup wizard to install Kdiff3.
Then setup the Git configuration to use KDiff3 as its mergetool:
$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false
$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false
(Remember to replace the path with the actual path of the KDiff3 EXE file.)
Then every time you come across a merge conflict, you just need to run this command:
$ git mergetool
Then it opens Kdiff3, and first tries to resolve the merge conflicts automatically. Most of the conflicts would be resolved spontaneously and you need to fix the rest manually.
Here's what Kdiff3 looks like:
Then once you're done, save the file and it goes to the next file with a conflict and you do the same thing again until all the conflicts are resolved.
To check if everything is merged successfully, just run the mergetool command again. You should get this result:
$ git mergetool
No files need merging
Upvotes: 15
Reputation: 40247
In speaking of pull/fetch/merge in the previous answers, I would like to share an interesting and productive trick,
git pull --rebase
This above command is the most useful command in my Git life which saved a lot of time.
Before pushing your newly committed change to remote server, try git pull --rebase
rather git pull
and manual merge
and it will automatically sync the latest remote server changes (with a fetch + merge) and will put your local latest commit at the top in the Git log. No need to worry about manual pull/merge.
In case of a conflict, just use
git mergetool
git add conflict_file
git rebase --continue
Find details at: What does “git pull –rebase” do?
Upvotes: 38
Reputation: 1271
I always follow the below steps to avoid conflicts.
git checkout master
(Come to the master branch)git pull
(Update your master to get the latest code)git checkout -b mybranch
(Check out a new a branch and start working on that branch so that your master always remains top of trunk.)git add .
and git commit
and git push (on your local branch after your changes)git checkout master
(Come back to your master)Now you can do the same and maintain as many local branches you want and work simultaneous by just doing a git checkout
to your branch whenever necessary.
Upvotes: 14
Reputation: 77122
Here's a probable use case, from the top:
You're going to pull some changes, but oops, you're not up to date:
git fetch origin
git pull origin master
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.
So you get up-to-date and try again, but have a conflict:
git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.
So you decide to take a look at the changes:
git mergetool
Oh my, oh my, upstream changed some things, but just to use my changes...no...their changes...
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"
And then we try a final time
git pull origin master
From ssh://[email protected]:22/projectname
* branch master -> FETCH_HEAD
Already up-to-date.
Ta-da!
Upvotes: 1807
Reputation: 13015
If you want to merge from branch test
to master
, you can follow these steps:
Step 1: Go to the branch
git checkout test
Step 2:
git pull --rebase origin master
Step 3: If there are some conflicts, go to these files to modify it.
Step 4: Add these changes
git add #your_changes_files
Step 5:
git rebase --continue
Step 6: If there is still conflict, go back to step 3 again. If there is no conflict, do following:
git push origin +test
Step 7: And then there is no conflict between test and master. You can use merge directly.
Upvotes: 27
Reputation: 5739
patience
For a big merge conflict, using patience
provided good results for me. It will try to match blocks rather than individual lines.
If you change the indentation of your program for instance, the default Git merge strategy sometimes matches single braces {
which belongs to different functions. This is avoided with patience
:
git merge -s recursive -X patience other-branch
From the documentation:
With this option, merge-recursive spends a little extra time to avoid
mismerges that sometimes occur due to unimportant matching lines
(e.g., braces from distinct functions). Use this when the branches to
be merged have diverged wildly.
If you have a merge conflict and want to see what others had in mind when modifying their branch, it's sometimes easier to compare their branch directly with the common ancestor (instead of our branch). For that you can use merge-base
:
git diff $(git merge-base <our-branch> <their-branch>) <their-branch>
Usually, you only want to see the changes for a particular file:
git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>
Upvotes: 21
Reputation: 3762
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.
Upvotes: -4
Reputation: 166879
Merge conflicts happens when changes are made to a file at the same time. Here is how to solve it.
git
CLIHere are simple steps what to do when you get into conflicted state:
git status
(under Unmerged paths
section).Solve the conflicts separately for each file by one of the following approaches:
Use GUI to solve the conflicts: git mergetool
(the easiest way).
To accept remote/other version, use: git checkout --theirs path/file
. This will reject any local changes you did for that file.
To accept local/our version, use: git checkout --ours path/file
However you've to be careful, as remote changes that conflicts were done for some reason.
Related: What is the precise meaning of "ours" and "theirs" in git?
Edit the conflicted files manually and look for the code block between <<<<<
/>>>>>
then choose the version either from above or below =====
. See: How conflicts are presented.
Path and filename conflicts can be solved by git add
/git rm
.
Finally, review the files ready for commit using: git status
.
If you still have any files under Unmerged paths
, and you did solve the conflict manually, then let Git know that you solved it by: git add path/file
.
If all conflicts were solved successfully, commit the changes by: git commit -a
and push to remote as usual.
See also: Resolving a merge conflict from the command line at GitHub
For practical tutorial, check: Scenario 5 - Fixing Merge Conflicts by Katacoda.
I've successfully used DiffMerge which can visually compare and merge files on Windows, macOS and Linux/Unix.
It graphically can show the changes between 3 files and it allows automatic merging (when safe to do so) and full control over editing the resulting file.
Image source: DiffMerge (Linux screenshot)
Simply download it and run in repo as:
git mergetool -t diffmerge .
On macOS you can install via:
brew install caskroom/cask/brew-cask
brew cask install diffmerge
And probably (if not provided) you need the following extra simple wrapper placed in your PATH (e.g. /usr/bin
):
#!/bin/sh
DIFFMERGE_PATH=/Applications/DiffMerge.app
DIFFMERGE_EXE=${DIFFMERGE_PATH}/Contents/MacOS/DiffMerge
exec ${DIFFMERGE_EXE} --nosplash "$@"
Then you can use the following keyboard shortcuts:
Alternatively you can use opendiff (part of Xcode Tools) which lets you merge two files or directories together to create a third file or directory.
Upvotes: 130
Reputation: 1435
I either want my or their version in full, or want to review individual changes and decide for each of them.
Fully accept my or theirs version:
Accept my version (local, ours):
git checkout --ours -- <filename>
git add <filename> # Marks conflict as resolved
git commit -m "merged bla bla" # An "empty" commit
Accept their version (remote, theirs):
git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"
If you want to do for all conflict files run:
git merge --strategy-option ours
or
git merge --strategy-option theirs
Review all changes and accept them individually
git mergetool
git add <filename>
git commit -m "merged bla bla"
Default mergetool
works in command line. How to use a command line mergetool should be a separate question.
You can also install visual tool for this, e.g. meld
and run
git mergetool -t meld
It will open local version (ours), "base" or "merged" version (the current result of the merge) and remote version (theirs). Save the merged version when you are finished, run git mergetool -t meld
again until you get "No files need merging", then go to Steps 3. and 4.
Upvotes: 63
Reputation: 4015
Simply, if you know well that changes in one of the repositories is not important, and want to resolve all changes in favor of the other one, use:
git checkout . --ours
to resolve changes in the favor of your repository, or
git checkout . --theirs
to resolve changes in favor of the other or the main repository.
Or else you will have to use a GUI merge tool to step through files one by one, say the merge tool is p4merge
, or write any one's name you've already installed
git mergetool -t p4merge
and after finishing a file, you will have to save and close, so the next one will open.
Upvotes: 36
Reputation: 1104
Please follow the following steps to fix merge conflicts in Git:
Check the Git status: git status
Get the patchset: git fetch (checkout the right patch from your Git commit)
Checkout a local branch (temp1 in my example here): git checkout -b temp1
Pull the recent contents from master: git pull --rebase origin master
Start the mergetool and check the conflicts and fix them...and check the changes in the remote branch with your current branch: git mergetool
Check the status again: git status
Delete the unwanted files locally created by mergetool, usually mergetool creates extra file with *.orig extension. Please delete that file as that is just the duplicate and fix changes locally and add the correct version of your files. git add #your_changed_correct_files
Check the status again: git status
Commit the changes to the same commit id (this avoids a new separate patch set): git commit --amend
Push to the master branch: git push (to your Git repository)
Upvotes: 33
Reputation: 598
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.
Upvotes: -5
Reputation: 2381
As of December 12th 2016, you can merge branches and resolve conflicts on github.com
Thus, if you don't want to use the command-line or any 3rd party tools that are offered here from older answers, go with GitHub's native tool.
This blog post explains in detail, but the basics are that upon 'merging' two branches via the UI, you will now see a 'resolve conflicts' option that will take you to an editor allowing you to deal with these merge conflicts.
Upvotes: 17
Reputation: 24814
A safer way to resolve conflicts is to use git-mediate (the common solutions suggested here are quite error prone imho).
See this post for a quick intro on how to use it.
Upvotes: 3
Reputation: 7497
git log --merge -p [[--] path]
Does not seem to always work for me and usually ends up displaying every commit that was different between the two branches, this happens even when using --
to separate the path from the command.
What I do to work around this issue is open up two command lines and in one run
git log ..$MERGED_IN_BRANCH --pretty=full -p [path]
and in the other
git log $MERGED_IN_BRANCH.. --pretty=full -p [path]
Replacing $MERGED_IN_BRANCH
with the branch I merged in and [path]
with the file that is conflicting. This command will log all the commits, in patch form, between (..
) two commits. If you leave one side empty like in the commands above git will automatically use HEAD
(the branch you are merging into in this case).
This will allow you to see what commits went into the file in the two branches after they diverged. It usually makes it much easier to solve conflicts.
Upvotes: 18
Reputation: 24891
Identify which files are in conflict (Git should tell you this).
Open each file and examine the diffs; Git demarcates them. Hopefully it will be obvious which version of each block to keep. You may need to discuss it with fellow developers who committed the code.
Once you've resolved the conflict in a file git add the_file
.
Once you've resolved all conflicts, do git rebase --continue
or whatever command
Git said to do when you completed.
Upvotes: 375
Reputation: 16315
If you're making frequent small commits, then start by looking at the commit comments with git log --merge
. Then git diff
will show you the conflicts.
For conflicts that involve more than a few lines, it's easier to see what's going on in an external GUI tool. I like opendiff -- Git also supports vimdiff, gvimdiff, kdiff3, tkdiff, meld, xxdiff, emerge out of the box and you can install others: git config merge.tool "your.tool"
will set your chosen tool and then git mergetool
after a failed merge will show you the diffs in context.
Each time you edit a file to resolve a conflict, git add filename
will update the index and your diff will no longer show it. When all the conflicts are handled and their files have been git add
-ed, git commit
will complete your merge.
Upvotes: 82