gitgithubgitlabbranchrebase

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?


Solution

  • 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 (NOTE: all uncommitted modifications in the working directory will be irreversibly deleted, so save/commit before proceeding):

    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.