'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
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 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[3]
In my case this happens when I send merge request without pulling and merging all new code from develop branch.
I just wrote this in console
git add .
git commit -m "my commit"
git push
Without change branch to develop and pull (after merge) everything from it.
For work you need to pull actual code from develop and merge it to your branch
git add .
git commit -m "my commit"
git checkout develop
git pull
git checkout <branch_name>
git merge develop
// if have any conflicts fix them and push it
git push // or if need set upstream git push --set-upstream origin <branch_name>
Solution 4:[4]
I've suffered a lot from it, and today I found an EXTREMELY SIMPLE way:
For example, assuming you want to pass the content:
From a branch called feature/NativeSubmodule To a branch called develop .
Download and install SourceTree [ https://www.sourcetreeapp.com ], then open your project in it, and follow the steps in this image:
And then:
If you want to just Replace the destination branch(develop) with the Origin branch content(feature/NativeSubmodule), you just don't have to do the "Extra)" step. :D
Solution 5:[5]
Staying on current branch:
git pull -r origin master
This pulls the current branch and rebase on origin-master branch. This is a fast-forward operation. If it doesn't work on the first try, then open VSCode and manually accept/decline the changes.
git status
will show still rebasing.
git rebase --continue
will add the new commits.
git push --force
will complete the rebasing and pipeline will start to build.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Biegeleisen |
Solution 2 | hundredrab |
Solution 3 | Randall |
Solution 4 | Breno Medeiros de Oliveira |
Solution 5 |