3

I have looked at similar stackoverflow questions, but none provided a working solution for me.

On the current project I am working on, we have a bunch of branches:

  • master: features get pushed on there once they are proven to be stable
  • X feature branches: each developer branches off of master, works on their feature, and pull requests it once done.

The problem is that we're currently undergoing huge refactoring of the code, meaning that regardless what feature someone is working on, it is likely that two or more people are touching the same method in the same file at the same time in a different branch.

Therefore, if another branch having worked on the same files I did pull requests before I do, my branch will now be behind the master branch.

Due to this, I pull from master into my branch to get the newest updates and resolve merge conflicts before continuing to work on my own feature/refactoring or pull requesting.

However, and here is the problem, git does not seem to show me all the merge conflicts that are actually there. To take a simple example, let's just assume that in my own branch I renamed a class A to NewA, and updated all references accordingly. Now me and person B are both touching a method Z, me mainly refactoring it, person Z just adding a few fixes to known problems. When merging, some conflicts, where the code is different, get shown - but it feels as if others don't, because all my NewA references have been reverted back to A, without git telling me about it.

For the record, when merging I do something like this:

commit&push all my changes to my branch
git pull origin master

Now, if it was just small changes like that that wouldn't even compile everything would be fine, but I'm assuming that if git does not warn me about overwriting these things, it won't warn me about overwriting other things either, and I therefore have no clue what I've lost.

TL;DR: Is there a way for stopping git from overwriting changes I made without telling me? (I'm aware I can force my changes to take priority, but I don't want to do that either. I need to review each change manually. And I can't cherry-pick commits either, as I need to always be working on a base version of master that is up-to-date.)

Edit: When I run both the master version of the file and the file from my branch through a random online diff tool, it CLEARLY shows me all the differences there are and doesn't just pretend to show some while actually overriding stuff. Why can't git just do the same with stuff that should clearly be overwritten by my version of the code, instead of overwriting it with the old version?

We usually don't have this problem since we're working on different features and files, but since I'm in the process of refactoring everything I can't avoid touching files other people are touching as well.

Fly
  • 810
  • 2
  • 9
  • 28
  • This might be related, though it does not seem to be the whole story: https://stackoverflow.com/questions/54480730/git-merge-doesnt-report-merge-conflict-and-picks-unexpected-end-result/54481842#54481842 – joanis Jul 05 '19 at 13:55
  • In any case, the diff command I suggest there might help you figure out what the problem is. The fact that `git merge` only looks at the merge base and the two end points is probably be the source of your problem. – joanis Jul 05 '19 at 14:01
  • Look at a three way diff between `git merge-base master feature-branch`, `master` and `feature-branch`, and keep in mind that Git keeps anything that has changed from the merge base to either branch. Conflicts are detected only if a block is changed on both sides. E.g., using meld: https://stackoverflow.com/a/55831128/3216427 – joanis Jul 05 '19 at 14:04
  • @joanis It's not quite the same problem, but it does seem similar. I'll check out the diff command, thanks (Stupid question, but how do I execute the two lines from your answer in a VSCode CLI?). Git is so complicated... But at this point if I can't manage to resolve the problem above git literally hinders productivity instead of helping it, which is quite a pain – Fly Jul 05 '19 at 14:07
  • @joanis Also, here's a quick and shorter recap of the commits described in my post: I create branch from master at day 0. I edit file A. Other people also create branches from master at day 0 and edit file A, but not even necessarily the same place. Other people's pull requests get accepted at day 1. I'm now at day 2, merging the new master into my branch. Instead of having my newer changes to file A override old master versions, master overrides mine without telling me anything – Fly Jul 05 '19 at 14:10
  • I don't know how to run any of these commands in VSCode. I suggest using bash instead, where my linked commands will work. As for Git hindering productivity, I'm afraid you're blaming the messenger here: the refactoring you're describing is very complicated and error prone, no matter what tools you use. – joanis Jul 05 '19 at 14:26
  • From your recap, it's hard to tell what should happen, but I think the three way diff is reasonably likely to explain why things did happen the way they did. – joanis Jul 05 '19 at 14:27
  • Sorry, just realized the meld command is for just one file at a time, maybe not what you want. I have a meeting now, but start with `MERGE_BASE=\`git merge-base master feature\`; git diff $MERGE_BASE master;` in one window, and `git diff $MERGE_BASE feature` in another. I'm sure the three way diff exists somehow, but I don't have time to look it up just now. – joanis Jul 05 '19 at 14:32
  • 1
    @joanis Thank you for your time, I really appreciate the help. I know I shouldn't blame git for this but rather myself for not knowing git properly, it's just a bit frustrating because the result I want seems to be so easy, yet would take ages to go through manually, haha. I just tried running those commands, and, well - they don't give me any output? Maybe I'm missing something here, but when I just do git diff master release it at least shows me what it thinks the diffs are, but the $MERGE_BASE diffs don't output anything? – Fly Jul 05 '19 at 14:37
  • OK, when you run `git merge-base master feature`, do you at least get a different commit than either `master` or `feature`? Just manually cut and paste that commit instead of using the variable, that should work. Oh, just thought, if you already merged, you'll want to do it on the last commits before the merge, if that wasn't obvious. – joanis Jul 05 '19 at 16:31
  • This may be helpful: https://stackoverflow.com/a/41886622/3216427 although the method suggested seems complicated, I think it does what you want. The question was a problem similar to yours I think, because they wanted to see all diffs, even when there was no conflict. – joanis Jul 05 '19 at 16:42
  • The absence of `git diff3` in Git bugged me enough that I decided to write it. See here https://stackoverflow.com/a/56917121/3216427 . It may be useful to you as well. – joanis Jul 06 '19 at 19:39
  • @joanis Thanks for your time. Unfortunately, whatever git diff I use, it never shows me the lines that it just overrrid with the old version of the code... – Fly Jul 08 '19 at 07:31

0 Answers0