标签云

微信群

扫码加入我们

WeChat QR Code

如何解决合并冲突的蠢货吗?

Is there a good way to explain how to resolve merge conflicts in Git?


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

2018年06月24日01分21秒

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.

2018年06月24日01分21秒

Don't forget that you can mitigate most merge conflicts by regularly merging downstream!

2018年06月24日01分21秒

Also see git-tower.com/learn/git/ebook/command-line/tools-services/…

2018年06月25日01分21秒

This seems to be a detailed tutorial - githubtraining.com/fix-merge-conflict-git-using-sourcetree

2018年06月25日01分21秒

FYI you can use git mergetool -y to save a few keystrokes if you're merging a lot of files at once.

2018年06月24日01分21秒

Well, it doesn't necessarily open a GUI unless you install one. Running git mergetool for me resulted in vimdiff 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.

2018年06月24日01分21秒

Good point Josh. On ubuntu I've had the best luck with meld, its three way merge display isn't bad. On OSX git chose a nice default.

2018年06月24日01分21秒

This opened KDiff3. Which I have absolutely no clue how to use.

2018年06月25日01分21秒

I don't understand why this answer received so many upvotes, it isn't really very helpful as it only contains this one command and absolutely no explanation how to use it. As others said, it opened a vimdiff and even if I know how to use vim (switch the windows at least or close them) I don't even know what each window represent neither how to compare or accept the changes. It's nice to know there is such a command, but with no explanation of how to use it or install other 3rd tools, it's useless answer

2018年06月25日01分21秒

This was super helpful because I had a lot of merge errors with binary files (art assets) and merging those seems to always fail, so I need to overwrite it with the new file always and not "merge"

2018年06月25日01分21秒

Careful! The meaning of --ours and --theirs is reversed. --ours == the remote. --theirs == local. See git merge --help

2018年06月24日01分21秒

In my case, I confirm that --theirs = remote repository, --ours = my own local repository. It is the opposite of mmell comments.

2018年06月24日01分21秒

mmell Only on a rebase, apparently. See this question

2018年06月24日01分21秒

Guys, "ours" and "theirs" is relative to whether or not you are merging or rebasing. If you're merging, then "ours" means the branch you're merging into, and "theirs" is the branch you're merging in. When you're rebasing, then "ours" means the commits you're rebasing onto, while "theirs" refers to the commits that you want to rebase.

2018年06月25日01分21秒

The diff3 option is a great feature to have with merges. The only GUI I've come across that shows it is Perforce's p4merge, which can be installed and used separately from Perforce's other tools (which I've not used, but heard complaints about).

2018年06月24日01分21秒

This is the best tutorial I have found on Internet regarding "how to fix your merge conflicts"!

2018年06月25日01分21秒

This answer is the one that deserves the votes; not the above two which are flooded with votes;

2018年06月25日01分21秒

After a rebase attempt which resulted in a merge conflict: $ git log --merge -p build.xml output: fatal: --merge without MERGE_HEAD?

2018年06月24日01分21秒

what if I have changes on one file from branch1 and deletion of that file in branch2. How can I solve that merge conflict? Is there any way using git where I can merge them by keeping the changes of one branch?

2018年06月25日01分21秒

Justin Think of Git as tracking content rather than tracking files. Then it's easy to see that the content you've updated isn't in the repository and needs to be added. This way of thinking also explains why Git doesn't track empty folders: Although they are technically files, there isn't any content to track.

2018年06月25日01分21秒

content is there, conflict occurs because there 2 version of content. Therefore "git add" does not sound correct. And it does not work (git add, git commit) if you want commit only that one file after conflict was resolved ("fatal: cannot do a partial commit during a merge.")

2018年06月25日01分21秒

Yes, technically, this answers the question which as asked, but is not a usable answer, in my opinion, sorry. What's the point of making one branch the same as another? Of course a merge will have conflicts..

2018年06月24日01分21秒

Thulfir: who said anything about making one branch the same as another? There are different scenarios where you need to merge, without "making one branch the same as another". One is when you're done with a development branch and want to incorporate its changes into the master branch; after this, the development branch can be deleted. Another one is when you want to rebase your development branch, in order to ease the eventual final merge into the master.

2018年06月24日01分21秒

JustinGrant git add stages files in the index; it does not add anything to the repository. git commit adds things to the repository. This usage makes sense for merges -- the merge automatically stages all of the changes that can be merged automatically; it is your responsibility to merge the rest of the changes and add those to the index when you are done.

2018年06月25日01分21秒

Also check out the "-m" option to "git checkout -m" - it allows you to extract the different flies back out into your workspace

2018年06月25日01分21秒

This saved me. Looking at each file separately allowed me to remember what I was going for in each branch. Then I could make the decision to choose.

2018年06月25日01分21秒

Using "git add" is the real trick here. You may not even want to commit (maybe you want to stash), but you have to do "git add" to complete the merge. I think mergetool does the add for you (although it isn't in the manpage), but if you do the merge manually, you need to use "git add" to complete it (even if you don't want to commit).

2018年06月25日01分21秒

git checkout . --theirs resolved my problem thanks

2018年06月24日01分21秒

if you prefer to resolve conflicts manually try opening the folder in Visual Studio Code, it marks files with conflicts and colors conflict lines inside every one

2018年06月25日01分21秒

Hey, I kinda understood your answer. But since i'm new to github merge conflicts, I think there is something missing. What happens to your local modifications when you do git checkout master and git fetch and git rebase --hard origin/master

2018年06月24日01分21秒

I believe you should add more details on what to do. Another example which is confusing me, you mentioned in your answer: we do git add ., will it save our local modifications so we can follow up with git checkout master ? or are they two different scenarios ?

2018年06月24日01分21秒

this is not asking about github thus I down voted what I view to be a very poor answer.

2018年06月25日01分21秒

thanks for keeping me in check, man.

2018年06月25日01分21秒

mschuett is right, the question is "how to resolve conflicts in git", not "how to resolve conflicts in github". There is a difference and there is already far too many people that think git and github are the same thing, so anything that propagate that feeling is wrong.

2018年06月24日01分21秒

In my case this didn't resolve merge conflicts well, since for some reason it kept duplicate lines of config in C# projects. Though it was more friendly than ENTIRE FILE IS DIFFERENT, which I had before

2018年06月24日01分21秒

Worked for me! Thanks!

2018年06月24日01分21秒

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 :)

1970年01月01日00分03秒