我在github上分叉了一个项目,并成功地对我的本地master进行了更改,并在github上推到了原点。我想发送一个拉请求,但只想包括最后一次提交。在github.com上的拉请求UI显示了最后9次提交,我不知道如何过滤下来。
我试图理解我是否应该创建一个新的本地分支,检查出来,以某种方式重置或重基到上游?然后应用我的最后一次提交从我的主人通过id到新的本地分支,并使用它的拉请求?
我正在努力掌握正确的概念,并找出正确的命令行来完成我需要的工作。
我在github上分叉了一个项目,并成功地对我的本地master进行了更改,并在github上推到了原点。我想发送一个拉请求,但只想包括最后一次提交。在github.com上的拉请求UI显示了最后9次提交,我不知道如何过滤下来。
我试图理解我是否应该创建一个新的本地分支,检查出来,以某种方式重置或重基到上游?然后应用我的最后一次提交从我的主人通过id到新的本地分支,并使用它的拉请求?
我正在努力掌握正确的概念,并找出正确的命令行来完成我需要的工作。
当前回答
Based on @kevin-hakanson's answer, I wrote this little bash script to make this process easier. It will add the upstream repo if it doesn't already exist (prompting you for the URL) then prompt for both the name of the new branch to create and the tag / SHA of the commit to cherry pick onto that branch. It checks what branch or commit you're currently on then stashes any changes so you can checkout the new branch. The merge strategy keeps the changes from the cherry-picked commit. After pushing the new branch to origin (assumed to be the name of your remote repo), the branch or commit you were on before is checked out again and your previous changes popped from the stash.
if ! git remote | grep -q upstream; then
read -p "Upstream git repo URL: " upstream
git remote add upstream $upstream
git remote update
fi
read -p "Feature branch name: " feature_branch
# note: giving "master" is the same as giving the SHA it points to
read -p "SHA of commit to put on branch: " sha
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "HEAD" ]; then
# detached HEAD; just get the commit SHA
current_branch=$(git rev-parse --short HEAD)
fi
git stash
git checkout -b $feature_branch upstream/master
git cherry-pick --strategy=recursive -X theirs $sha
git push origin $feature_branch
git checkout $current_branch
git stash pop
(这在几个简单的测试中对我有效,但我不是bash程序员或git专家,所以如果我遗漏了可以更好地自动化的情况,请告诉我!)
其他回答
从最近的提交开始创建一个新分支,它也在原始存储库中:
git branch new-branch origin/master
git checkout new-branch
然后使用git来获得你想要的pull请求的单个提交。如果带有此提交的分支称为feature,并且您想要的提交是该分支中的最新提交,则将为
git cherry-pick feature
假设这个补丁没有冲突地应用,那么您现在就有了一个可以执行pull请求的分支。
在第二步中,您现在需要决定如何处理您的特性分支。如果你还没有在这个分支上发布你的更改,最好的方法可能是在new-branch上重新基于这个分支(并且删除最后一次提交,如果git rebase没有自动完成的话)。
我最终遇到了这样的情况:我已经分叉了,想要向原始项目提交一个拉请求。
我有:
orignal_project forked_project(从SHA的原始项目创建:9685770) my_fork(从SHA的fork项目创建:207e29b) 在我的fork (SHA: b67627b)中提交一个我想提交回原始项目的提交
为此,我:
从原来项目分叉的SHA创建了一个新的分支 都是从原项目中提取的 cherry选择了我想作为拉请求提交的提交 都放到github上了
git命令是这样的:
Git分支my-feature-request 9685770 Git checkout my-feature-request Git拉https://github.com/original_project/original_project.git Git选择b67627b Git推送源my-feature-request
然后我选择my-feature-request作为对原始项目的pull请求的分支。
你需要创建一个新的分支,并选择你想要添加到它的提交。
注意:在执行checkout/cherry-pick命令之前可能需要这些命令 Git远程添加上游< Git仓库> Git远程更新
git checkout -b <new-branch-name> upstream/master
git cherry-pick <SHA hash of commit>
git push origin <new-branch-name>
之后,你会在github上看到<new-branch-name> branch,切换到它,可以提交拉请求和你想要的更改。
我已经完成了提交,我希望能够将其作为一个拉请求隔离回当前分支。
所以我去了一家新分店
git checkout -b isolated-pull
这就是我的解决方案与@Kevin Hakanson的不同之处,因为我需要将这个分支重置到我想要不同的历史中的地方
git reset --hard [sha-to-diff-by]
然后选择我想从中创建一个独立的拉请求的提交
git cherry-pick [my-isolated-commit-sha]
最后把它推上遥控器
git push origin isolated-pull
并拉请求dat shi。
Based on @kevin-hakanson's answer, I wrote this little bash script to make this process easier. It will add the upstream repo if it doesn't already exist (prompting you for the URL) then prompt for both the name of the new branch to create and the tag / SHA of the commit to cherry pick onto that branch. It checks what branch or commit you're currently on then stashes any changes so you can checkout the new branch. The merge strategy keeps the changes from the cherry-picked commit. After pushing the new branch to origin (assumed to be the name of your remote repo), the branch or commit you were on before is checked out again and your previous changes popped from the stash.
if ! git remote | grep -q upstream; then
read -p "Upstream git repo URL: " upstream
git remote add upstream $upstream
git remote update
fi
read -p "Feature branch name: " feature_branch
# note: giving "master" is the same as giving the SHA it points to
read -p "SHA of commit to put on branch: " sha
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" == "HEAD" ]; then
# detached HEAD; just get the commit SHA
current_branch=$(git rev-parse --short HEAD)
fi
git stash
git checkout -b $feature_branch upstream/master
git cherry-pick --strategy=recursive -X theirs $sha
git push origin $feature_branch
git checkout $current_branch
git stash pop
(这在几个简单的测试中对我有效,但我不是bash程序员或git专家,所以如果我遗漏了可以更好地自动化的情况,请告诉我!)