Skip to content

Instantly share code, notes, and snippets.

@minrk
Last active September 28, 2018 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/a1723e10a160826ff1ae548fde8549b3 to your computer and use it in GitHub Desktop.
Save minrk/a1723e10a160826ff1ae548fde8549b3 to your computer and use it in GitHub Desktop.

Splitting a pull request using git reset and git rebase

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
  1. 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.

  2. 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 with git 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 requires git 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.

  3. Now we can update second-branch to remove first commit. This will use git rebase. To start, check out second-branch:

    git checkout second-branch
    

    Now that you are in second-branch, run git rebase -i origin/master (or upstream/master, or whatever you have chosen to call jupyterhub/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 applying second commit on top of origin/master (whatever you passed to git rebase). Now you should have second-branch with only one commit after origin/master. You can push this new branch and open a new pull request with only the second commit.

@SansSeryph
Copy link

These steps make a lot of sense. My hangups are specifically related to not being familiar with the reset and rebase 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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment