I was going to do more work on a feature and before coding i ran git pull --rebase upstream master
. Then ran git push origin feature-branch
; this resulted in one of those 'fast-forward' errors in which I then ran git pull origin feature-branch
. Following that I fixed a merge conflict and ran git push origin feature-branch
again. Now my pr from feature-branch
to master
is polluted with other's commits. I noticed this questions was asked previously but never answered.
Can someone explain what I did wrong and maybe how to fix it?
From a content perspective, rebasing is changing the base of your branch from one commit to another making it appear as if you'd created your branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base.
Rebasing applies the changes from your branch onto the latest version of the base branch, resulting in a branch with a linear history since no merge commit is created. To update by rebasing, click the drop down menu next to the Update Branch button, click Update with rebase, and then click Rebase branch.
Now if you know the number of commits you’ve made on the branch that you want to rebase, you can simply run the git rebase command like so: git rebase -i HEAD~ x Here, -i refers to the rebase being interactive, and HEAD refers to the latest commit from the master branch.
An interactive rebase can be used to edit previous commit messages, combine several commits into one, or delete or revert commits that are not necessary any longer. To do this, we will need to be able to reference the commits that we have made either by number or by a string that references the base of our branch.
In Git, this is called rebasing . With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch. For this example, you would check out the experiment branch, and then rebase it onto the master branch as follows:
The following command rebase the current branch from master (or choose any other branch like develop, suppose, the name of remote is origin, which is by default): After git rebase, conflicts may occur. You should resolve them and add your changes by running git add command: git add . Do not run git commit after git add .
I figured it out:
Write down the git commit hashes for all commits in the PR that you want to save (i.e. your commits).
Then run the following:
git fetch upstream
git reset --hard upstream/master
git cherry-pick <hash 1>
git cherry-pick <hash 2>
// cherry-pick all of your commits then:
git push -f origin your-branch
And it should fix your PR automatically
If your current branch has commits you want to keep (your own) mixed in with commits you don't want (the ones added by the rebase), you can use git rebase --interactive
to edit your branch again and keep only the commits you pick.
First, find the hash of the commit before the last one you might want to edit. If your branch was originally based on master
, a good commit to choose is the one returned by git merge-base HEAD master
.
Next, start an interactive rebase from the current commit to that old commit:
git rebase --interactive f1c3d284
Your text editor will pop up with lines representing each commit in your branch and with instructions from Git below them. As the instructions say, change pick
to drop
for each line representing a commit you don't want. When you save and close the document, your current branch will be rebuilt according to those instructions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With