Git 打补丁-- patch 和 diff 的使用(详细)

git diff和diff产生的文件简介

git patch制作相关简介

git format-patch 用法

如果要修改的patch当前存在git仓库,那么直接可以在当前git仓库下使用git format-patch制作patch文件。通常当前仓库下还会有patches目录里面包含.patch文件,那么如果需要修改a.c文件,而且patches目录下已有.patch文件对a.c文件

做了修改,则通常需要编译该package,在编译目录下寻找a.c源文件,编译目录下的源文件是打了当前patches下所有patches后的源文件。将该源文件替换当仓库下的源文件提交一次commit,然后基于这次commit进行自己的修改,提交自己的commit, 再使用git format -patch制作patch.最后将制作的patch放入patches目录下提交gerrit.

如果要修改的package不存在git 仓库, openwrt下的package很多都是这种情况,这种情况下编译的sorce来自opensource,源码下载的连接通常在Makefile中指定。这时候就要自己git clone opensource的源码仓库,将本地编译目录下要修改的文件替换到该仓库下制作patch. 

当然openwrt下制作patch可以使用quilt工具更为方便。

package/libs/libnl

Makefile patches/

source url: https://github.com/thom311/libnl/release/download/...

git clone  https://github.com/thom311/libnl.git 

git diff 制作的.patch不含commit信息, git format -patch制作的patch包含commit信息。

1. git diff commitid1 commid2 (commit id 1 is previous commit one, commitid2 is current commit one)

   git diff commitid1 commitid2  > test.patch

diff --git a/src/fok/file1.c b/src/fok/file1.c
index 0d3c576..e5c4f35 100644
--- a/src/fok/file1.c
+++ b/src/fok/file1.c
@@ -456,5 +456,6 @@ static int my_type_get(u8 type)
 }
 
-static int my_value_set(void *priv, char value)
+/* my value set function */
+static int my_value_set(void *priv, u8 value)
 {
        struct  *pridata = priv;
diff --git a/src/test/file2.c b/src/test/file2.c
index 3fe44bd..5372fbc 100644
--- a/src/test/file2.c
+++ b/src/test/file2.c
@@ -2045,8 +2045,8 @@ static int my_input_check(struct my_structure *structure,
        }
 
        msg_dump(MSGDUMP, "my debug information is:",
-                   structure, structure_len);
+                   (const char *)structure, structure_len);
 
        /* below is checking... */
        next = (struct a *) structure;
        for(i =0 ; i<len ; i++)

2. git format-patch

src/fok/file1.c | 3 ++-  表示file1.c有三行发生变化,插入了两行,删除了一行

src/test/file2.c | 2 +- 表示file2.c有两行发生了变化,插入了一行,删除了一行

2 files changed, 3 insertions(+), 2 deletions(-) 表示一共两个文件有改动,一共三处插入,两处删除

@@ -456,5 +456,6 @@ 表示从这里开始 修改前是第456行开始有5行,即456-460行,下图patch内容中的17,18,19,22,23;修改后是456行开始有6行,即456-461行,下图patch内容中的17,18,20,21,22,23行; file1.c删除了一行 插入了两行

由此可见如果file1.c 后面如果还有修改那么会是如下格式,即修改前是643行开始修改后是644行开始(因为前面修改时多引入了一行)

@@643,4 +644,6 @@ 

From 0c3456b953d12f570c341f032ab854c9d100ac56 Sun Nov 29 00:00:00 2020
From: username <username@example.mail>
Date: Sun, 29 Nov 2020 00:13:54 +0800
Subject: [PATCH] Test: give an example for patch

Signed-off-by: username <username@example.mail>
---
 src/fok/file1.c  | 3 ++-
 src/test/file2.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
  
diff --git a/src/fok/file1.c b/src/fok/file1.c
index 0d3c576..e5c4f35 100644
--- a/src/fok/file1.c
+++ b/src/fok/file1.c
@@ -456,5 +456,6 @@ static int my_type_get(u8 type)
 }
 
-static int my_value_set(void *priv, char value)
+/* my value set function */
+static int my_value_set(void *priv, u8 value)
 {
        struct  *pridata = priv;
diff --git a/src/test/file2.c b/src/test/file2.c
index 3fe44bd..5372fbc 100644
--- a/src/test/file2.c
+++ b/src/test/file2.c
@@ -2045,8 +2045,8 @@ static int my_input_check(struct my_structure *structure,
        }
 
        msg_dump(MSGDUMP, "my debug information is:",
-                   structure, structure_len);
+                   (const char *)structure, structure_len);
 
        /* below is checking... */
        next = (struct a *) structure;
        for(i =0 ; i<len ; i++)
--
2.7.4

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐