Github教程(3)

作者:Grey

原文地址:Github教程(3)

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项目到自己账户下

打开Git Bash

将项目clone到本地:

1
2
3
4
5
6
7
8
9
10
11
$ git clone https://github.com/GreyZeng/TestForkPR.git

Cloning into 'TestForkPR'...

remote: Counting objects: 3, done.

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

Unpacking objects: 100% (3/3), done.

Checking connectivity... done.

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

1
2
3
4
5
6
7
$ git branch -a

* master

remotes/origin/HEAD -> origin/master

remotes/origin/master

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

增加到暂存区:

1
$ git add T.java

提交修改:

1
2
3
4
5
6
7
$ git commit -m "this is test"

[master 2b72d0f] this is test

1 file changed, 5 insertions(+)

create mode 100644 T.java

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git push -u origin master

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 345 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

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按钮:

弹出文件差异对比界面

确认无误时,点击Create pull request

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

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

点击进入这条信息,

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

场景2:

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

举例:

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

直接clone至本地:

1
2
3
4
5
6
7
8
9
10
11
$ git clone https://github.com/grey927/TestFetchMerge.git

Cloning into 'TestFetchMerge'...

remote: Counting objects: 3, done.

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

Unpacking objects: 100% (3/3), done.

Checking connectivity... done.

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

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

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

获取最新源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ git fetch upstream

remote: Counting objects: 3, done.

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

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

Unpacking objects: 100% (3/3), done.

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

* [new branch] master -> upstream/master

合并:

1
2
3
4
5
6
7
8
9
10
11
$ git merge upstream/master

Updating e2215cc..a215a8a

Fast-forward

newfile.txt | 1 +

1 file changed, 1 insertion(+)

create mode 100644 newfile.txt