git 如何“拉取请求”特定提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34027850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:43:47  来源:igfitidea点击:

How to "pull request" a specific commit

gitgithub

提问by pontikos

I've got a specific commit which I would like to contribute to a repository I have forked on github. I assume the mechanism to do so is a "pull request". However when I try this I can only pull request my whole branch. I do not wish to pull request the other commits as they are not relevant. Any idea how I can do this.

我有一个特定的提交,我想为我在 github 上分叉的存储库做出贡献。我假设这样做的机制是“拉取请求”。但是,当我尝试这个时,我只能请求我的整个分支。我不想拉请求其他提交,因为它们不相关。任何想法我怎么能做到这一点。

repo I wish to pull request to.

我希望将请求拉到的回购。

The last commit b50b2e7 is the only commit I wish to pull request. Anyway I can do this or are all commits dependent on each other?

最后一次提交 b50b2e7 是我希望拉取请求的唯一提交。无论如何我可以做到这一点还是所有提交都相互依赖?

commit I wish to pull request

提交我希望拉取请求

回答by Joseph Silber

Create a new branch with just that change:

只用这个更改创建一个新分支:

$ git fetch --all
$ git checkout -b my-single-change upstream/master
$ git cherry-pick b50b2e7
$ git push -u origin my-single-change

Then create the PR from that branch.

然后从该分支创建 PR。



The above assumes you've set up upstreamas a remote. If not, do this first:

以上假设您已设置upstream为遥控器。如果没有,请先执行以下操作:

$ git remote add upstream https://github.com/konradjk/exac_browser.git

回答by gibbone

I had the same error of alwaysCurious, so I did a little digging. 1

我有与alwaysCurious相同的错误,所以我做了一些挖掘。1

The regular case

常规案例

A - B - C [master]
         \
          D - E - F - G [feature] 

You're working on a project, you use a separate branch (feature) for your committed changes (D-E-F-G) and you want to create a pull request. However you want only some of the commits to be included in the pull request (Eand F)

您正在处理一个项目,您使用单独的分支 ( feature) 进行提交的更改 ( D-E-F-G),并且您想要创建拉取请求。但是,您只希望在拉取请求中包含一些提交(EF

The procedure here is the one from Joseph's answer

这里的程序是约瑟夫的回答

# optional: set upstream as remote if it's not
git remote add upstream https://github.com/<upstream_github_username>/<upstream_github_repo_name>.git
# fetch changes
git fetch --all
# create specific branch for your partial pull request
git checkout -b partial-change upstream/master

Now this is how it looks:

现在它是这样的:

          [partial-change]
A - B - C [master]
         \
          D - E - F - G [feature]

Cherry-pick your specific commits and push the changes:

樱桃挑选您的特定提交并推送更改:

git cherry-pick <hash of commit E>
git cherry-pick <hash of commit F>
git push -u origin partial-change

After fixing any conflict this is where you'll get:

解决任何冲突后,您将获得:

          E1 - F1 [partial-change]
         / 
A - B - C [master]
         \
          D - E - F - G [feature]

The consecutive case

连续案例

If instead you just want to apply all the consecutive commits up to the last one (or two or three) you can just branch out at the specific commit. For instance here I just want the commits up to Eand not the subsequent ones:

相反,如果您只想将所有连续提交应用到最后一个(或两个或三个),您可以在特定提交处分支。例如在这里我只想要提交E而不是后续的提交:

git checkout -b partial-consecutive-changes <hash of commit E>
git push -u origin partial-consecutive-changes

A - B - C [master]
         \
          D - E [partial-consecutive-changes]
               \
                F - G [feature]

The rookie mistake

菜鸟的错误

The last procedure can also help you if you just applied consecutive changes to master without using a specific branch for them and now you want to cherry-pick them after. This is relevant if you've forked a project at Cand proceeded on master with the other commits. Here I am adding an asterisk to signal that new changes are happening on the fork:

如果您只是对 master 应用连续更改而不使用特定分支,那么最后一个过程也可以帮助您,现在您想在之后挑选它们。如果您已经在那里分叉了一个项目C并在 master 上继续进行其他提交,则这是相关的。在这里,我添加一个星号来表示分叉上正在发生新的变化:

A - B - C - D* - E* - F* - G* [master]

What you shouldn't do is:

你不应该做的是:

git checkout -b partial-change upstream/master
git cherry-pick <hash of commit D>
git cherry-pick <hash of commit E>
git push -u origin partial-change

In this case you're trying to branch out the master at G*and cherry picking the previous commits will get you the warning:

在这种情况下,您尝试将 master 分支出来,G*然后选择以前的提交会给您警告:

The previous cherry-pick is now empty, possibly due to conflict resolution.

之前的cherry-pick 现在是空的,可能是由于冲突的解决。

since you're adding the same old commits on the new branch.

因为您在新分支上添加了相同的旧提交。

What you should do instead is:

你应该做的是:

git checkout -b partial-change <hash of commit E>
git push -u origin partial-change

A - B - C - D* - E* - F* - G* [master]
                  \
              D* - E* [partial-change]               

After this you're ready to make a pull request with only the selected commits.

在此之后,您就可以仅使用选定的提交发出拉取请求。



Notes:

笔记:

  1. Here I'm extending this great answerfrom Schwern.

  2. To get the last ncommit hashes it may be useful to use: git log --pretty=oneline --abbrev-commit | head -n

  1. 在这里,我扩展了 Schwern 的这个很棒的答案

  2. 要获取最后一次n提交哈希,使用以下方法可能很有用:git log --pretty=oneline --abbrev-commit | head -n

回答by alwaysCurious

I'm not familiar with cherry-pick and had a problem when I tried Joseph's approach (something about the cherry-pick being empty). I found a work-around that seems to have worked well:

我不熟悉樱桃,当我尝试约瑟夫的方法时遇到了问题(樱桃是空的)。我找到了一个似乎运行良好的解决方法:

# Create new branch directly from specified commit:
$ git checkout -b my-single-change b50b2e7
$ git push --set-upstream origin my-single-change

You can now select this branch in GitHub and create a pull request.

您现在可以在 GitHub 中选择此分支并创建拉取请求。