Skip to content

Commit 5064ed9

Browse files
Add instructions for rebasing onto a branch
Useful if someone has created a PR from the wrong branch
1 parent c807cce commit 5064ed9

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

contributing/workflow/pr_workflow.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ will raise an error:
518518
hint: Updates were rejected because the tip of your current branch is behind
519519
hint: its remote counterpart.
520520

521-
This is a sane behavior, Git will not let you push changes that would
521+
This is reasonable behavior, Git will not let you push changes that would
522522
override remote content. But that's actually what we want to do here, so we
523523
will have to *force* it:
524524

@@ -530,6 +530,31 @@ And tadaa! Git will happily *replace* your remote branch with what you had
530530
locally (so make sure that's what you wanted, using ``git log``). This will
531531
also update the PR accordingly.
532532

533+
Rebasing onto another branch
534+
----------------------------
535+
536+
If you have accidentally opened your PR on the wrong branch, or need to target another branch
537+
for some reason, you might need to filter out a lot of commits that differ between the old branch
538+
(for example ``4.2``) and the new branch (for example ``master``). This can make rebasing difficult
539+
and tedious. Fortunately ``git`` has a command just for this situation, ``git rebase --onto``.
540+
541+
If your PR was created from the ``4.2`` branch and you want to update it to instead start at ``master``
542+
the following steps *should* fix this in one step:
543+
544+
.. code-block:: text
545+
546+
$ git rebase -i --onto master 4.2
547+
548+
This will take all the commits on your branch *after* the ``4.2`` branch, and then splice them on top of ``master``,
549+
ignoring any commits from the ``4.2`` branch not on the ``master`` branch. You may still need to do some fixing, but
550+
this command should save you a lot of tedious work removing commits.
551+
552+
Just like above for the interactive rebase you need to force push your branch to handle the different changes:
553+
554+
::
555+
556+
$ git push --force origin better-project-manager
557+
533558
Deleting a Git branch
534559
---------------------
535560

0 commit comments

Comments
 (0)