Git - Cherry 为拉取请求选择一个提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25955822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git - Cherry pick a single commit for pull request
提问by user3590149
I might not have the terminology down yet. I made file to be added to a open project on git. I forked the project. I made some changes and my last commit is the file I want to request to the project and not the small changes I made before hand. When I go to github site and make pull request I get all the commits before the one I want which is last one of a file and I don't' want to submit all of the other commits because I don't think its necessary for the project. Just my own changes. What do I do? Should I just make another res or attach the file singularly and submit, if that's possible.
我可能还没有掌握术语。我将文件添加到 git 上的打开项目中。我分叉了这个项目。我做了一些更改,最后一次提交是我想要向项目请求的文件,而不是我之前所做的小改动。当我去 github 站点并提出拉取请求时,我得到了我想要的所有提交之前的所有提交,这是文件的最后一个,我不想提交所有其他提交,因为我认为这没有必要该项目。只是我自己的变化。我该怎么办?如果可能的话,我应该制作另一个 res 还是单独附加文件并提交。
回答by SLaks
You need to create a fresh branch from the remote HEAD, cherry-pick the commit to that branch, push the branch to your repo on GitHub, then create a pull request.
您需要从远程 HEAD 创建一个新分支,挑选对该分支的提交,将该分支推送到您在 GitHub 上的存储库,然后创建一个拉取请求。
git checkout -b mybranch
git fetch upstream
git reset --hard upstream/master
git cherry-pick <commit-hash>
git push origin mybranch:mybranch
回答by Jashan
As I'm doing this a lot, and had to look up this answer a few times, I put SLaks answer into a little batch file. This is for Windows / CMD but should be trivial to port to Linux, Mac or PowerShell. As I usually do several commits, each being one pull request, I also added switching back to the master branch.
由于我经常这样做,并且不得不多次查找此答案,因此我将 SLaks 答案放入一个小批处理文件中。这适用于 Windows / CMD,但移植到 Linux、Mac 或 PowerShell 应该是微不足道的。由于我通常会进行多次提交,每次提交都是一个拉取请求,因此我还添加了切换回主分支的功能。
@echo off
REM Idea from: https://stackoverflow.com/questions/25955822/git-cherry-pick-a-single-commit-for-pull-request
set BRANCH_NAME=%1
set COMMIT_HASH=%2
IF %BRANCH_NAME%.==. GOTO NoBranchName
IF %COMMIT_HASH%.==. GOTO NoCommitId
git checkout -b %BRANCH_NAME%
git fetch upstream
git reset --hard upstream/master
git cherry-pick %COMMIT_HASH%
git push origin %BRANCH_NAME%:%BRANCH_NAME%
git switch master
GOTO End
:NoBranchName
ECHO No branch name given. Usage: add-pull-request.bat BRANCH_NAME COMMIT_HASH
GOTO End
:NoCommitId
ECHO No commit id given. Usage: add-pull-request.bat BRANCH_NAME COMMIT_HASH
GOTO End
:End
One obvious improvement would be first picking up the current branch and then use that in the last line but that's something I didn't need and it would make the whole thing significantly more complex.
一个明显的改进是首先选择当前分支,然后在最后一行中使用它,但这是我不需要的,它会使整个事情变得更加复杂。