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

教程:系列中开发变更

本教程将演示在同一分支上开发多个变更集的一个简单场景。如果您愿意,您可以跟随操作,使用 sandbox 仓库,并精确地执行其中的命令。

如果您是视觉学习者,您可能更喜欢 这个视频,它大致做了同样的事情(但没有使用 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/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"

现在提交 f17f040 位于 d76195e 之上,而 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 中,我的下限不能高于我的最小值。如果我仍然在本地拥有我的分支,我可以跳过下一步,但为了避免一些常见的陷阱,我将从 gerrit 重新获取整个系列,并要求 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

…并修改当前提交

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 显示为只是一个重新基于。