Is there a good way to explain how to resolve merge conflicts in Git?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
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
and in the other
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 useHEAD
(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.
For those who are using Visual Studio (2015 in my case)
Close your project in VS. Especially in big projects VS tends to freak out when merging using the UI.
Do the merge in command prompt.
git checkout target_branch
git merge source_branch
Then open the project in VS 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, Take Target. The merge tool in VS is very easy to use.
Using
patience
I'm surprised no one else spoke about resolving conflict using
patience
with the merge recursive strategy. For a big merge conflict, usingpatience
provided good results for me. The idea is that 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 withpatience
:From the documentation:
Comparison with the common ancestor
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
:Usually, you only want to see the changes for a particular file:
Merge conflicts could occur in different situations:
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 git configs to use Kdiff as its mergetool:
(Remember to replace the path with the actual path of Kdiff exe file.)
Then every time you come across a merge conflict you just need to run this command:
Then it opens the 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 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:
I follow the below process.
The process to fix 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 IDE by deleting those extra characters.
Do a
git add
to add these edited files to the git queue so that it can becommit
andpush
to the same branch you are working on.As
git add
is done, do agit 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.
Here's a probable use-case, from the top:
You're going to pull some changes, but oops, you're not up to date:
So you get up-to-date and try again, but have a conflict:
So you decide to take a look at the changes:
Oh me, oh my, upstream changed some things, but just to use my changes...no...their changes...
And then we try a final time
Ta-da!