Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

git - How to do a pull request in GitHub with only the latest commit in the master branch of my forked repository

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...