创建仓库,创建测试文件,提交文件到本地仓库
git init
已初始化空的 Git 仓库于 /home/hjz/patch/.git/
创建测试文件并添加内容
vi mian.c
#include <stdio.h>
int main(void)
{
printf("hello world\n");
return 0;
}
把修改的内容提交到本地仓库
git add main.c
git commit -m 'touch main.c'
创建分支,修改内容,获取差异补丁
创建并切换patch分支
git checkout -b patch
修改patch分支下main.c 内容
#include <stdio.h>
int main(void)
{
printf("hello world\n");
printf("hello patch\n"); //增加次行测试代码
return 0;
}
提交修改到patch分支下的仓库
git add mian.c
git commit -m 'modify main.c'
使用git log查看提交记录
git log
commit 6479ce609a0b46028cd77ae100b6dcab2eee2d84 (HEAD -> patch)
Author: hongjz <hongjz@tech-now.com>
Date: Sat Apr 16 19:27:36 2022 +0800
modify main.c
commit cbc5bbce75795672674cf0cab0282da9ff242899 (master)
Author: hongjz <hongjz@tech-now.com>
Date: Sat Apr 16 19:19:42 2022 +0800
touch main.c
查看两次提交的差异,格式为git diff ‘commit id’ ‘commit id’
git diff cbc5bbce75795672674cf0cab0282da9ff242899 6479ce609a0b46028cd77ae100b6dcab2eee2d84
diff --git a/main.c b/main.c
index 90014ed..4c87bb9 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
int main(void)
{
printf("hello world\n");
+ printf("hello patch\n"); //增加次行测试代码
return 0;
}
查看两次提交的差异,并生成patch文件,格式为git diff ‘commit id’ ‘commit id’ > 文件名
git diff cbc5bbce75795672674cf0cab0282da9ff242899 6479ce609a0b46028cd77ae100b6dcab2eee2d84 > test.patch
切换到需要主分支,模拟打补丁
切换到master分支
git checkout master
可以检测patch补丁是否能用,不输出任何信息即为可用
git apply --check test.patch
打补丁
git apply test.patch
查看mian.c已经改变,和patch分支的一模一样了
cat main.c
#include <stdio.h>
int main(void)
{
printf("hello world\n");
printf("hello patch\n"); //增加次行测试代码
return 0;
}
也可以使用git diff查看打上补丁的文件和未打之前的差异
git diff
diff --git a/main.c b/main.c
index 90014ed..4c87bb9 100644
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
int main(void)
{
printf("hello world\n");
+ printf("hello patch\n"); //增加次行测试代码
return 0;
}
————————————————
版权声明:本文为CSDN博主「牛杂天天见」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45703168/article/details/124218465