git拉取请求

Let’s say you wrote some code on the develop branch. You’re done with what you were working on, and you want to merge it into the master branch.

假设您在develop分支上编写了一些代码。 您已经完成了所要做的工作,并且想要将其合并到master分支中。

But you don’t know whether the code you’ve written is good enough. You want someone to review your code before you merge it into the master branch.

但是您不知道编写的代码是否足够好。 您希望有人在将代码合并到master分支之前对其进行检查。

You can do that with a pull request.

您可以通过请求请求来做到这一点。

(What is a pull request?)

A pull request is short for “requesting for a Git Pull”.

拉取请求是“请求进行Git拉取”的缩写。

This can be confusing to understand, so we’re going to explain it with an analogy.

这可能会让人难以理解,因此我们将以类推的方式进行解释。

Imagine you have a crate of bananas that you want to load onto a ship. The only way to load the bananas is:

假设您有一箱香蕉要装载到船上。 装载香蕉的唯一方法是:

  1. You get someone to throw down a rope
  2. You tie the rope to the crate
  3. They pull the crate up

When they pull the crate up, they’ll want to check whether your bananas are good. If they spot a rotten banana, they might want to request you change that rotten banana for a good one.

当他们拉起板条箱时,他们将要检查您的香蕉是否很好。 如果他们发现烂香蕉,他们可能会要求您将烂香蕉换成好香蕉。

They might also think that all your bananas suck and decide to throw them away. (But let’s hope this doesn’t happen, shall we?)

他们可能还认为您所有的香蕉都烂透了,决定扔掉它们。 (但我们希望这不会发生,对吧?)

In this analogy, the crate of bananas is your code from the develop branch. The ship is the master branch. The sailor is here to check whether your code is good enough for the master branch.

以此类推,香蕉箱是您来自develop分支的代码。 该船是主分支。 水手在这里检查您的代码对于master分支是否足够好。

That’s what a pull request is: You get someone to check your code before merging into another branch.

这就是请求请求:在合并到另一个分支之前,您需要有人检查您的代码。

The only difference between our analogy we used and an actual pull-request is: You don’t ask someone to throw a rope down for you. You bring the rope yourself, you tie it to your bananas at the harbor, and you throw it onto the ship. The only thing the sailor needs to do is check the bananas.

我们使用的类比与实际的拉动请求之间的唯一区别是:您不要求某人为您扔绳子。 您自己携带绳索,将其绑在港口的香蕉上,然后扔到船上。 水手唯一需要做的就是检查香蕉。

This is what we mean by submitting a pull request.

这就是我们提交拉取请求的意思。

There are two ways to submit a pull request. How you do it depends on whether you have write access to the repository.

有两种提交拉取请求的方法。 如何执行取决于您是否对存储库具有写权限。

Before we submit a pull request, we need to make some changes, so we have something to merge. In this case, we’re going to add a heading to the code:

在提交拉取请求之前,我们需要进行一些更改,因此我们需要进行一些合并。 在这种情况下,我们将向代码添加标题:

<!-- The code we're using to submit the pull request --> <h2>I'm a heading</h2>

You need to commit this code into the develop branch. Then, you need to push it to the git remote.

您需要将此代码提交到developer分支。 然后,您需要将其推送到git远程。

It’ll appear in the section I highlighted in the image below.

它会出现在下图中我突出显示的部分中。

If you see the message, great. Click on it. You’ll save a few steps.

如果您看到此消息,那就太好了。 点击它。 您将保存一些步骤。

If you don’t see it, you can click on the pull request tab at the top of your Github repo.

如果看不到它,可以单击Github存储库顶部的请求请求选项卡。

Then, click on the green button that says “new pull request”.

然后,单击绿色按钮,显示“新的拉取请求”。

You’ll come to a page that says “Compare changes”.

您将进入一个页面,显示“比较更改”。

To create a pull request, you want to set the base and compare branches to the following:

要创建拉取请求,您需要设置基础并将分支与以下内容进行比较:

  • base: branch you want to merge to
    base :您要合并到的分支
  • compare: branch you want to merge from
    compare :要合并的分支

In our case, we want to merge to master, so we’ll set base to master. We’re merging from develop, so we’ll set compare to develop.

在本例中,我们希望合并到master,因此我们将base设置为master 。 我们正在从develop合并,因此我们将compare设置为develop

Once you’ve selected your branches, Github will show you the list of commits that have been made. Here, what you need to do is click on the “Create pull request” button.

选择分支后,Github将向您显示已进行的提交的列表。 在这里,您需要做的是单击“创建请求请求”按钮。

Github will show you a page that says “Open a pull request”.

Github将显示一个页面,显示“打开请求请求”。

Note: This is the page you’ll arrive at if you saw the “Your branch has been updated X minutes ago” message I talked about earlier.

注意:如果您看到我之前提到的“您的分支已在X分钟前更新”消息,则将到达此页面。

The title you write will be the title people see in the pull request tab. We’re going to set it to “Add a heading to the index”.

您编写的标题将是人们在拉取请求选项卡中看到的标题。 我们将其设置为“向索引添加标题”。

For the comments, it’s going to be the first message people see when they click into the pull request. In this case, we’ll say “I’ve added a heading. Let me know if its good?”

对于评论,这将是人们单击拉入请求时看到的第一条消息。 在这种情况下,我们会说“我添加了标题。 让我知道它的好处吗?”

Once you’ve added the title and message, you can click on the Create pull request button at the bottom of the page.

添加标题和消息后,您可以单击页面底部的创建请求请求按钮。

Now, if you click on the pull request tab at the top, you’ll see an open pull request that we’ve just submitted.

现在,如果单击顶部的拉取请求选项卡,您将看到我们刚刚提交的未完成的拉取请求。

This is how you create a pull request if you have write access to the repository.

如果您具有对存储库的写访问权,这就是创建请求请求的方式。

If you don’t have write access to a repository, you’ll have to create a Fork. Let’s pause and talk about what a Fork is.

如果您无权访问存储库,则必须创建一个Fork。 让我们停下来谈谈什么是叉子。

(What is a Fork?)

A Fork is NOT the git client you’re using. Don’t get confused!

叉子不是您正在使用的git客户端。 不要困惑!

A fork in Git means a repository that is based off another repository.

Git中的fork表示基于另一个存储库的存储库。

You already learned about branches so far, and you know that a branch can be created from another branch. (Creating a develop branch from master, for example).

到目前为止,您已经了解了分支,并且知道可以从另一个分支创建分支。 (例如,从master创建一个develop分支)。

On the same note, a repository can be created from another repository. The repository that was created from another repository is called a fork.

同样,可以从另一个存储库创建一个存储库。 从另一个存储库创建的存储库称为派生。

The forked repository contains everything the main repository has (at the time it was forked). It includes all tags and branches.

派生的存储库包含主存储库拥有的所有内容(在创建时)。 它包括所有标签和分支。

You own the forked repository (which gives you write access). This forked repository will still be tracked to the main repository. You’re going to make a pull request through this tracked link.

您拥有分叉的存储库(它使您可以进行写访问)。 此分叉的存储库仍将跟踪到主存储库。 您将通过此跟踪链接发出请求请求。

Let’s see how it works in practice.

让我们看看它在实践中是如何工作的。

Note: You cannot fork your own repository. What I’m going to do is use a dummy account to show you how it works. From this point on, take note of these two account names:

注意:您不能派生自己的存储库。 我要做的是使用一个虚拟帐户向您展示其工作方式。 从这一点开始,请注意以下两个帐户名称:

  1. Main account: zellwk 主账户: zellwk
  2. Dummy account: zellwk2.
    虚拟帐户: zellwk2

(I should have created a totally different name… but I think you’ll be able to follow along just fine).

(我应该创建一个完全不同的名称……但是,我认为您可以照做)。

To fork a repository, you click on the fork button that’s on the top-right-hand corner of the repository.

要存储库,请单击存储库右上角的fork按钮。

Once the fork is completed, you’ll see a repository that looks (almost) exactly like the repository you forked from. There is one difference though — if you look at the title of the project, you can see that the project is forked from another repository.

派生完成后,您将看到一个存储库,该存储库看起来(几乎)与您从中进行分叉的存储库完全相同。 但是有一个区别–如果查看项目的标题,您会看到该项目是从另一个存储库中派生的。

You have write access to this forked repository.

您对此分叉存储库具有写访问权。

Here’s what we do when we submit a pull request from a forked repository:

当我们从派生的存储库提交拉取请求时,我们要做的是:

  1. Create a new branch
  2. Write code in that new branch
  3. Send a pull request from that new branch

But for this lesson, I’m not going to create the new branch because I’ll have to set up the dummy account on my Git client (which is a tedious process).

但是在本课程中,我将不会创建新分支,因为我必须在Git客户端上设置虚拟帐户(这是一个繁琐的过程)。

So, for this lesson, we’re going to write in the develop branch directly. Then, we’ll submit a pull request from the develop branch.

因此,在本课程中,我们将直接在develop分支中编写代码。 然后,我们将从develop分支提交一个拉取请求。

In this case, I’m adding a list with the following HTML in the index.html file:

在这种情况下,我将在index.html文件中添加具有以下HTML的列表:

<ul>  <li>Item 1</li>   <li>Item 2</li>   <li>Item 3</li>

Note: I left the <ul> tag opened in this example. We’ll pick this error up when we review this pull request in the next lesson.

注意:在此示例中,我未打开< ul>标签。 在下一课中,当我们审查此拉取请求时,我们将拾取此错误。

The commit message for this changes is: Add list.

进行此更改的提交消息为: Add list

Now, if you go back to your forked repository, you should be able to see a button called “new pull request”. This button is beside the button that lets you switch branches.

现在,如果您返回到派生的存储库,则应该可以看到一个名为“ new pull request”的按钮。 该按钮位于允许您切换分支的按钮旁边。

Click on this new pull request button to create a pull request.

单击此新的请求请求按钮以创建请求请求。

Github will bring you to a page that says “Open a new pull request”. This page looks like the “Open a new pull request” page you saw above.

Github将带您到一个页面,上面显示“打开新的拉取请求”。 该页面看起来像您在上面看到的“打开新的拉取请求”页面。

The difference is you need to set four things:

区别在于您需要设置四件事:

  1. The base fork
  2. The base branch
  3. The head fork
  4. The compare branch

From the above example, we know two things:

从上面的示例中,我们知道两件事:

  1. The base branch is branch you want to merge to
    base分支是您要合并到的分支
  2. The compare branch is the branch you want to merge from
    compare分支是您要合并的分支

This means we should set the following:

这意味着我们应该设置以下内容:

  1. The base fork: zellwk/project 基本分支: zellwk/project
  2. The base branch: master 基础分支: master
  3. The head fork: zellwk2/project 前叉: zellwk2/project
  4. The compare branch: develop 比较分支: develop

What you need to do next is write the pull request title, write the comment, and click on the New Pull Request button.

接下来,您需要编写请求请求的标题,编写注释,然后单击“新建请求请求”按钮。

And you have submitted a pull request from a forked repository.

您已经从派生的存储库中提交了拉取请求。

(Wrapping up)

When you submit a pull request, you ask someone to review your changes before merging them into a branch.

提交拉取请求时,您要求某人检查您的更改,然后再将其合并到分支中。

There are two ways to create a pull request. How you do it depends on whether you have write access to the repository.

有两种创建拉取请求的方法。 如何执行取决于您是否对存储库具有写权限。

If you have write access, you can create a pull request from the repository’s page.

如果您具有写访问权,则可以从存储库页面创建拉取请求。

If you don’t have write access, you need to fork the repository to your own page. Then, you create a pull request from your forked repository.

如果没有写访问权,则需要将存储库派生到自己的页面。 然后,您从派生的存储库中创建一个拉取请求。

Thanks for reading. Did this article help you in any way? If you did, I hope you consider sharing it. You might help someone out. Thank you!