1

I am trying to merge two branch without conflict in my script. but if there is any conflict i do not want to fire git merge command, merge two branch. So is anyway that i can check before fire merge command there is any conflict or not?

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
paras chauhan
  • 777
  • 4
  • 13

1 Answers1

4

Manually, the naive route might be to

# actually do the merge
git merge <target>

Then, either it's a conflict, in which case you :

git merge --abort

...or it's not, and you just undo your commit :

git reset --soft HEAD^

and you're back in step 1 in both cases, the merge undone, and you have your answer to the question "Will this be a conflict?".

And YES I find it clunky too but hey! it works. (and to be fair, with aliases it can be very practical)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • actully merge and reset takes time in script. so i want the way that before merge i can check any conflict if not then i merge if conflict i dont want to merge\ – paras chauhan Mar 29 '19 at 14:09
  • Yeah, that was the point of my earlier question about *manual* or *scripting*. This is an answer for manual action. – Romain Valeri Mar 29 '19 at 14:10
  • 2
    @paraschauhan - Well, "want" and "can" are two different things. `git` does not provide a "dry run" for merging, so what you'd have to do (afaik) is (1) diff `HEAD` against the merge base; (2) diff the merge target against the merge base; (3) compare the patches to look for overlapping/adjacent change hunks. That's reproducing most of the work of the merge, though it *might* be faster for lack of actually updating the work tree. But that said... – Mark Adelsberger Mar 29 '19 at 14:23
  • 2
    @paraschauhan - You say that if there's no conflict, you do actually want to do the merge. So if you leave out the `reset` when the merge is successful, and otherwise follow the above process, then the only time when the script "wastes" time is when there's a conflict - which for most projects is relatively rare – Mark Adelsberger Mar 29 '19 at 14:24