0

Usually when we merge feature branch with master or any other branch and if same file is modified in different branches but on different lines then GIT does resolve the conflict automatically. We dont want these merge happened automatically and expecting GIT should warn us with list of common files modified in two branches to be merged.

e.g. In Master, we have file test1.txt as below

AAA
BBB

Feature branch (feature/test1) created out of master and updated file test1.txt

AAA
BBB
CCC

also added new file Test2.txt

If I merge feature branch in master then this is will resolve conflicts automatically and merges the file contents successfully. We wanted if same file is modified in feature and master branch then automatic merge should not occur, howeven it should warn saying test1.txt is modified and needs manual merge.

Please assist how to achieve.
NitinK
  • 103
  • 1
  • 7
  • 2
    Why do you want to merge all of that *manually*? – jonrsharpe Jun 29 '20 at 13:20
  • Just to correct your terminology, this is not a conflict. Git is incapable of automatically resolving conflicts. What type of content are you tracking in git that automatically merging content is causing you problems? – LightBender Jun 29 '20 at 14:06
  • @jonsharpe, its mostly happens with SQL files which are not getting compiled and if its not warned then mostly getting some functionality deployed which we are not supposed to deploy for testing. – NitinK Jun 29 '20 at 17:19
  • @LightBender, we are having T-SQL/PL-SQL files, but we are not getting, when the changes are done on the same lines then it warns Conflicts.. but in case of changes having at different line it simply merges the contents tried below but dint helped. git merge BRANCH --no-commit --no-ff – NitinK Jun 29 '20 at 17:23
  • One potential option is only allowing a branch to be merged to master if master is already fully merged into it. That way you can fully verify all your SQL files before you merge them into master – LightBender Jun 30 '20 at 02:15

2 Answers2

0

Not an automated solution, but you can get the list of files modified in both branches as follows:

(git log --format="" --name-only branch1..branch2 | sort -u
 git log --format="" --name-only branch2..branch1 | sort -u) |
sort |
uniq -c |
sed '/  *1 /d; s/  *[0-9][0-9]* //'

Edit: updated to ensure that each file is represented only once in each git log command.

Bill Jetzer
  • 1,105
  • 4
  • 6
  • tried this but I am getting newly added file as well which were not common modified files in both branches – NitinK Jun 29 '20 at 17:36
  • Likely due to a file added in one commit and updated in a subsequent commit. I've edited my answer to ensure that the list of of files output by each `git log` command is unique. – Bill Jetzer Jun 29 '20 at 17:59
  • Thanks @Bill, the file is added and other file is updated in same commit. – NitinK Jun 30 '20 at 04:32
  • I will even be happy if there is any way to list the updated files only.. and not the added/deleted ones – NitinK Jun 30 '20 at 04:33
0

It seems like your intent is to have all the code reviewed before merging into master. This is typically achieved through the pull request feature in Github/Bitbucket or the merge request feature in Gitlab. Is this what you're looking for?

Daly
  • 787
  • 2
  • 12
  • 23
  • yeah the intention is same but we are already having pull requests mechanism in place. problem occurs when same file is changed by multiple people and at the time of testing some unintended code is delivered due to merging – NitinK Jun 29 '20 at 17:25
  • If all code is merged through a PR, and all PRs have to be reviewed and approved, it seems like that would prevent unintended code from getting in. Maybe it's an issue with the group's PR process? – Daly Jun 29 '20 at 18:55
  • we have master branch, from one state, 2 Devs created 2 feature branches and modified same file, we have automated process which is releasing the code to prod and merging branch to Master, so for Dev1 who will merge first is not a issue, but when 2nd Dev goes for merging same file, in case of conflicts (change on same line) merge command throws errors (all good) but if Dev2 has modified on different line then git merge automatically merges the contents and does not warn. In our case this automatic merging sometime does gives expected results so want to stop this behavior. – NitinK Jun 30 '20 at 03:40
  • Ah I see your issue. Hypothetically you could write a custom merge tool to raise conflicts for any additions to the same file, but that would raise a bunch of issues. Namely, which commits to compare between, how to display conflicts on different lines, etc. While that may be possible, Bill's solution might serve you better. In my experience teams usually solve this through communication and a detailed code review process. I wish I could be more helpful, but I don't think there is an easy solution here. – Daly Jun 30 '20 at 22:46
  • 1
    Thanks @Daly, custom merge tool is considering as last option, I am sure that will add more complexities also looking into alternate options like GIT DIFF/GIT LOG to find common modified files between branches with Squash commits. – NitinK Jul 01 '20 at 02:46