利用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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章