git: 如何用git-am来合并git format-patch生成的一系列的patch.

这篇文章主要介绍一下git-am 和 format-patch 的使用。 由于在git使用当中,
会有不少时候别人(供应商或者其余的开发人员)发过来一系列的patch,这些patch一般的是相似这样的名字:linux

0001--JFFS2-community-fix-with-not-use-OOB.patch
0002--Community-patch-for-Fix-mount-error-in.patch
0003--partial-low-interrupt-latency-mode-for-ARM113.patch
0004--for-the-global-I-cache-invalidation-ARM11.patch
0005--1-arm-Add-more-cache-memory-types-macr.patch
0006--2-Port-imx-3.3.0-release-to-2.6.28.patch
0007--3-Add-MX25-support.patch
0008--Move-asm-arch-headers-to-linux-inc-dir.patch
0009--1-regulator-allow-search-by-regulator.patch

里面包含了提交的日志,做者,日期等信息。你想作的是把这些patch引入到你的
代码库中,最好是也能够把日志也引入进来, 方便之后维护用。传统的打patch方式是android

patch -p1 < 0001--JFFS2-community-fix-with-not-use-OOB.patch

这样来打patch,可是这样会把这些有用的信息丢失。git

因为这些patch显然是用git format-patch来生成的,因此用git的工具应该就能够很好的作好。app

git-am 就是做这件事情。工具

在使用git-am以前, 你要首先git am –abort 一次,来放弃掉之前的am信息,这样才能够进行一次全新的am。
否则会遇到这样的错误。
.git/rebase-apply still exists but mbox given.日志

git-am 能够一次合并一个文件,或者一个目录下全部的patch,或者你的邮箱目录下的patch.code

下面举两个例子:orm

  1. 你如今有一个code base: small-src, 你的patch文件放在~/patch/0001-trival-patch.patch

     

cd small-src
git-am ~/patch/0001-trival-patch.patch

若是成功patch上去, 你就能够去喝杯茶了。ip

若是失败了, git 会提示错误, 好比:开发

error: patch failed: android/mediascanner.cpp:452
error: android/mediascanner.cpp: patch does not apply

这样你就须要先看看patch, 而后改改错误的这个文件,让这个patch可以patch上去。

  1. 你有一堆patch, 名字是上面提到的那一堆patch, 你把他们放在~/patch-set/目录下(路径随意)

     

cd opencore
git am ~/patch-set/*.patch

(这里git就会按照文件名的顺序一次am这些patch)
若是一切顺利, 你全部的patch都OK了, 你又Lucky了。

不过不顺利的时候十有八九,若是git am中间遇到了patch,am就会停到打这个
patch的地方, 告诉你是哪一个patch打不上去。

好比我如今有一个文件file,有两个patch.
file 的内容是

the text

more text

两个patch分别是:

0001-add-line.patch:

From 48869ccbced494e05738090afa5a54f2a261df0f Mon Sep 17 00:00:00 2001
From: zhangjiejing <zhangjiejing@zhangjiejing-desktop.(none)>
Date: Thu, 22 Apr 2010 13:04:34 +0800
Subject: [PATCH 1/2] add line

---
 file |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/file b/file
index 067780e..685f0fa 100644
--- a/file
+++ b/file
@@ -3,3 +3,5 @@ file:
 some text

 more text
+
+add line
--
1.6.3.3

0002-change-line.patch:

From f756e1b3a87c216b7e0afea9d15badd033171578 Mon Sep 17 00:00:00 2001
From: zhangjiejing <zhangjiejing@zhangjiejing-desktop.(none)>
Date: Thu, 22 Apr 2010 13:05:19 +0800
Subject: [PATCH 2/2] change line

---
 file |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/file b/file
index 685f0fa..7af7852 100644
--- a/file
+++ b/file
@@ -1,6 +1,6 @@
 file:

-some text
+Change line text

 more text

--
1.6.3.3

运行
git am *.patch

来merge这些patch, 报错, Patch failed at 0001 add line这样咱们看0001这
个patch,原来patch须要的是some text, 而file里面是the text, 因此咱们用编
辑器把这行改为some text,

vi file
git apply 0001-add-line.patch
git add file
git am --resolved

在解决完冲突之后, 好比用git add来让git知道你已经解决完冲突了。

  • 若是你发现这个冲突是没法解决的, 要撤销整个am的东西。 能够运行git am –abort,
  • 若是你想只是忽略这一个patch,能够运行git am –skip来跳过这个patch.
相关文章
相关标签/搜索