Proposition: we have a single branch first-branch
that has two commits.
This has been pushed to GitHub and an open pull request, but we want to revert that pull request to only include the first commit and open a new, separate pull request with the second commit. Our log looks like:
$ git log --oneline
67b58467 (HEAD -> first-branch) second commit
377539cf first commit
3a06310d (origin/master, master) Merge pull request #2201 from kyla-harper/master
-
git checkout -b second-branch first-branch
to create a new branch. Now you have two branches that both have both commits. We are going to rewrite the git history of both branches to include only one of those commits. -
restore
first-branch
to the commit you want. This is the easier one. You can see the desired commit hash on GitHub, or get it withgit log
:$ git log --oneline 67b58467 (HEAD -> second-branch, first-branch) second commit 377539cf first commit 3a06310d (origin/master, master) Merge pull request #2201 from kyla-harper/master
git reset
lets you change which commit a branch refers to.git checkout first-branch git reset 377539cf --hard
This reverts
first-branch
to 'first commit' above.first-branch
no longer has "second commit" in it. You can push this to your branch on GitHub, which requiresgit push --force
in order to allow rewriting history:git push --force mine first-branch # mine is whatever you call *your* fork
At this point, your PR should be updated with only the commit you wanted. The extra commits are no longer included in the PR.
-
Now we can update
second-branch
to remove first commit. This will usegit rebase
. To start, check outsecond-branch
:git checkout second-branch
Now that you are in
second-branch
, rungit rebase -i origin/master
(orupstream/master
, or whatever you have chosen to calljupyterhub/master
:) ). This brings up your editor with a list of commits to choose. It will replay the history, however you tell it to in this file. For two commits, it will look something like this:pick 377539cf first commit pick 67b58467 second commit
Since you are on
second-branch
, remove the line with "first commit", save the file, and close the editor. This will replay applyingsecond commit
on top oforigin/master
(whatever you passed togit rebase
). Now you should havesecond-branch
with only one commit afterorigin/master
. You can push this new branch and open a new pull request with only the second commit.
These steps make a lot of sense. My hangups are specifically related to not being familiar with the
reset
andrebase
commands and I think this writeup definitely helps me wrap my head around how I can use them. Thank you so much for writing this! :)