linux git patch 和patch以及git diff 命令

1.git log 查看commit id,修改前为id1,修改后id2

2.根据id1到id2有几次提交来生成几个patch,否则的话会根据所有节点生成很多patch

比如:

commit id2

commit idx

commit id1

那么执行:git format-patch -2 id1 id2   会按照顺序生成两个patch,比如下:

0001-add-build-scripts.patch

0002-add-test-build-scripts.patch

3.先回退到id1 : git reset --hard id1

4.打patch时检查patch:

先检查patch文件:git apply --stat  patchname.patch
检查能否应用成功:git apply --check patchname.patch    (如果打不上,在这一步会报错,报错显示和git am patch是一样的,如果patch有顺序的话,检查第一个就可以)

5.打patch :git am patchname.patch

打patch时严格按照patch的顺序一个一个打

6.patch失败的话,采用 patch -pn < patchname.patch

pn表示跳过多少级目录,如果patch在当前目录下,直接用patch -pn < patchname.patch ,如果patch在上层目录下,则patch -p1 < ../patchname.patch

7.git diff 也可以打patch,使用:

  git diff commit_id1 commit_id2 > test.patch

  打补丁: git apply test.patch

备注,如果打补丁失败,要先把新增的文件删掉。

如果没有commitid,只是修改了文件,可以用git diff 后接改动文件来生成patch,如下:

(1)git diff test.java > test.patch # 生成补丁

然后切换到指定路径下,检查一下这个补丁,并把这个补丁打上。

(2)git apply --check  test.patch #测试补丁能否成功

(3)git apply test.patch # 打补丁

上一篇:移动端网页特效


下一篇:使用CSS让多出来的字变为省略号