Gaurang
Gaurang

Reputation: 181

Create separate pull request for each commit

I know this is not a new question, however I tried many solution provided here and nothing seems to work for me.

I have forked a project to contribute on it. I did multiple commits and they all have been included under one pull request only.

I want to create new pull request for each commit, I tried creating a new branch and cheery-picking particular commit however it doesn't seem to work.

  1. I have a master branch which is 4 commits ahead of the remote branch. https://github.com/Gaurang033/Selenium2Library
  2. If I create a new branch from my master all these changes are also getting copied. and so creating a branch is also not helping. https://github.com/Gaurang033/Selenium2Library/tree/click_elements

Here is some output from git log --all --oneline --graph --decorate:

* 340fb9e (origin/click_elements) Feature Request - Click Elements #585
*   8a8f485 (HEAD -> master, origin/new_locator, origin/master, origin/HEAD) Merge remote-tracking branch 'upstream/master'
|\
| * 2466942 (upstream/master) Libdoc updates - link to project and fix 404 (#668)
* | e66862f Added method to find element by class name #673
* | f8ec2ed Feature Request - Click Elements #585
* | 264e38c Added following keywords and their acceptance test cases to fix the issue #463 table_cell_should_not_contain table_column_should_not_contain table_footer_should_not_contain table_header_should_not_contain table_row_should_not_contain table_should_not_contain
| * [truncated: more commits on master in original repo...]
|/
*   ef5d24d Merge pull request #539 from ReadmeCritic/master

Someone please let me know how to resolve this issue?

Upvotes: 4

Views: 4170

Answers (1)

Scott Weldon
Scott Weldon

Reputation: 10227

When submitting a PR on GitHub, it is usually best to keep the PR atomic, and have it up-to-date with the official master. That is, any branch you are using for a PR should come directly off upstream/master, and should contain only the commits needed for that PR.

To fix your issue, first, reset master to be in sync with upstream:

git checkout master
git reset --hard upstream/master

Now, for each PR you want to submit, create a new branch and cherry-pick the appropriate commit. E.g.:

git checkout -b branch1
git cherry-pick f8ec2ed
git push --set-upstream origin branch1

After you have finished, you can delete your original click_elements branch:

git branch -D click_elements
git push :click_elements

Or, if you want that branch to only contain the first commit (thus allowing you to reuse the existing PR), do:

git checkout click_elements
git reset --hard 264e38c
git push --force-with-lease

To avoid this problem in the future, never commit directly to master. Instead, do the following:

  1. Update your repo, if necessary:

    git checkout master
    git pull upstream master
    
  2. Create a new branch (git checkout -b mybranch).

  3. Make any changes needed for this one PR on that branch, committing as usual.
  4. Immediately before submitting your PR, rebase your feature branch:

    git checkout master
    git pull upstream master
    git checkout mybranch
    git rebase master
    

    (Depending on the project, you may be able to rebase even after the PR has been opened. However, often you won't need to, especially if you did so right before opening the PR.)

  5. Open the PR.


Obligatory warning: Since a rebase (as well as reset --hard) rewrites history, this can be dangerous / disruptive for anyone else working on this branch. Be sure you clearly communicate what you have done with anyone you are collaborating with.

Upvotes: 4

Related Questions