Github教程(3)

时间:2022-07-20 08:30:40

Pull Request

Pull Request 是自己修改源代码后,请求对方仓库采纳该修改时采取的一种行为。

场景1:

用户A在fork完用户B的项目时,A修改了代码并提交了一个Pull Request给B用户,B用户Merge了A用户提交的Pull Request。

假设需要fork以下项目:

https://github.com/grey927/TestForkPR

点击账户右上角的fork按钮,fork项目到自己账户下

Github教程(3)

Github教程(3)

打开Git Bash

将项目clone到本地:

$ git clone https://github.com/GreyZeng/TestForkPR.git

Cloning into 'TestForkPR'...

remote: Counting objects: , done.

remote: Total  (delta ), reused  (delta ), pack-reused 

Unpacking objects: % (/), done.

Checking connectivity... done.

进入TestForkPR目录,查看分支状态:

$ git branch -a

* master

remotes/origin/HEAD -> origin/master

remotes/origin/master

在TestForkPR目录下增加一个文件,T.java

增加到暂存区:

$ git add T.java

提交修改:

$ git commit -m "this is test"

[master 2b72d0f] this is test

 file changed,  insertions(+)

create mode  T.java

Push到自己账户下的远程仓库下:

$ git push -u origin master

Counting objects: , done.

Delta compression using up to  threads.

Compressing objects: % (/), done.

Writing objects: % (/),  bytes |  bytes/s, done.

Total  (delta ), reused  (delta )

To https://github.com/GreyZeng/TestForkPR.git

daaf13f..2b72d0f master -> master

Branch master set up to track remote branch master from origin.

打开自己账户的远程仓库,点击New Pull Request按钮:

Github教程(3)

弹出文件差异对比界面

Github教程(3)

确认无误时,点击Create pull request

Github教程(3)

写上相关描述并点击Create pull request:

Github教程(3)

此时,原仓库会收到一条pull request的信息:

Github教程(3)

Github教程(3)

点击进入这条信息,

Github教程(3)

点击Merge pull request,即可接受这个pull request的请求。

Github教程(3)

场景2:

直接clone原仓库到本地,不执行fork操作,并同时使用fetch/merge命令使本地仓库和原仓库保持代码一致。

举例:

原仓库的地址:https://github.com/grey927/TestFetchMerge

直接clone至本地:

$ git clone https://github.com/grey927/TestFetchMerge.git

Cloning into 'TestFetchMerge'...

remote: Counting objects: , done.

remote: Total  (delta ), reused  (delta ), pack-reused 

Unpacking objects: % (/), done.

Checking connectivity... done.

给原仓库设置upstream的名称,将其作为远程仓库。

$ git remote add upstream https://github.com/grey927/TestFetchMerge.git

我们每次只要从原仓库获取最新源码,并和本地分支进行合并即可:

获取最新源码:

$ git fetch upstream

remote: Counting objects: , done.

remote: Compressing objects: % (/), done.

remote: Total  (delta ), reused  (delta ), pack-reused 

Unpacking objects: % (/), done.

From https://github.com/grey927/TestFetchMerge

* [new branch] master -> upstream/master

合并:

$ git merge upstream/master

Updating e2215cc..a215a8a

Fast-forward

newfile.txt |  +

 file changed,  insertion(+)

create mode  newfile.txt