将开发分支rebase到最新状态

在本地仓库的work分支下不断进行本地开发。将当前分支设置为work后,对thanks.c进行如下的修改并提交。


$git checkout work

-printf("Thank you guys!\n");

-return 0;

+printf("Thank you so much guys!\n");

+return 1;

$git commit-a


在此以前,提交信息都只有1行,而这次需要输入多行,如下所示。第2行只需另起一空行。


Modify message thanks.c

I really appreciate your efforts.


另外,本地开发成果必须基于最新版的远程仓库(上游仓库)。当前的work分支是以版本2为起点的,而这已经不是最新版。处于最新状态的是刚才进行了同步的本地仓库的master分支。因此,如图1-10所示,只需要将work分支复位基底到master分支的HEAD。

thanks. c内发生了冲突,可以按照与上文所述“合并分支”同样的方法来消除冲突。为了保留上游的修改,并加入自己的开发成果,需要对thanks.c进行如下修改。


$git rebase master

First, rewinding head to replay your work on top of it……

Applying:Modify thanks.c

Using index info to reconstruct a base tree……

Falling back to patching base and 3-way merge……

Auto-merging thanks.c

CONFLICT(content):Merge conflict in thanks.c

Failed to merge in the changes.

Patch failed at 0001 Modify thanks.c

When you have resolved this problem run"git rebase—continue".

If you would prefer to skip this patch, instead run"git rebase—skip".

To restore the original branch and stop rebasing run"git rebase—abort".


将开发分支rebase到最新状态 - 图1

图 1-10 本地开发分支的复位基底

例1-1 thanks.c的冲突标记


<<<<<<<HEAD

printf("Thank you guys!\n");

return 2;

=======

printf("Thank you so much guys!\n");

return 1;

>>>>>>>Modify thanks.c


例1-2 thanks.c的修改结果


printf("Thank you so much guys!\n");

return 2;


因为发生冲突而中断的复位基底,在消除冲突并对文件执行git add命令后,再执行git rebase—continue就会继续。


$git add thanks.c

$git rebase—continue


这样就成功地复位基底到最新版了。这时也可以使用git log确认记录。

用邮件将补丁发送给维护人员

现在,本地仓库的开发终于完成了。Linux内核开发的流程是先以补丁的形式将开发成果发送到邮件列表,经过评估与讨论后再整合到上游的仓库内。可以使用git format-patch将补丁输出为文件,粘贴到平时使用的电子邮件的正文并发送,而Git也备有直接发送邮件的功能—git send-email命令。这里使用这条命令来发送邮件。

另外,在笔者所使用的环境(Ubuntu 10.04、Fedora14)中,git send-mail命令原来是放在与Git不同的git-email数据包里的,因此需要事先下载。

小贴士:通过Git直接发送邮件,就可以避免电子邮件软件出现的换行符等问题。

要将本地仓库的开发成果,即以master分支为起点的work分支的各次提交(目前只有一个),作为补丁以邮件发送,可以执行下列命令。另外,如果事先在配置文件中设置各种选项,就不需要每次都在命令行进行输入。表1-13所示为与选项对应的配置文件的段落名。


$git send-email—to=hello_maintainer@hogeraccho.com—cc=hello-ml@hogeraccho.com—smtp-server=smtp.googlemail.com—smtp-encryption=ssl—smtp-server-port=465—smtp-user=youruser@gmail.com—smtp-pass=yourpw master..work


将开发分支rebase到最新状态 - 图2

将—to、—cc等改成自己的地址,并尝试发送邮件,应当会收到如下列内容的邮件。


From:Munehiro Muuhh Ikeda<m_ikeda@hogeraccho.com>

To:you@your.domain.co.jp

Cc:Munehiro Muuhh Ikeda<m_ikeda@hogeraccho.com>

Subject:[PATCH]Modify message thanks.c

I really appreciate your efforts.


thanks.c|2+-

1 files changed,1 insertions(+),1 deletions(-)

diff—git a/thanks.c b/thanks.c

index c28de46..806371d 100644

—-a/thanks.c

+++b/thanks.c

@@-3,7+3,7@@

int main(void)

{

-

printf("Thank you guys!\n");

+printf("Thank you so much guys!\n");

return 2;

}

1.7.1


提交信息的第1行是邮件标题,从空行之后(即第3行开始)是邮件的正文。

git send-email有非常多的选项,可以进行各种设置。建议浏览帮助页面(man page),进行各种尝试,最后生成最佳的配置文件。