-2

Is it possible to merge two branches only if there are no new commits on the target branch?

Say we have master branch with commits:

1.1.0 -> 1.1.1 -> 1.2.0(new)

And dev branch with commits

1.2.0-0 -> 1.2.0-1 -> 1.2.0-2

Say i now want to merge 1.2.0-2 into master to make a new 1.2.0 but fail if the existing commits on master have not been merged into dev. This merge is automated, so i don't want any conflicts or any unexpected behaviour.

Eg imagine that master was merged into dev to make 1.2.0-0, master then receives a hotfix 1.1.1. Then finally the tested 1.2.0-2 on dev should be merged to master, but conflicts or has unexpected behaviour due to presence of 1.1.1. I want the process to fail here, and force a merge first of 1.1.1 into dev, testing and then a merge back.

I have scanned the git docs but can't see a command option that will prevent the merge in this case.

One solution is to check first if the last commit on master exists in dev branch, but ideally this will just be a merge command.

Paul Grimshaw
  • 19,894
  • 6
  • 40
  • 59
  • Does this answer your question? [How to avoid merge-commit hell on GitHub/BitBucket](https://stackoverflow.com/questions/16358418/how-to-avoid-merge-commit-hell-on-github-bitbucket) – John Kugelman Oct 14 '20 at 22:07
  • The question is not the same. The answer may contain the --ff-only option, but not sure how I was supposed to find that – Paul Grimshaw Oct 15 '20 at 05:17
  • So having investigated, the --ff-only option doesn't solve my issue either (see below). No idea why this is being downvoted, whoever did that please tell me what i have not explained or tried – Paul Grimshaw Oct 15 '20 at 06:30

1 Answers1

0

Use the --ff-only option:

git merge --ff-only dev

i don't want any conflicts or any unexpected behaviour

Conflicts also can't happen in this case.

krisz
  • 2,686
  • 2
  • 11
  • 18
  • The ff-only stops conflicts, but it still merges successfully. Successful merges in my case may be as harmful, as it could result in untested behaviour going to production. I simply want to run the command merge command (or other) that will give me an error like `Cannot merge dev into master as master contains commits that are not on dev` – Paul Grimshaw Oct 15 '20 at 06:31
  • 1
    It does not create a merge commit in any case. If true merge is necessary it gives an error `fatal: Not possible to fast-forward, aborting.` and exits with a non-zero status. – krisz Oct 15 '20 at 13:49