I forked a repository on github. I made some changes and did a pull request.
Now I made some other changes and want to do a new pull request, but on the preview screen before doing the pull request it shows the old commits too (the ones that were already accepted).
How do I select only the latest commit in the master branch of my forked repository so that I can do a pull request with only that commit?
This answer from a coworker fixed my problem:
git checkout -b NEW_BRANCH_NAME LAST_COMMIT_NAME_BEFORE_THE_ONE_WANTED
git cherry-pick COMMIT_NAME_WANTED
git push origin NEW_BRANCH_NAME
Then on GitHub you can do a pull request for the new branch you created.
UPDATE
I asked and answered this question when I first started working with git. Now that I know more about it, I'd like to expand this answer.
When working with a fork you probably want to keep it updated with respect to the original repo. So these would be the steps I would follow today:
git remote add upstream GIT_URL_OF_THE_ORIGINAL_REPO
Now you have a reference called upstream
which points to that repo. By default you should also have another one called origin
which would point to your fork in this case. upstream
and origin
is just how people usually name these references, but you can use whatever name you want.
Now you need to get the latest changes:
git fetch upstream
Then if you want to update your fork with changes from upstream
you'd do this:
git checkout master //checkout your master branch
git merge upstream/master //merge changes from the master branch in upstream into the branch you checked out (master)
git push origin master //if you want to push your updated master to your remote fork
Now, to answer the original question, what I would do today if I wanted to submit a new PR, would be:
git fetch upstream //get the latest changes from the original repo
git checkout -b my_new_feature upstream/master //create a new branch, starting from the master in the original repo
git cherry-pick WHATEVER_COMMIT_I_WANT //select the commit I want and add it to this new branch
git push origin my_new_feature //push a new branch to my fork
Then I would request a new PR for the my_new_feature
branch.
You can replace git cherry-pick WHATEVER_COMMIT_I_WANT
with just modifying/adding a file and then doing git add FILENAME
, git commit -m "Fixing some stuff"
.