<?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>Valentin's Lab &#187; git</title>
	<atom:link href="https://vaab.blog.kal.fr/tag/git/feed/" rel="self" type="application/rss+xml" />
	<link>https://vaab.blog.kal.fr</link>
	<description>Ratiocination of an opensource techie</description>
	<lastBuildDate>Thu, 15 Nov 2018 08:04:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F&amp;language=en_US&amp;category=text&amp;title=Valentin%27s+Lab&amp;description=Ratiocination+of+an+opensource+techie&amp;tags=blog" type="text/html" />
	<item>
		<title>extracting a subpart of a git repository with history</title>
		<link>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/</link>
		<comments>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 08:10:24 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[comp]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[sci]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=233</guid>
		<description><![CDATA[I recently had to extract a subdirectory of an existing git repository towards its own new repository. And I wanted to keep all the commit information and history that was related to this directory. EDIT: Even if the solution I &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>I recently had to extract a subdirectory of an existing git repository towards its own new repository. And I wanted to keep all the commit information and history that was related to this directory.</p>
<p><strong>EDIT</strong>: Even if the solution I used was simpler than I would have expected, I was far from knowing that GIT had a 'filter-branch' command which does the job perfectly well (and it is  <a class="reference external" href="http://progit.org/book/ch6-4.html#making_a_subdirectory_the_new_root">documented in ProGIT</a>). Thanks Jon for the tip.</p>
<div class="section" id="sketching-the-tools-used">
<h3>sketching the tools used</h3>
<p>First step, is to retrieve only the commit Hashes related to this directory:</p>
<pre class="literal-block">
git log --format=%H -- MY_DIRECTORY
</pre>
<p>to filter out only the commit Hashes related to this directory. Then:</p>
<pre class="literal-block">
git format-patch HASH -1
</pre>
<p>will be used to output patch files with the commit information (subject, author...)</p>
<p>Then, with some <tt class="docutils literal">sed</tt> you'll have to change the directories used in all the outputed patch:</p>
<pre class="literal-block">
sed -i s%MYOLDDIRECTORY%MYNEWDIRECTORY%g *.patch
</pre>
<p>The new directory in my case was &quot;&quot; (nothing) as it was in the root folder.</p>
<p>Finally, you'll have to apply your patches to your other repository with:</p>
<pre class="literal-block">
git am *.patch
</pre>
</div>
<div class="section" id="complete-commands-used">
<h3>Complete commands used</h3>
<p>The complete commands that I've used:</p>
<pre class="literal-block">
cd PARENT_OF_MYDIRECTORY
git log --format=%H -- MYDIRECTORY | tac | \
    (i=0
     while read h; do
         i=$[$i+1]
         git format-patch $h -1 --start-number $i
     done)
</pre>
<p>This produces <tt class="docutils literal"><span class="pre">NNNN-*.patch</span></tt> files, one for each commit:</p>
<pre class="literal-block">
sed -i s%MYDIRECTORY%%g *.patch
</pre>
<p>This will update the paths in all patches:</p>
<pre class="literal-block">
mv *.patch MY_NEW_REPO
cd MY_NEW_REPO
git am *.patch
</pre>
<p>A quick check with <tt class="docutils literal">gitk</tt> ensures me that message subject and commit were transfered ok.</p>
<p>Et voilà !</p>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=233&amp;md5=c192eb8bf3b9556f88fb1adac13f4fef" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2011/06/29/extracting-a-subpart-of-a-git-repository-with-history/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2011%2F06%2F29%2Fextracting-a-subpart-of-a-git-repository-with-history%2F&amp;language=en_GB&amp;category=text&amp;title=extracting+a+subpart+of+a+git+repository+with+history&amp;description=I+recently+had+to+extract+a+subdirectory+of+an+existing+git+repository+towards+its+own+new+repository.+And+I+wanted+to+keep+all+the+commit+information+and+history+that+was...&amp;tags=git%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
