vimer linux kernel 爱好者

git pull-request使用简介

2018-06-12
git

考虑这样一个场景:一个公司围绕一个产品开发了主分支-master,当然,不可能每个 人都在主分支上做开发对不对,好,我们使用PR进行多部门的开发.

请注意,下面2-4步,是基于自己在github建立的git 仓库的事实,如果你想为其他开源软件做贡献,肯定不行,请看 给他人提交pull request

step 0

首先要Frok别人的repo,fork, fork.

step 1 : 克隆仓库

首先,先克隆一个git仓库,这个不用说太多。我以自己的仓库为例,进行代码演示。

git clone https://github.com/yuzibo/yuzibo.github.io.git
正克隆到 'yuzibo.github.io'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 2931 (delta 2), reused 8 (delta 2), pack-reused 2922
接收对象中: 100% (2931/2931), 1010.61 KiB | 502.00 KiB/s, 完成.
处理 delta 中: 100% (1939/1939), 完成.

当然,这个时候默认为master主分支。

cd yuzibo.github.io/
yubo@debian:~/yuzibo.github.io$ git branch
* master

step 2 : 新建分支

这个分支就是你自己要开发的分支,记住一点,实时的rebase master分支。

git checkout -b test-pr
git rebase origin/master  # 首先在主分支使用fetch

step 3 : 产生request pull

在你开发完成后,为了将自己的工作合并到主分支中,需要产生request pull,让维护人接受你的改变。

这里的使用是标准的git开发流程git add && commit.接下来,在你的开发分支上,使用以下命令,我们可以看到:

git push -u
fatal: 当前分支 test-pr 没有对应的上游分支。
为推送当前分支并建立与远程上游的跟踪,使用

    git push --set-upstream origin test-pr

提示的非常明显,第一步,我以github网站为例,首先在网站上合并rp,另外一种是命令行。

上面的提示命令我简写一步:


git push -u origin test-pr
对象计数中: 4, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (4/4), 完成.
写入对象中: 100% (4/4), 624 bytes | 0 bytes/s, 完成.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'test-pr' on GitHub by visiting:
remote:      https://github.com/yuzibo/yuzibo.github.io/pull/new/test-pr
remote:
To https://github.com/yuzibo/yuzibo.github.io.git
* [new branch]      test-pr -> test-pr
分支 test-pr 设置为跟踪来自 origin 的远程分支 test-pr。

这个提示就说明在网站上有了rp请求,现在我的图床没法使用了,图片就先不适用了。你可以到github的仓库上去看一下,根据提示你应该没问题的,有问题的话,可以再找我咨询。

有人会问这个request pull不一定找得到,根据提示你就可以访问URL即可。 下面我贴一些详细的命令,效果是类似的.

git push --set-upstream origin test-pr

对象计数中: 4, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (4/4), 完成.
写入对象中: 100% (4/4), 1.84 KiB | 0 bytes/s, 完成.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'test-pr' on GitHub by visiting:
remote:      https://github.com/yuzibo/yuzibo.github.io/pull/new/test-pr
remote:
To https://github.com/yuzibo/yuzibo.github.io.git
* [new branch]      test-pr -> test-pr
分支 test-pr 设置为跟踪来自 origin 的远程分支 test-pr。

step 4 : 更新

这里就是更新你的master.其他的分支接着跟进.

git checkout master
git fetch origin master
git rebase origin/master

给人仓库产生pull request请求

上面我说了,是基于自己的github账号,没有权限问题,但是他人的git 仓库恐怕不行。这个问题,你需要先fork之后才可以.下面我以自己的例子说明这个操作。

1 首先fork

首先在github fork别人的仓库,这样在你的github主页上就会显示fork的仓库。

#1 I forked the https://github.com/netoptimizer/prototype-kernel
#2 prototype-kernel
Forked from netoptimizer/prototype-kernel
Prototyping kernel development work outside mainline

# 或者
yuzibo/prototype-kernel
forked from netoptimizer/prototype-kernel

2. clone

接着clone 在你github上的forked git 仓库,记住,不是原官方仓库。

https://github.com/yuzibo/prototype-kernel.git

3. 新建分支

接着创建一个产生pull request的分支。

git checkout -b test

4. commit

接下来就是你自己commited自己的修改就可以了。

5. push

git push origin -u test

就可以,这时候在你的github(你forked的仓库)上就有了test分支,此时他会提示你产生pull request的。

6. force-push

这个提示经常在github上的PR log中见到的问题。也就是比如,你的pull branch需要修改,就会遇到此类的问题。我这里找到一个(网页)[https://blog.adamspiers.org/2015/03/24/why-and-how-to-correctly-amend-github-pull-requests/],介绍的有点过于理论,还可以参考这个网页我是这样操作的:

在已经 pull request的分支上,你的commmit已经保留。然后在位于HEAD的commit

#1  --amend commit

git commit --amend
# --amend不会产生新的commit id,是在原commit上的修改

#2 git add
git add -A

#3 git push
git push  -f origin HEAD
对象计数中: 6, 完成.
Delta compression using up to 12 threads.
压缩对象中: 100% (5/5), 完成.
写入对象中: 100% (6/6), 616 bytes | 616.00 KiB/s, 完成.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/yuzibo/connectedhomeip.git
 + 5794aea16...39d62156c HEAD -> fix_install_dep (forced update)

这样,就算你已经push到原git repo,这样的push commit后,新的commit就已经上传到你的pull request上了。

命令行

上面的步骤是通过网站,这可能对于有些人来说不是很友好,下面介绍下如何通过命令行进行开发.

首先,这个操作和上面的想法不太一样,来自stackoverflow。思路就是将自己开发分支的特性合并到master上去,让别人审阅master分支,而不是自己的开发分支。如下面这种情况:

  A---B---C feature/awesomeFeature
 /
D---E---F---G origin/master
              master

使用下面的命令即可完成这个工作.

git request-pull origin/master feature/awesomeFeature

对于我的开发来说就是这个样子的:

git request-pull origin/master test-prfatal: 'test-pr' does not appear to be a git repository
fatal: 无法读取远程仓库。

请确认您有正确的访问权限并且仓库存在。
warn: No match for commit c3165d2416cdde49be09334f531e58379172567b found at test-pr
warn: Are you sure you pushed 'HEAD' there?
The following changes since commit c3165d2416cdde49be09334f531e58379172567b:

Merge pull request #5 from yuzibo/test-pr (2018-11-02 10:57:30 +0800)

are available in the git repository at:

test-pr

for you to fetch changes up to c3165d2416cdde49be09334f531e58379172567b:

Merge pull request #5 from yuzibo/test-pr (2018-11-02 10:57:30 +0800)

然后你在master分支里,就看到最新的变化了,这样就省掉了网站request pull这一步。但是,如果你的开发分支不在同一个工作区如何操作?比如说两个电脑这种情况?


Comments

Content