GitHub 的 Pull Request 是指什么意思?
Pull Request场景
在使用GitHub贡献代码时,经常会遇到一个操作,就是Pull Request的。那么这个指令到底什么意思呢?明明是提交代码,为什么叫Pull Request呢?当然,大家也会简称PR。
可用下面考试之后老师改卷的场景来类比说明一下:
学生做的试卷就像仓库,试卷中的错误就相当于程序里的bug。老师把试卷拿过来,相当于先fork。在卷子上做一些修改批注,相当于git commit。最后把改好的试卷给学生,相当于发pull request,学生拿到试卷重新改正错误,相当于merge。
共享代码的流程
当想共享代码到别人的仓库时,一般会走如下流程:
(1)先fork别人的仓库,相当于拷贝一份,直观上是在自己的GitHub账户里复制了一个项目。
(2)clone(自己fork的项目)到本地分支,做一些 bug fix;
(3)发起pull request给原仓库;
(4)原仓库管理者,review这个bug,如果修改没问题,则将你的pull request进行合并。
这样,一个完整pull request的过程就结束了。
GitHub示例
fork别人的仓库代码:

到自己的仓库:

在开发者的机器上git clone这个fork来的仓库,并进行bug修复:
~ git clone https://github.com/beepony/bootstrap.git ~ cd bootstrap ~ git checkout -b test-pr ~ git add . && git commit -m "test-pr" ~ git push origin test-pr
完成修改之后,回到 test-pr 分支,点击旁边绿色的 Compare & pull request 按钮:

添加注释信息,确认提交:

作者看到对应的pull request,核查无误进行merge,合并到原项目中。
难以理解的pull request
对于初学者来说,GitHub的Pull Request的确让人难以理解,国内外各大网站都可以看到用户的声音:

明明是提交代码,为什么是pull request?
概括地说,pull request是一个request,这个request的内容是:请求对方运行一下git的pull命令。

使用过git的应该都知道git pull,这个命名相当于连续执行两个命令git fetch然后git merge。所以,pull request的意思就是一个“请求”(request),请对方做一个git fetch拿到request中的代码commits,然后git merge一下到某个分支上。
pull request简称PR,是github的概念,不是git的概念,所以也不是立刻出结果的,需要有人去同意完成这个PR,当然同意完成PR的可以是自己,也可以拒绝PR。现实项目中,即使自己对某个repo的特定分枝有写入权限,也可以提PR来修改,因为这是code review的一种方式,让团队的其他人看一看改的咋样。

总之,可以将pull request当做一个专业术语来用,而忽略本身的语法,或许可以更好的记忆和实践。
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接