<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fwolf's Blog &#187; import - Fwolf's Blog</title>
	<atom:link href="http://www.fwolf.com/blog/post/tag/import/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fwolf.com/blog</link>
	<description>随心·随意·随缘·努力～</description>
	<lastBuildDate>Wed, 07 Jul 2010 07:07:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Git 合并 patch 时的冲突处理一例</title>
		<link>http://www.fwolf.com/blog/post/448</link>
		<comments>http://www.fwolf.com/blog/post/448#comments</comments>
		<pubDate>Tue, 25 Aug 2009 15:02:35 +0000</pubDate>
		<dc:creator>Fwolf</dc:creator>
				<category><![CDATA[Svn/Git]]></category>
		<category><![CDATA[conflict]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[patch]]></category>

		<guid isPermaLink="false">http://www.fwolf.com/blog/?p=448</guid>
		<description><![CDATA[git version 1.6.0.4 几个新手刚刚开始接触 Git，为了维护核心仓库的“纯洁”，避免太多无关信息被误提交进仓库（再次批评一些图形化工具默认的“Select All”），采用了核心仓库只读，邮件提交 patch，审核后再提交的工作流程。 期间有时会遇到合并冲突，正常的原因一般是未及时下载新版本产生了冲突，特殊一点的原因是手工修改 patch 内容导致的。有时候看注释写得不够准确，忍不住就改了，有时候是 Geany 保存时自动去除了 patch 原文中的行尾空格，有时候是文件回车格式、BOM 等变动了，总之合并 patch 的时候，如果生成 patch 的“原稿”找不到，一般就产生了冲突，比如： $ git am 0001-BUG-Sybase.patch Applying: CHG: 读取Sybase如果时间为空,设置默认时间的修改 error: patch failed: source.php:38 error: source.php: patch does not apply Patch failed at 0001. When you have resolved this problem run "git am --resolved". If you would prefer to [...]]]></description>
			<content:encoded><![CDATA[<p>git version 1.6.0.4</p>

<p>几个新手刚刚开始接触 Git，为了维护核心仓库的“纯洁”，避免太多无关信息被误提交进仓库（再次批评一些图形化工具默认的“Select All”），采用了核心仓库只读，邮件提交 patch，审核后再提交的工作流程。</p>

<p>期间有时会遇到合并冲突，正常的原因一般是未及时下载新版本产生了冲突，特殊一点的原因是手工修改 patch 内容导致的。有时候看注释写得不够准确，忍不住就改了，有时候是 Geany 保存时自动去除了 patch 原文中的行尾空格，有时候是文件回车格式、BOM 等变动了，总之合并 patch 的时候，如果生成 patch 的“原稿”找不到，一般就产生了冲突，比如：</p>

<pre><code>$ git am 0001-BUG-Sybase.patch
Applying: CHG: 读取Sybase如果时间为空,设置默认时间的修改
error: patch failed: source.php:38
error: source.php: patch does not apply
Patch failed at 0001.
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
</code></pre>

<p>刚开始一看有些懵，因为没有任何冲突在哪里的提示，后来找到一种方法，am 操作出问题后先手工 apply：</p>

<pre><code>$ git apply --reject 0001-BUG-Sybase.patch
Checking patch source.php...
error: while searching for:
        // 注释
        // 以下为几行代码片断
error: patch failed: source.php:38
Applying patch source.php with 1 rejects...
Rejected hunk #1.
</code></pre>

<p>这样，就把没有冲突的文件先合并了，剩下有冲突的作了标记。先看输出，error: while searching for: 说明是这段代码有冲突，error: patch failed: source.php:38 指明了产生冲突的代码<strong>片断</strong>的开始行号，相应的，patch 中应该有这么一段：</p>

<pre><code>diff --git a/source.php b/source.php
index 8770441..4e77b8a 100644
--- a/source.php
+++ b/source.php
@@ -38,27 +38,23 @@ class Site extends Module
        // 注释
        // 以下为几行代码片断
</code></pre>

<p>同时，还会产生一个 source.php.rej 文件，里面也是上面这段因为冲突无法合并的代码片断。</p>

<p>现在，在这段代码中查找冲突原因，并对文件进行修改，source.php.rej 参考完了可以删掉。改好之后，用 git add 把 source.php 添加到缓冲区，同时也要把其他没有冲突合并成功了的文件也加进来，因为在作 apply 操作的时候他们也发生了变化：</p>

<pre><code>$ git add source.php
$ git add 其他 apply 进来的文件们
</code></pre>

<p>最后：</p>

<pre><code>$ git am --resolved
Applying: CHG: 读取Sybase如果时间为空,设置默认时间的修改
</code></pre>

<p>大功告成。</p>

<p>中间如果处理乱了，用 git reset 恢复即可，所以合并 patch 在一个“干净”的分支上处理更好。</p>

	Tags: <a href="http://www.fwolf.com/blog/post/tag/conflict" title="conflict" rel="tag">conflict</a>, <a href="http://www.fwolf.com/blog/post/tag/example" title="example" rel="tag">example</a>, <a href="http://www.fwolf.com/blog/post/tag/git" title="git" rel="tag">git</a>, <a href="http://www.fwolf.com/blog/post/tag/import" title="import" rel="tag">import</a>, <a href="http://www.fwolf.com/blog/post/tag/merge" title="merge" rel="tag">merge</a>, <a href="http://www.fwolf.com/blog/post/tag/patch" title="patch" rel="tag">patch</a>, <a href="http://www.fwolf.com/blog/post/tag/scm" title="Svn/Git" rel="tag">Svn/Git</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.fwolf.com/blog/post/446" title="[Git]初学者注意事项 (2009-08-04)">[Git]初学者注意事项</a> (0)</li>
	<li><a href="http://www.fwolf.com/blog/post/127" title="利用SVN更新网站 (2006-01-19)">利用SVN更新网站</a> (7)</li>
	<li><a href="http://www.fwolf.com/blog/post/162" title="[ubuntu]安装vmware时找不到c header files的小问题 (2006-05-09)">[ubuntu]安装vmware时找不到c header files的小问题</a> (3)</li>
	<li><a href="http://www.fwolf.com/blog/post/441" title="[Git]真正回滚已上传的更新 (2009-05-14)">[Git]真正回滚已上传的更新</a> (0)</li>
	<li><a href="http://www.fwolf.com/blog/post/431" title="[Git]提交后自动发email (2009-03-27)">[Git]提交后自动发email</a> (2)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.fwolf.com/blog/post/448/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
