利用git制作和提交patch

今天提了一个电源管理的patch,算是一个好的开端吧。写下整个patch提交的过程,为后来人引路。

一般在新的系统上,我们都需要先配置下自己的 Git 工作环境。最重要的是配置你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录:

$ git config --global user.name "Zhang xxx"
$ git config --global user.email [email protected]

要检查已有的配置信息,可以使用$ git config --list 或 更加简洁的$ git config -l
b46552@rock:~/linux-pm$ git config -l
[email protected]
user.name=xxx Zhang
remote.review.url=gerrit:infrastructure/legal-review
remote.review.receivepack=git receive-pack
remote.review.push=HEAD:refs/for/master
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

也可以单独查看某一项$ git config user.name
b46552@rock:~/linux-pm$ git config user.name
xxx Zhang

Linux opensource的代码review主要靠maillist,也有一些公司内部的SDK开发还会用到基于web并整合了git的代码review的gerrit,这点下次再讲解

PM(电源管理)有一个单独的git tree,由rafael维护(可以通过vi linux-devel/MAINTAINER; 查找 linux-pm Maintainer列表找到)
首先从服务器上将linux-pm tree clone下来。$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git
好了现在本地源码有了,开始修改代码吧
比如修改了
file1.c
file2.c
file3.c
此时三个文件的状态可以通过git status来查看
b46552@rock:~/linux-pm$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   drivers/cpufreq/ppc-corenet-cpufreq.c(红色字体)
#
no changes added to commit (use "git add" and/or "git commit -a")


如果你想把它们放到一个patch里。可以git add file1.c file2.c file3.c加入暂存区,
再次git status,我们看到
b46552@rock:~/linux-pm$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   drivers/cpufreq/ppc-corenet-cpufreq.c(绿色字体)
#

现在你就可以通过git commit -s来提交修改了。
-s意思是sign off,就是作者签名,留下email方便以后的人查找。
此时会进入编辑状态
  1
  2
  3 Signed-off-by: xxx Zhang <[email protected]>
  4
  5 # Please enter the commit message for your changes. Lines starting
  6 # with '#' will be ignored, and an empty message aborts the commit.
  7 # On branch master
  8 # Changes to be committed:
  9 #   (use "git reset HEAD <file>..." to unstage)
 10 #
 11 #       modified:   drivers/cpufreq/ppc-corenet-cpufreq.c
 12 #
第一行显示的是补丁文件头部的Subject信息,第二行起可以输入补丁文件的注释信息详细说明,一般每行不超过80个字符。

标题和注释的写法可以找个别人的patch效仿一下。

现在你修改的代码已经提交到本地,通过git log可以查看
b46552@rock:~/linux-pm$ git log
commit 850f272da031764b27ae63d0d34a158b7563d5fa
Author: xxx Zhang <[email protected]>
Date:   Mon Mar 17 14:10:21 2014 +0800

    cpufreq: powerpc: add cpufreq transition latency for FSL e500mc Socs

    According to the data provided by HW Team, at least 12 internal platform
    clock cycles are required to stabilize a DFS clock switch on FSL e500mc Socs.
    This patch replaces the CPUFREQ_ETERNAL with appropriate HW clock transition
    latency to make DFS governors work normally on example e500mc boards.

    Signed-off-by: xxx Zhang <[email protected]>


接下来就是制作patch了
git format-patch 可以在当前目录下自动生成标准化的patch,

例如我的patch就生成为0001-cpufreq-powerpc-add-cpufreq-transition-latency-for-F.patch

OK,patch制作好了,就可以向opensource社区提交了
git send-email --to [email protected] --cc [email protected] -cc [email protected] 0001-cpufreq-powerpc-replace-CPUFREQ_ETERNAL-with-cpufreq.patch --smtp-server=remotesmtp.example.net
这里 --to指定发给谁,-cc指定抄送给谁,另外需要指定你的邮箱的smtp服务器,也可以在linux-pm/.git/config文件中指定。
b46552@rock:~/linux-pm$ git send-email --to [email protected] --cc [email protected] -cc [email protected] -cc [email protected] -cc [email protected] -cc xxx.Zhang@f
example.com 0001-cpufreq-powerpc-add-cpufreq-transition-latency-for-F.patch --smtp-server=remotesmtp.example.net
0001-cpufreq-powerpc-add-cpufreq-transition-latency-for-F.patch
(mbox) Adding cc: xxx Zhang <[email protected]> from line 'From: xxx Zhang <[email protected]>'
(body) Adding cc: xxx Zhang <[email protected]> from line 'Signed-off-by: xxx Zhang <[email protected]>'

From: xxx Zhang <[email protected]>
To: [email protected]
Cc: [email protected],
    [email protected],
    [email protected],
    [email protected],
    [email protected]
Subject: [PATCH] cpufreq: powerpc: add cpufreq transition latency for FSL e500mc Socs
Date: Mon, 17 Mar 2014 14:24:57 +0800
Message-Id: <[email protected]>
X-Mailer: git-send-email 1.8.4

    The Cc list above has been expanded by additional
    addresses found in the patch commit message. By default
    send-email prompts before sending whenever this occurs.
    This behavior is controlled by the sendemail.confirm
    configuration setting.

    For additional information, run 'git send-email --help'.
    To retain the current behavior, but squelch this message,
    run 'git config --global sendemail.confirm auto'.

Send this email? ([y]es|[n]o|[q]uit|[a]ll): y
OK. Log says:
Server: remotesmtp.example.net
MAIL FROM:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
RCPT TO:<[email protected]>
From: xxx Zhang <[email protected]>
To: [email protected]
Cc: [email protected],
    [email protected],
    [email protected],
    [email protected],
    [email protected]
Subject: [PATCH] cpufreq: powerpc: add cpufreq transition latency for FSL e500mc Socs
Date: Mon, 17 Mar 2014 14:24:57 +0800
Message-Id: <[email protected]>
X-Mailer: git-send-email 1.8.4

Result: 250 2.0.0 s2H77o6H024754 Message accepted for delivery

至此patch提交成功,等待Maintainer的回信吧。


其他命令:
希望将一个文件从暂存区删除,即希望取消git add文件的操作,可以使用
$ git reset HEAD <file>

希望提交的commit回滚,有3个命令
git reset -soft :取消git commit 
git reset -mixed(缺省):取消git commit和git add
git reset -hard :取消git commit,git add,以及对源文件修改。(慎用,不然辛苦写下的代码就没有了)
例如 git reset 38679ed709fd0a3767b79b93d0fba5bb8dd235f8(commitID)


发布了24 篇原创文章 · 获赞 15 · 访问量 16万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章