Is there a good way to explain how to resolve merge conflicts in Git?
|
Try: 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 @JoshGlover comment: The command doesn't necessarily open a GUI unless you install one. Running |
|||||||||||||||||||||
|
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! |
|||||||||||||||||||||
|
I find merge tools rarely help me understand the conflict or the resolution. I'm usually more successful looking at the conflict markers in a text editor and using git log as a supplement. Here are a few tips: Tip OneThe best thing I have found is to use the "diff3" merge conflict style:
This produces conflict markers like this:
The middle section is what the common ancestor looked like. This is useful because you can compare it to the top and bottom versions to get a better sense of what was changed on each branch, which gives you a better idea for what the purpose of each change was. If the conflict is only a few lines, this generally makes the conflict very obvious. (Knowing how to fix a conflict is very different; you need to be aware of what other people are working on. If you're confused, it's probably best to just call that person into your room so they can see what you're looking at.) If the conflict is longer, then I will cut and paste each of the three sections into three separate files, such as "mine", "common" and "theirs". Then I can run the following commands to see the two diff hunks that caused the conflict:
This is not the same as using a merge tool, since a merge tool will include all of the non-conflicting diff hunks too. I find that to be distracting. Tip TwoSomebody already mentioned this, but understanding the intention behind each diff hunk is generally very helpful for understanding where a conflict came from and how to handle it.
This shows all of the commits that touched that file in between the common ancestor and the two heads you are merging. (So it doesn't include commits that already exist in both branches before merging.) This helps you ignore diff hunks that clearly are not a factor in your current conflict. Tip ThreeVerify your changes with automated tools. If you have automated tests, run those. If you have a lint, run that. If it's a buildable project, then build it before you commit, etc. In all cases, you need to do a bit of testing to make sure your changes didn't break anything. (Heck, even a merge without conflicts can break working code.) Tip FourPlan ahead; communicate with co-workers. Planning ahead and being aware of what others are working on can help prevent merge conflicts and/or help resolve them earlier -- while the details are still fresh in mind. For example, if you know that you and another person are both working on different refactoring that will both affect the same set of files, you should talk to each other ahead of time and get a better sense for what types of changes each of you is making. You might save considerable time and effort if you conduct your planned changes serially rather than in parallel. For major refactorings that cut across a large swath of code, you should strongly consider working serially: everybody stops working on that area of the code while one person performs the complete refactoring. If you can't work serially (due to time pressure, maybe), then communicating about expected merge conflicts at least helps you solve the problems sooner while the details are still fresh in mind. For example, if a co-worker is making a disruptive series of commits over the course of a one-week period, you may choose to merge/rebase on that co-workers branch once or twice each day during that week. That way, if you do find merge/rebase conflicts, you can solve them more quickly than if you wait a few weeks to merge everything together in one big lump. Tip FiveIf you're unsure of a merge, don't force it. Merging can feel overwhelming, especially when there are a lot of conflicting files and the conflict markers cover hundreds of lines. Often times when estimating software projects we don't include enough time for overhead items like handling a gnarly merge, so it feels like a real drag to spend several hours dissecting each conflict. In the long run, planning ahead and being aware of what others are working on are the best tools for anticipating merge conflicts and prepare yourself to resolve them correctly in less time. |
|||||||||||||||||
|
|
|||||||||||||||||||||
|
Check out the answers in Stack Overflow question Aborting a merge in Git, especially Charles Bailey's answer which shows how to view the different versions of the file with problems, for example,
|
|||||||||
|
If you're making frequent small commits, then start by looking at the commit comments with 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: Each time you edit a file to resolve a conflict, |
|||||
|
See How Conflicts Are Presented or, in Git, the Also, the How to Resolve Conflicts section explains how to resolve the conflicts:
You can also read about merge conflict markers and how to resolve them in the Pro Git book section Basic Merge Conflicts. |
||||
|
For Emacs users which want to resolve merge conflicts semi-manually:
shows all files which require conflict resolution. Open each of those files one by one, or all at once by:
When visiting a buffer requiring edits in Emacs, type
This will open three buffers (mine, theirs, and the output buffer). Navigate by pressing 'n' (next region), 'p' (prevision region). Press 'a' and 'b' to copy mine or theirs region to the output buffer, respectively. And/or edit the output buffer directly. When finished: Press 'q'. Emacs asks you if you want to save this buffer: yes. After finishing a buffer mark it as resolved by running from the teriminal:
When finished with all buffers type
to finish the merge. |
||||
|
Merge conflicts happens when changes are made to a file at the same time. Here is how to solve it.
|
Please follow the following steps to fix merge conflicts in git:
|
||||
|
You could fix merge conflicts in a number of ways as other have detailed. I think the real key is knowing how changes flow with local and remote repositories. The key to this is understanding tracking branches. I have found that I think of the tracking branch as the 'missing piece in the middle' between me my local, actual files directory and the remote defined as origin. I've personally got into the habit of 2 things to help avoid this. Instead of:
Which has two drawbacks - a) All new/changed files get added and that might include some unwanted changes. So instead I do:
This way you are more deliberate about which files get added and you also get to review the list and think a bit more while using the editor for the message. I find it also improves my commit messages when I use a full screen editor rather than the [Update - as time has passed I've switched more to:
] Also (and more relevant to your situation), I try to avoid:
or
because pull implies a merge and if you have changes locally that you didn't want merged you can easily end up with merged code and/or merge conflicts for code that shouldn't have been merged. Instead I try to do
You may also find this helpful: git branch, fork, fetch, merge, rebase and clone, what are the differences? |
||||
|
CoolAJ86's answer sums up pretty much everything. In case you have changes in both branches in the same piece of code you will have to do a manual merge. Open the file in conflict in any text editor and you should see following structure.
Choose one of the alternatives or a combination of both in a way that you want new code to be, while removing equal signs and angle brackets.
|
||||
|
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 What I do to work around this issue is open up two command lines and in one run
and in the other
Replacing 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. |
||||
|
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, so use:
to resolve changes in the favor of your repository, or
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
and after finishing a file, you will have to save and close, so the next one will open |
|||||||||
|
Bonus:In speaking of pull/fetch/merge in the above answers, I would like to share an interesting and productive trick,
|
I always follow the below steps to avoid conflicts.
Now you can do the same and maintain as many local branches you want and work simultaneous my just doing a git checkout to your branch when ever necessary. |
|||
|
if you want to merge from branch(test) to master, you can follow these steps: Step1: go to the branch
Step2: Step3: if there are some conflicts, go to these files to modify it. Step4: add these changes
Step5: Step6: if there is still conflict, go back to Step3 again. If there is no conflict, do following: Step7: and then there is no conflict between test and master. you can use merge directly. |
|||
|
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 :
|
||||
|
if you not use tool to merge, first copy your code outsite
it resolve conflict and you can copy your code. |
|||||
|
protected by Will Dec 17 '10 at 13:56
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?