git merge repeating conflicts
Asked Answered
C

2

14

I have a branch master and a branch feature which was checked out from master.

master gets commits pushed into it from other developers and I regularly merge master back into feature, so that it keeps up with new developments from other branches.

However, I keep getting the same conflicts in the same files every time I do a git merge master, although I resolved the conflict before and committed the changes.

Any ideas on what could be the cause for this and how to prevent it?

Thanks!

Chiropody answered 17/9, 2012 at 10:35 Comment(7)
It doesn't sound right. Are you sure that these files are not changing?Jayme
All developers push on the same branch ?! I think you should make a dedicated branch for each developer. By the way, you mean, conflicts appear again ?! Source files are changed again ?! Your commits have gone ?! Do you mean this ? Or conflicts appear in new files pushed.Friedman
@kan: the files are changing but not in the lines that cause the conflict. In one case, after going through all the conflicting lines in the file and git adding it again, git even removed it from the index because it was exactly the same as the one already present.Chiropody
@KamranAmini: Each developer uses different branches for different features but they obviously end up pushing them into master. Sorry, I probably wasn't clear enough about that the first time. The commits are not gone and some of the source files in master are changed again after the previous merge. Which leads me to think that this might be the cause for the problem... I would expect git to be smart enough to see that the conflicting part of the file has not been changed and thus it wouldn't mark it as a conflict, though...Chiropody
I don't understand anyway, if you merged once, it doesn't merge the same again. Conflicts could happen only with new changes made after the merge. Or do you do rebase or stash?Jayme
No rebase or stashing. But apparently it's not that uncommon. So much that git has a tool to deal with this. See the link in ouah's answer below.Chiropody
@Chiropody Maybe I am wrong, but the rerere is not very common actually, it is handy only if you merge different but similar branches differently or doing rebases a lot. The normal branch-merge workflow doesn't need it.Jayme
J
11

First consider not merging integration branches into feature branches.

Second have a look to git rerere command and use it:

$ git config --global rerere.enabled true

NAME

  git-rerere - Reuse recorded resolution of conflicted merges

DESCRIPTION

  In a workflow employing relatively long lived topic branches,
  the developer sometimes needs to resolve the same conflicts
  over and over again until the topic branches are done (either
  merged to the "release" branch, or sent out and accepted upstream)
Jehol answered 17/9, 2012 at 10:39 Comment(3)
That looks very useful. I'll give that a try next time I try this particular branch merge. Thanks!Chiropody
By the way, you suggested not merging integration branches into feature branches. In principle it sounds good (I didn't use to do it until recently) but how do you deal with long term branches? Do you merge them only at the very end, when you're done with them? That sometimes means a lot of changes, which is dangerous and is exactly what we're trying to avoid by regularly merging master into feature.Chiropody
You maybe want rebase feature onto a newer master instead, if your intent is that feature's changes are a few additions to master.Moralist
T
0

This was mentioned by mike but it deserves it's own answer I think

rerere is a good solution, but there is one, that I consider superior: Use git rebase.

Your feature branch knows nothing about about the merge to master, so when you merge it again, it encounters the same conflicts again.

Instead rebase your feature branch onto master, then do a fast-forward merge on master. The conflict resolution now happens during the rebase which is in the common past of master and feature, and you don't see any conflicts anymore.

Tauto answered 22/8 at 6:55 Comment(1)
Yeah, I have since drastically changed the way I use Git (it's been 12 years since I asked this question - you're doing some archaeology here ;) ) and in fact, shortly after this, I realised how stupid it is in general to merge master into feature branches, so I never did it again.Chiropody

© 2022 - 2024 — McMap. All rights reserved.