<?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>David Underhill &#187; bash</title>
	<atom:link href="http://dound.com/category/coding/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://dound.com</link>
	<description>dound&#039;s space on the web</description>
	<lastBuildDate>Tue, 31 Jan 2012 10:57:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>git: forever remove files or folders from history</title>
		<link>http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/</link>
		<comments>http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 23:39:23 +0000</pubDate>
		<dc:creator>David Underhill</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[version control]]></category>
		<category><![CDATA[binary file]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://dound.com/?p=132</guid>
		<description><![CDATA[I recently had a need to rewrite a git repository&#8217;s history. This isn&#8217;t generally a very good idea, though it is useful if your repository contains files it should not (such as unneeded large binary files or copyrighted material). I also am using it because I had a branch where I only wanted to merge [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a need to rewrite a <a href="http://git-scm.com/">git</a> repository&#8217;s history.  This isn&#8217;t generally a very good idea, though it is useful if your repository contains files it should not (such as unneeded large binary files or copyrighted material).  I also am using it because I had a branch where I only wanted to merge a subset of files back into master (though there are probably better ways of doing this).  Anyway, it is not very hard to rewrite history thanks to the excellent <a href="http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html">git-filter-branch</a> tool which comes with <a href="http://git-scm.com/">git</a>.  However, if your goal was to reduce a large repository&#8217;s size then <a href="http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html">git-filter-branch</a> does not quite finish the job since it makes temporary backups of the filtered out files.  To remove those, you need to do a little more work.  To make it easier to permanently remove files, I wrapped it in a little bash script <a href="http://dound.com/wp/files/git-remove-history">git-remove-history</a> (also shown below) &#8212; simply go to the root of your repository and run the script with the list of files you want to delete and it will do the rest.  There is an interesting thread about doing this <a href="http://kerneltrap.org/mailarchive/git/2007/10/7/331471">here</a> on <a href="http://kerneltrap.org/">KernelTrap</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-o</span> errexit
&nbsp;
<span style="color: #666666; font-style: italic;"># Author: David Underhill</span>
<span style="color: #666666; font-style: italic;"># Script to permanently delete files/folders from your git repository.  To use </span>
<span style="color: #666666; font-style: italic;"># it, cd to your repository's root and then run the script with a list of paths</span>
<span style="color: #666666; font-style: italic;"># you want to delete, e.g., git-delete-history path1 path2</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">0</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># make sure we're at the root of git repo</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> .git <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Error: must run this script from the root of a git repository&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remove all paths passed as arguments from the history of the repo</span>
<span style="color: #007800;">files</span>=$<span style="color: #000000; font-weight: bold;">@</span>
git filter-branch <span style="color: #660033;">--index-filter</span> <span style="color: #ff0000;">&quot;git rm -rf --cached --ignore-unmatch <span style="color: #007800;">$files</span>&quot;</span> HEAD
&nbsp;
<span style="color: #666666; font-style: italic;"># remove the temporary history git-filter-branch otherwise leaves behind for a long time</span>
<span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> .git<span style="color: #000000; font-weight: bold;">/</span>refs<span style="color: #000000; font-weight: bold;">/</span>original<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> git reflog expire <span style="color: #660033;">--all</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span>  git <span style="color: #c20cb9; font-weight: bold;">gc</span> <span style="color: #660033;">--aggressive</span> <span style="color: #660033;">--prune</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>

