简体   繁体   English

使用 git pull request 为开源项目做出贡献的工作流程是什么? (例如通过 Github)

[英]What's the workflow to contribute to an open source project using git pull requests? (eg. via Github)

我对我如何做到这一点有一个全面的逐步描述,我想在这里分享它,以便开发人员可以从中受益(我会回答我自己的问题)。

Since changes contributed to open source projects will have to be peer reviewed it's common to see a workflow which relies on git pull requests.由于对开源项目所做的更改必须经过同行评审,因此通常会看到依赖于 git pull 请求的工作流。 Pull requests are not allowed from directly cloned repos (you need your own fork).不允许从直接克隆的 repos 中提取请求(您需要自己的 fork)。 So these are the steps I follow to maintain a healthy fork and contribute to an open source periodically:因此,我遵循以下步骤来维护一个健康的分叉并定期为开源做出贡献:

Note: Steps 1, 2 and 3 are only done once per project on a single development machine to set everything up.注意:步骤 1、2 和 3 仅在单个开发机器上为每个项目执行一次以设置所有内容。

Note 2: If the open source project that you will be contributing to doesn't use master as the default working branch you will have to replace all reference to 'master' in the commands of steps 4) and 7) below with the name of that branch.注意 2:如果您将贡献的开源项目不使用 master 作为默认工作分支,您必须将下面步骤 4) 和 7) 的命令中对“master”的所有引用替换为名称那个分支。

1) Make sure you're working locally on your "fork" of the project rather than on a cloned repository pointing to the project as origin. 1)确保您在项目的“分支”上本地工作,而不是在指向项目作为源的克隆存储库上工作。 In order to fork a Github project go to https://github.com/entity/project , click on "Fork" and choose a suitable GitHub account for the fork eg.要分叉 Github 项目,请访问https://github.com/entity/project ,单击“分叉”并为分叉选择合适的 GitHub 帐户,例如。 your personal Github account.您的个人 Github 帐户。 Note that your forked project "origin" will be no longer the original project repo but your own fork on Github.请注意,您的分叉项目“起源”将不再是原始项目存储库,而是您自己在 Github 上的分叉。 Be careful with privacy if you're forking a private project since you probably don't want your fork to be public.如果您要分叉一个私人项目,请注意隐私,因为您可能不希望您的分叉公开。

2) Clone your own project fork into your development machine and cd into that directory. 2)将您自己的项目 fork 克隆到您的开发机器中,然后 cd 进入该目录。

git clone git@github.com:yourgithubuser/project.git

3) Add the original project repo as upstream repository in your forked project. 3)将原始项目存储库添加为您的分叉项目中的上游存储库。

git remote add upstream git@github.com:entity/project.git

The original main project repo is now "upstream" but not "origin"原来的主要项目回购现在是“上游”,但不是“起源”

And now comes the work loop that you'll repeat when you work with your forked project:现在是您在处理分叉项目时将重复的工作循环:

4) Before starting your work always make sure the master branch of your forked repo is synchronized with the master branch of the original project repo: 4)在开始工作之前,请务必确保您的分叉仓库的主分支与原始项目仓库的主分支同步:

git checkout master
git fetch upstream
git merge upstream/master
git push origin master

5) Create a new branch in your project fork for the specific fixes that you want to contribute (name it after either a bugfix, a tracker issue, a documentation section, etc) and switch to it. 5)在您的项目分支中为您想要贡献的特定修复创建一个新分支(在错误修复、跟踪器问题、文档部分等之后命名)并切换到它。

git checkout -b myfixes

This automatically creates the branch and switches to it.这会自动创建分支并切换到它。 Make sure the branch does not exist already .确保分支不存在 You might also want to get rid of your old fix branches that have already been merged into the docs (otherwise you'll have tons of useless branches in your project).您可能还想摆脱已经合并到文档中的旧修复分支(否则您的项目中将有大量无用的分支)。 You can take a look at your local branches by issuing a git branch and if in that list you find a branch that has already been merged with the upstream project then you can do:您可以通过发出git branch查看本地git branch ,如果在该列表中您找到已与上游项目合并的分支,则可以执行以下操作:

git branch -D myoldfixes
git push origin --delete myoldfixes

Important note : if you were already working on a branch on a different machine and want to continue that work on a new machine you need to redo steps 2, 3 and 4 on the new machine and in step 5 instead of doing git checkout -b myfixes you should do git checkout myfixes (remove the -b).重要提示:如果您已经在另一台机器上的分支上工作,并希望在新机器上继续该工作,您需要在新机器上重做步骤 2、3 和 4,并在步骤 5 中而不是执行git checkout -b myfixes你应该做git checkout myfixes (删除 -b)。 Otherwise you can end up with a "detached head" state which is not good (kind of an anonymous branch)否则,您最终可能会出现一个不好的“分离头”状态(一种匿名分支)

6) Work on that branch (eg. myfixes) and commit your changes: 6)在该分支上工作(例如myfixes)并提交您的更改:

git commit -a -m "My fixes"

(Alternatively you can stage specific files and commit without using -a. You can commit as many times as you want but don't leave uncommited changes in the branch) (或者,您可以在不使用 -a 的情况下暂存特定文件并提交。您可以根据需要多次提交,但不要在分支中留下未提交的更改)

7) While you were working on your fixes the original upstream project repo might have changed (due to other contributors working on it). 7)当您进行修复时,原始上游项目 repo 可能已经更改(由于其他贡献者正在处理它)。 So first you'll have to rebase your current branch (myfixes) from the upstream destination branch.因此,首先您必须从上游目标分支重新定位当前分支(myfixes)。 In other words you need to replay your fixes on top of that latest work from upstream repo master branch to make sure your commits are still compatible with the latest commits in upstream.换句话说,您需要在上游 repo master 分支的最新工作之上重播您的修复,以确保您的提交仍然与上游中的最新提交兼容。 This will result in a fast-forward merge for the pull request which is what we want:这将导致我们想要的拉取请求的快进合并:

git checkout myfixes
git pull --rebase upstream master

Note 3 : this can result in conflicts but this is normal, fixing them is part of the process (this happens more often on very active projects)注意 3 :这可能会导致冲突,但这是正常的,修复它们是过程的一部分(这种情况在非常活跃的项目中更常见)

Note 4 : if you have many commits in the branch and you're a considerate person you might want to squash your commits into a single one for the benefit of the maintainer of the original project注意 4 :如果您在分支中有许多提交并且您是一个体贴的人,您可能希望将您的提交压缩为一个,以使原始项目的维护者受益

8) After fixing the conflicts (if any) of the previous step you have applied your fixes on top of the latest version of upstream master. 8)修复上一步的冲突(如果有)后,您已将修复应用到最新版本的上游 master 之上。 Since the pull requests are initiated from your forked repository on Github you want to keep that one in sync too:由于拉取请求是从您在 Github 上的分叉存储库发起的,因此您也希望保持同步:

git checkout myfixes
git push origin myfixes -f

9) Finally, you can go to Github https://github.com/entity/project (the original project not your fork) and click on "Pull Request". 9)最后,你可以去Github https://github.com/entity/project (原来的项目不是你的fork),点击“Pull Request”。 Make sure you choose upstream repo "master" as the destination branch and your forked repo "myfixes" as the source branch (the branch will be automatically removed for you after the pull request is accepted)确保您选择上游仓库“master”作为目标分支,并将您的分叉仓库“myfixes”作为源分支(在拉取请求被接受后,该分支将自动为您删除)

Enjoy!享受!

6a) The topic of the commit and format of the commit message are also critical. 6a)提交的主题和提交消息的格式也很关键。

Commit topic提交主题

The commit topic should cover just one logical change.提交主题应仅涵盖一项逻辑更改。 For example, if you were to describe the changes to someone (for example in a commit message, see below) you should not be able to use the word "and" to describe the topic, ex.例如,如果您要向某人描述更改(例如在提交消息中,见下文),您不应该使用“和”一词来描述主题,例如。 not "Fix bug 123 and update config default."不是“修复错误 123 并更新配置默认值。” These should be two separate commits.这些应该是两个单独的提交。

If you have a bunch of separate issues addressed but uncommitted in your working tree, a great tool is the interactive add.如果您在工作树中有一堆单独的问题已解决但未提交,那么交互式添加是一个很好的工具。 Have your fingers remember this batch of commands, then follow the directions:让你的手指记住这批命令,然后按照说明进行操作:

git add -i
5<CR>
*<CR>

You can get fancier with the other options, but that says "show me every change in my working tree and let me pick what to stage."您可以对其他选项更感兴趣,但这就是说“向我展示我工作树中的每一个变化,让我选择要上演的内容。”

Then, as usual:然后,像往常一样:

git ci

Commit message提交消息

You want to be terse and to the point in the title for people scanning the history, while also providing good details in the body for people in the future (including yourself in six months!) digging in to an issue that involves your change.您希望在标题中简洁明了,以便人们浏览历史,同时还为未来(包括六个月后的您自己!)深入研究涉及您的更改的问题提供良好的正文细节。

My favorite guide to writing good commit messages is theErlang/OTP wiki page on good commit messages , and to that I'll add that you can't go wrong with the following format:我最喜欢的编写好提交消息的指南是Erlang/OTP wiki page on good commit messages ,我要补充一点,使用以下格式不会出错:

Short (<50 chars) present-tense summary of changes

Previously, <a couple sentences clearly describing the previous behavior
and the resulting customer issue>. 

This commit <a sentence or two clearly describing the code change, and the
resulting expected behavior>.

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

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