简体   繁体   English

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

[英]How to "pull request" a specific commit

I've got a specific commit which I would like to contribute to a repository I have forked on github.我有一个特定的提交,我想为我在 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.任何想法我怎么能做到这一点。

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

The last commit b50b2e7 is the only commit I wish to pull request.最后一次提交 b50b2e7 是我希望拉取请求的唯一提交。 Anyway I can do this or are all commits dependent on each other?无论如何我可以做到这一点还是所有提交都相互依赖?

提交我希望拉取请求

Create a new branch with just that change:只用这个更改创建一个新分支:

$ git fetch --all                                   # Get the latest code
# If you haven't set up your remote yet, run this line:
# git remote add upstream https://github.com/konradjk/exac_browser.git
$ git checkout -b my-single-change upstream/master  # Create new branch based on upstream/master
$ git cherry-pick b50b2e7                           # Cherry pick the commit you want
$ git push -u origin my-single-change               # Push your changes to the remote branch

Then create the PR from that branch.然后从该分支创建 PR。

I had the same error of alwaysCurious , so I did a little digging.我有与alwaysCurious相同的错误,所以我做了一些挖掘。 1 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 ( DEFG ) and you want to create a pull request.您正在处理一个项目,您为提交的更改 ( DEFG ) 使用单独的分支 ( feature ),并且您想要创建拉取请求。 However you want only some of the commits to be included in the pull request ( E and F )但是,您只希望在拉取请求中包含一些提交( 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 E and 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.如果您只是对 master 应用连续更改而不使用特定分支,那么最后一个过程也可以帮助您,现在您想在之后挑选它们。 This is relevant if you've forked a project at C and proceeded on master with the other commits.如果您已经在C上分叉了一个项目并继续在 master 上进行其他提交,则这是相关的。 Here I am adding an asterisk to signal that new changes are happening on the fork:在这里,我添加一个星号来表示分叉上正在发生新的变化:

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:在这种情况下,您尝试在G*处分支出 master 并且樱桃选择以前的提交会给您警告:

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 answer from Schwern.在这里,我扩展了 Schwern 的这个很棒的答案

  2. To get the last n commit hashes it may be useful to use: git log --pretty=oneline --abbrev-commit | head -n要获取最后n提交哈希,使用以下命令可能很有用: git log --pretty=oneline --abbrev-commit | head -n git log --pretty=oneline --abbrev-commit | head -n

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 中选择此分支并创建拉取请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM