Github: split PR into two PRs by files

Viewed 2065

Here's an operation that's trivial in Perforce and seems nigh impossible in Git, but let's see. Situation: Pull Request with six files, call them: A, B, C, X, Y, and Z. None of this is merged to master, but the local branch has been pushed to the remote (Github if it matters).

There are many, many commits that touch any and all of these files. Some commits touch only one file. Some touch all six.

I want to create two new PRs. One PR contains files A, B, and C with all the changes from all the commits. The other PR contains X, Y, and Z with all the changes to those files from all the commits. I don't care what happens to the commit history or branches. All commits will be squashed before merge and all work branches will be deleted.

2 Answers

I was in a similar situation as you and this is what I did :-

  1. Checkout your feature branch.
  2. Considering that your master branch is named as main, run git reset --soft main. This will delete all your commits in your feature branch. But all the changes that you did in those commits will stay as unstaged files.
  3. Now, create a new branch. Stage files A, B and C. Commit, push and create a new PR. This PR will contain changes only from files A, B and C.
  4. Checkout your original feature branch again. Again, create a new branch. This time, stage files X, Y, and Z. Commit, push and create a new PR. This PR will contain changes only from files X, Y, and Z.
  5. Close your original PR (that contained changes from all 6 files) as it is no longer required and ask reviewers to review the 2 new PRs that you created.

One way to resolve this:

  1. squash all
  2. create a new branch from your current branch
  3. amend the commit and remove the changes of files X, Y and Z
  4. force push and create the 2nd PR
  5. go back to your other branch
  6. amend the commit and remove the changes of file A, B and C
  7. force push to update your current PR
Related