Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast-forward merge is not possible. To merge this request, first rebase locally

Recently, I have created newbranch and created a merge request to Master branch. Before TeamLead accept merge request into Master branch another team member was committed another fix to the same branch(newbranch). After that I committed my local changes and pulled the changes in newbranch to local branch. And I pushed my local commit to newbranch

gitlab error

My TeamLead told me to rebase my branch to an earlier version. And resolve conflicts. I don't know what to do now. Any idea?

like image 628
SanjX Avatar asked Oct 30 '18 14:10

SanjX


People also ask

Is it possible for a fast-forward merge to have conflicts?

It's not possible to have conflicting changes in a fast-forward merge.

What is the difference between fast-forward merge and rebase?

There are a few differences between the two workflows: The rebase workflow keeps the git graph linear, while the merge workflow keeps track of the branch commits.

What happened fast-forward merge?

Fast-forward merges literally move your main branch's tip forward to the end of your feature branch. This keeps all commits created in your feature branch sequential while integrating it neatly back into your main branch.


2 Answers

Starting on your newBranch:

git checkout master to get back on the master branch

git pull origin master to get the most up-to-date version of the master branch

git checkout newBranch to get back on your newBranch

git rebase origin/master -i to perform an interactive rebase. The command will take you through and let you pick commits, rename them, squash them, etc. Assuming you will want to keep them all, it will pause when there are merge conflicts and then you'll have to resolve them in your text editor, it will tell you where (in your text editor) that the conflicts occur. You will have to add those files after fixing them then do git rebase --continue to proceed with the rebase.

When you're done with the rebase your newBranch will be synced up with master and have any commits in master that weren't there when you started your work, and all merge conflicts will be resolved so that you can easily merge your newBranch.

like image 63
chevybow Avatar answered Sep 19 '22 12:09

chevybow


It seems that your GitLab is configured to not allow feature branches with merge commits to be merged into the master branch. This is where you took a wrong turn:

After that I committed my local changes and pulled the changes in newbranch to local branch.

What you should have done is to commit your work, and then pull via rebase from the remote newbranch branch. To remedy the situation, I suggest nuking the merge commit which happened when you pulled from GitLab. A merge commit is likely what happened, because git pull by default uses the merge strategy, not the rebase strategy. Check git log, and see how many commits were introduced due to the incorrect pull. Assuming there were only a single merge commit, then the following should do:

git reset --hard HEAD~1 

Verify again that git log looks correct. You should now see only your latest commit on the top of the branch. Assuming you do see this, then you are good to pull via rebase:

git pull --rebase origin newbranch 

This will bring in your colleague's commit, then replay your latest commit on top of the branch. Finally, you may push the branch out, and the problem should be resolved:

git push origin newbranch 

Note that doing a hard reset as I have suggested above is generally not a good thing to do. But in your case, no one has seen that merge commit yet, because GitLab rejected your attempt to push. So you should be safe in removing it.

like image 31
Tim Biegeleisen Avatar answered Sep 17 '22 12:09

Tim Biegeleisen