git - 如何在 GitHub 中仅使用我的分支存储库的主分支中的最新提交执行 pull 请求

标签 git github branch fork pull-request

我在 github 上创建了一个仓库。我做了一些更改并提出了 pull 请求。

现在我做了一些其他更改并想做一个新的 pull 请求,但是在执行 pull 请求之前的预览屏幕上它也显示了旧的提交(那些已经被接受的)。

如何只选择我的分支存储库的 master 分支中的最新提交,以便我可以只使用该提交执行 pull 请求?

最佳答案

同事的这个回答解决了我的问题:

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

然后在 GitHub 上,您可以对您创建的新分支执行 pull 请求。

更新

我在刚开始使用 git 时问过并回答过这个问题。现在我对它有了更多的了解,我想扩展这个答案。

使用复刻时,您可能希望根据原始存储库对其进行更新。所以这些就是我今天要遵循的步骤:

git remote add upstream GIT_URL_OF_THE_ORIGINAL_REPO

现在您有一个名为 upstream 的引用,它指向该存储库。默认情况下,您还应该有另一个名为 origin 的文件,在这种情况下它会指向您的 fork 。 upstreamorigin 是人们通常命名这些引用的方式,但您可以使用任何您想要的名称。

现在您需要获取最新的更改:

git fetch upstream

然后如果你想用来自upstream的改变来更新你的fork,你可以这样做:

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

现在,要回答最初的问题,如果我想提交一个新的 PR,我今天会做什么:

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

然后我会为 my_new_feature 分支请求一个新的 PR。

您可以将 git cherry-pick WHATEVER_COMMIT_I_WANT 替换为修改/添加文件然后执行 git add FILENAME, git commit -m "Fixing some stuff ".

关于git - 如何在 GitHub 中仅使用我的分支存储库的主分支中的最新提交执行 pull 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561161/

相关文章:

amazon-web-services - AWS CodePipeline 操作执行失败

git - 如何处理功能分支内的内部 Maven 依赖关系

node.js - 在 WebStorm 和 SourceTree 中使用 NVM 时 Git Hooks 错误

eclipse - 在 Eclipse IDE 的 EGit 中提交时选择较早的提交消息?

git - 如何设置具有不同 pull/推上游的分支

python - 如何将 gist 中的补丁应用到 Django 源代码?

git - GIT : I've got no branch! 吓坏我的生活

git - 在没有文件 check out 的情况下切换 Git 分支

git - 'pack' 文件对于裸 git 存储库至关重要吗?

git - merge 两个分支不是 master