[ English | Indonesia | 한국어 (대한민국) | español (México) | English (United Kingdom) | Deutsch | 中文 (简体, 中国) ]

教程:开发一系列变更

This tutorial walks through a simple scenario of developing multiple change sets in a series on the same branch. If you wish, you can follow along, using the sandbox repository, executing the commands exactly as they’re laid out.

If you are a visual learner, you may prefer this video which does roughly the same thing (but without git-restack).

我们开始吧!

从一个新 pull 出来的主分支开始:

efried:~/openstack/sandbox$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
efried:~/openstack/sandbox$ git pull --all
<snip>

在工作在一个蓝图(bp)上时,您可能会想要在蓝图后命名本地分支。 在此示例中,我们将使用 bp/nova-cyborg-interaction

efried:~/openstack/sandbox$ git checkout -b bp/nova-cyborg-interaction
Switched to a new branch 'bp/nova-cyborg-interaction'
efried:~/openstack/sandbox$ git log --oneline -1 --decorate
3d008a3 (HEAD -> bp/nova-cyborg-interaction, origin/master, origin/HEAD, gerrit/master, master) Merge "suggest change  punctuation to english punctuantion"

当您执行 git commit (不带 --amend 选项)时,您将在开始的任何提交之上创建一个新的提交。 如果您从一个干净的,新的 pull 出来的主分支开始,那将是主分支中最新合并的提交。 在此示例中,是提交 3d008a3

假设我对第一个补丁进行了编辑并提交:

efried:~/openstack/sandbox$ echo 'python-cyborgclient>=1.0' >> requirements.txt
efried:~/openstack/sandbox$ echo 'python-cyborgclient==1.1' >> lower-constraints.txt
efried:~/openstack/sandbox$ git add -A
efried:~/openstack/sandbox$ git commit -m "Add cyborg client to requirements"
[bp/nova-cyborg-interaction d76195e] Add cyborg client to requirements
 2 files changed, 2 insertions(+)
 create mode 100644 lower-constraints.txt
efried:~/openstack/sandbox$ git log --oneline -2 --decorate
d76195e (HEAD -> bp/nova-cyborg-interaction) Add cyborg client to requirements
3d008a3 (origin/master, origin/HEAD, gerrit/master, master) Merge "suggest change  punctuation to english punctuantion"

我刚刚在 3d008a3 之上提交了 d76195e 。 您会注意到我的分支名称( bp/nova-cyborg-interaction )与我一同出现了。

现在,我将进行另一项更改,但其中一部分是正在进行的提交:

efried:~/openstack/sandbox$ mkdir -p nova/pci/cyborg
efried:~/openstack/sandbox$ touch nova/pci/cyborg/__init__.py
efried:~/openstack/sandbox$ git add nova/pci/cyborg/__init__.py
efried:~/openstack/sandbox$ git commit -m "WIP: Cyborg PCI handling"
[bp/nova-cyborg-interaction f17f040] WIP: Cyborg PCI handling
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 nova/pci/cyborg/__init__.py
efried:~/openstack/sandbox$ git log --oneline -3 --decorate
f17f040 (HEAD -> bp/nova-cyborg-interaction) WIP: Cyborg PCI handling
d76195e Add cyborg client to requirements
3d008a3 (origin/master, origin/HEAD, gerrit/master, master) Merge "suggest change  punctuation to english punctuantion"

现在提交 f17f040d76195e 之上,而 d76195e 仍然在 3d008a3 (也就是 master )之上。 请注意,我的分支名称又来了。

到此处,我将我的一系列提交推到gerrit。 请注意,这使我确认我真的想一次推送两次提交。

efried:~/openstack/sandbox$ git review
You are about to submit multiple commits. This is expected if you are
submitting a commit that is dependent on one or more in-review
commits, or if you are submitting multiple self-contained but
dependent changes. Otherwise you should consider squashing your
changes into one commit before submitting (for indivisible changes) or
submitting from separate branches (for independent changes).

The outstanding commits are:

f17f040 (HEAD -> bp/nova-cyborg-interaction) WIP: Cyborg PCI handling
d76195e Add cyborg client to requirements

Do you really want to submit the above commits?
Type 'yes' to confirm, other to cancel: yes
remote:
remote: Processing changes: new: 2, refs: 2
remote: Processing changes: new: 2, refs: 2
remote: Processing changes: new: 2, refs: 2, done
remote:
remote: New Changes:
remote:   https://review.opendev.org/635341 Add cyborg client to requirements
remote:   https://review.opendev.org/635342 WIP: Cyborg PCI handling
remote:
To ssh://review.opendev.org:29418/opendev/sandbox.git
 * [new branch]      HEAD -> refs/for/master%topic=bp/nova-cyborg-interaction

现在,如果您转到这些链接中的任何一个-例如 https://review.opendev.org/#/c/635342/-您会在右上角看到补丁被堆叠在一起。

但是,糟糕,我在第一次提交时犯了一个错误。 我的下限不能高于 requirements.txt 中的下限。 如果我仍然在本地拥有分支机构,则可以跳过此下一步,但为避免某些常见的困难,我会通过要求 git-review 抓住*上层*更改来重新编写整个系列 :

efried:~/openstack/sandbox$ git review -d 635342
Downloading refs/changes/42/635342/1 from gerrit
Switched to branch "review/eric_fried/bp/nova-cyborg-interaction"

现在,我处在最上层更改上了(您会发现它恰好与我推送它之前完全相同 - 再次,这意味着从技术上讲我可以从原来的位置继续工作,但请参见上文):

efried:~/openstack/sandbox$ git log --oneline -3 --decorate
f17f040 (HEAD -> review/eric_fried/bp/nova-cyborg-interaction, bp/nova-cyborg-interaction) WIP: Cyborg PCI handling
d76195e Add cyborg client to requirements
3d008a3 (origin/master, origin/HEAD, gerrit/master, master) Merge "suggest change  punctuation to english punctuantion"

但是我想编辑 d76195e ,同时将 f17f040 正确堆叠在上面。 在这里,我使用了一个名为 git-restack 的工具(运行 pip install git-restack 进行安装)。

efried:~/openstack/sandbox$ git restack

这使我进入编辑器,向我显示任何我所处位置与主分支之间的所有提交(现在,它们处于最高优先顺序):

pick d76195e Add cyborg client to requirements
pick f17f040 WIP: Cyborg PCI handling
<snip>

我想修复第一个,因此将 pick 更改为 edit

edit d76195e Add cyborg client to requirements
pick f17f040 WIP: Cyborg PCI handling
<snip>

保存并退出编辑器,我会看到:

Stopped at d76195e...  Add cyborg client to requirements
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

我修复了 lower-constraints.txt

efried:~/openstack/sandbox$ sed -i 's/cyborgclient==1.1/cyborgclient==1.0/' lower-constraints.txt

…和*修正(amend)*当前提交

efried:~/openstack/sandbox$ git commit -a --amend --no-edit
[detached HEAD df226f7] Add cyborg client to requirements
 Date: Wed Feb 6 16:15:30 2019 -0600
 2 files changed, 2 insertions(+)
 create mode 100644 lower-constraints.txt

…然后告诉 git-restack 继续

efried:~/openstack/sandbox$ git rebase --continue
Successfully rebased and updated refs/heads/review/eric_fried/bp/nova-cyborg-interaction.

如果我有一个更高的系列,并且已经为超过一次提交将 pick 更改成了 edit ,那么我现在可能处于我需要编辑的下一个提交上。 实际上,这是我唯一需要做的事情,所以我已经完成并再次处于我的系列的顶部。

efried:~/openstack/sandbox$ git log --oneline -3 --decorate
e937eef (HEAD -> review/eric_fried/bp/nova-cyborg-interaction) WIP: Cyborg PCI handling
df226f7 Add cyborg client to requirements
3d008a3 (origin/master, origin/HEAD, gerrit/master, master) Merge "suggest change  punctuation to english punctuantion"

请注意,两个提交都已更改了提交哈希值(但对于 master 则没有)。 顶层的一个发生了变化,因为它重新基于了中间版本的新版本。

现在,如果我将该系列备份回gerrit,则会得到相同的确认提示,并且两个更改都将获得一个新的补丁集。 如果您查看gerrit中最顶层的补丁,您会看到补丁集2只是一个更新基础代码而已。