Is there a way to force Gitlab to merge "main" to your branch when you try to merge TO "main"?

Here’s my issue. We have a master branch “main” that all MRs have to be merged to to be deployed. Before any MR can be merged to “main”, they must be approved by multiple peers, which can take awhile. We then hit merge, which does a squash-and-merge. It fails…a lot. Let me explain:

The procedure for creating a feature branch MR is to:

  1. Pull main
  2. create branch called ‘feature_branch’
  3. do work
  4. merge latest main into ‘feature_branch’ locally
  5. push ‘feature_branch’
  6. create the MR

In the time it can take to get an approval, however, “main” might change. So when you go to merge, the squash fails. There is not merge conflict, but it is likely some history conflict. It doesn’t really matter, because if you re-merge main into your branch and push, the problem is resolved. However, you’ll then need multiple approvals again. What if that takes hours and you get out of date again? We have 20 engineers contributing to the same repo, I’ve been caught in this cycle 3 or more times. It’s a massive time suck.

My solution: ask gitlab, when you press the merge button, to merge main to the feature branch first. That way, barring an actual merge conflict, everything should go smoothly.

I tried googling how to this but it’s a tricky query. Anyone able to point me to documentation on how to do it?

1 Like

Hi @ClaytonAndTheClayton

I’d expect there are a few ways to automate what you’re trying to do. Personally I use a merge bot to pull feature branches up to date.

3 Likes

Oh, I didn’t know about marge-bot before, thank you for sharing @snim2! :smile:

It looks like that merge bot might have what you’re looking for @ClaytonAndTheClayton, specifically with the rebase support.

Alternatively, I also came across the gitlab-auto-mr project and related blog post on How to automatically create a new MR on GitLab with GitLab CI. You could fork that script and add any specific team needs around merging/rebasing on intervals.

2 Likes