avatarValentin Despa

Summary

The web content provides a step-by-step guide on how to resolve a "Merge blocked" issue in GitLab when a fast-forward merge is not possible due to missing changes from the main branch and potential merge conflicts.

Abstract

When working with Merge Requests in GitLab, users may encounter a "Merge blocked" message indicating that a fast-forward merge cannot be performed because the branch lacks updates from the main branch and has conflicts. The article outlines the process of cloning the repository, updating the main branch, rebasing the feature branch, resolving conflicts, and force-pushing the rebased branch to GitLab. It emphasizes the importance of integrating the latest changes from the main branch and suggests using tools like Visual Studio Code to ease conflict resolution. The author also notes that after rewriting history with a rebase, one must use the --force flag to push the changes back to the remote repository.

Opinions

  • The author implies that encountering "Merge blocked" messages is a common occurrence for GitLab users working with Merge Requests.
  • The preference for resolving merge conflicts is to manually edit files, potentially with the aid of an IDE such as Visual Studio Code.
  • The author advocates for the use of the --force flag when pushing changes after a rebase, acknowledging the necessity of overriding the remote branch after rewriting history.
  • Engagement with the content is encouraged through comments, sharing, and applause (up to 50 times), indicating the author's desire for feedback and community interaction.
  • A recommendation is made for an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus (GPT-4), suggesting the author's endorsement of this tool for similar tasks or queries.

GitLab: Merge blocked: fast-forward merge is not possible. To merge this request, first rebase locally.

If you are working with Merge Requests in GitLab, you may see this message quite often. So what can be done to fix this and merge the branch?

So what is wrong?

If you go to any merge request displaying this message, you will see that the branch does not have all changes from the main branch. Also, GitLab cannot rebase this as there are some conflicts.

This occurs when you are using fast-forward merges.

Usually, you can rebase directly from the Merge Request in GitLab, unless there is a merge conflict. In that case, you can’t do that through the UI and get this message:

How to fix “Merge blocked”?

The first step is to clone the repository on your computer and to go into the respective branch.

git clone git@gitlab.com:some-group/my-project.git

This assumes you have already configured your local Git installation to work with GitLab.

cd my-project

If you already have the repository cloned, let’s go into the main branch and make sure it has the latest changes:

git checkout master
git pull origin master

The next step would be to go into the feature branch and rebase:

git checkout feature/changeTemplate
git rebase master

This will most likely issue a merge conflict in one or more files. Example:

CONFLICT (content): Merge conflict in src/pages/index.js

You can manually resolve this conflict with a regular text editor or use an IDE. I am using Visual Studio Code for this project and this will make my life a bit easier.

Right on top, I will choose the most appropriate setting, which for me is “Accept Both Changes”. However, may depend on your needs. You can edit the file as you need.

Now I need to add the file or files modified:

git add src/pages/index.js

Since we are still in a rebase, I will run:

git rebase --continue

The rebase will show other conflicts (if any) or complete. Check your Git log and the changes made. Make sure everything looks right.

With rebase, we have rewritten the history. So if we try something like:

git push origin feature/changeTemplate

The push will be rejected with an error similar to this one:

! [rejected] feature/changeTemplate -> feature/changeTemplate (non-fast-forward)
error: failed to push some refs to ‘[email protected]:some-group/my-project.git’
Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g.
‘git pull …’) before pushing again.
See the ‘Note about fast-forwards’ in ‘git push — help’ for details.

So once we have rewritten the Git history, we can no longer push to the old branch. So we need to override the branch completely, by adding the — force flag.

git push origin feature/changeTemplate --force

If you enjoy content like this and it helped you solve a problem, help me create more. Please leave a comment, share, and press that 👏 a few times (up to 50 times). And consider subscribing to Medium for more amazing content.

Conclusion

I hope this tutorial helped you fix this error while working with Merge Requests in GitLab. Leave a comment in the section below if you have any questions. I would love to hear from you!

Thank you for sticking with this article until the end. If you enjoyed it, please leave a comment, share, and press that 👏 a few times (up to 50 times). It will help others discover this information and maybe it will help someone else as well.

Follow me on Medium and YouTube if you’re interested in more tutorials like this one.

Gitlab
Gitlab Ci
Git
Git Merge
Merge Request
Recommended from ReadMedium