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?
Asked
Active
Viewed 54 times
1
-
Do you want to do this manually or in a script? – Romain Valeri Mar 29 '19 at 14:00
-
through git command using in script – paras chauhan Mar 29 '19 at 14:05
-
You can see it in GitHub by creating a merge request. – DTeuchert Mar 29 '19 at 14:06
-
Possible duplicate of [Is there a git-merge --dry-run option?](https://stackoverflow.com/questions/501407/is-there-a-git-merge-dry-run-option) – phd Mar 29 '19 at 15:00
-
https://stackoverflow.com/search?q=%5Bgit%5D+merge+conflict+without+merge – phd Mar 29 '19 at 15:00
1 Answers
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