使用Git製作和提交patch
背景概況
現在很多大型的開源項目,如:Linux, U-Boot,都是通過patch來提交代碼的。
普通開發者從軟件倉庫git clone下代碼,修改代碼、製作patch,最後用E-mail發送給對應代碼維護者就好了。
Git最初作爲Linux的版本控制工具,提供了透明、完整、穩定的patch功能。
Git提供了兩種簡單的patch方案。一是使用git diff生成的標準patch,二是使用git format-patch生成的Git專用patch。
git diff生成的標準patch
爲了修改代碼,我們一般的做法是建立一個新分支:test
$ git branch
* master
$ git branch test
$ git checkout test
Switched to branch 'test'
$ git branch
master
* test
接下來就可以開始修改代碼,修改完成之後,製作補丁:
$ git commit -a -m "commit message"
[test xxxx] commit message
n file changed, n insertion(+), n deletion(-)
$ git diff master > patch
$ git checkout master
Switched to branch 'master'
在當前目錄下產生補丁文件:patch,我們可以用git apply來應用補丁。
git format-patch生成的Git專用patch
前面部分和上面一樣,新建分支並修改代碼。
製作patch:
$ git commit -a -m "commit message" -s
[test xxxx] commit message
n file changed, n insertion(+), n deletion(-)
$ git format-patch -M
git commit 增加-s參數是指在commit中添加Signed-off-by信息。
這裏的-M是指生成最近M次commit的patch。
我們可以用git am來應用補丁。
生成的patch不僅有diff的信息,還有提交者、時間等等,還是一個E-mail文件。
提交者信息可以這樣配置:
$ git config --global user.name "Your name"
$ git config --global user.email "Your email"
git send-email發送patch
配置E-mail信息:
$ vim ~/.gitconfig
以gmail爲例:
[sendemail]
smtpencryption = tls
smtpserver = smtp.gmail.com
smtpuser = yourname
smtppass = yourpassword
smtpserverport = 587
from = yourname@gmail.com
然後就可以發送補丁了:
$ git send-email --to <maintainer's email> --cc <mailing list> <your patch>
- 我的個人主頁:http://www.techping.cn/
- 我的個人站點博客:http://www.techping.cn/blog/wordpress/
- 我的CSDN博客:http://blog.csdn.net/techping
- 我的簡書:http://www.jianshu.com/users/b2a36e431d5e/timeline
- 我的GitHub:https://github.com/techping
歡迎相互follow~