<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-filter-branch(1)">
<articleinfo>
    <title>git-filter-branch(1)</title>
        <indexterm>
                <primary>git-filter-branch(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-filter-branch - Rewrite branches</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git filter-branch</emphasis> [--env-filter &lt;command&gt;] [--tree-filter &lt;command&gt;]
        [--index-filter &lt;command&gt;] [--parent-filter &lt;command&gt;]
        [--msg-filter &lt;command&gt;] [--commit-filter &lt;command&gt;]
        [--tag-name-filter &lt;command&gt;] [--subdirectory-filter &lt;directory&gt;]
        [--original &lt;namespace&gt;] [-d &lt;directory&gt;] [-f | --force]
        [--] [&lt;rev-list options&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Lets you rewrite git revision history by rewriting the branches mentioned
in the &lt;rev-list options&gt;, applying custom filters on each revision.
Those filters can modify each tree (e.g. removing a file or running
a perl rewrite on all files) or information about each commit.
Otherwise, all information (including original commit times or merge
information) will be preserved.</simpara>
<simpara>The command will only rewrite the <emphasis>positive</emphasis> refs mentioned in the
command line (e.g. if you pass <emphasis>a..b</emphasis>, only <emphasis>b</emphasis> will be rewritten).
If you specify no filters, the commits will be recommitted without any
changes, which would normally have no effect.  Nevertheless, this may be
useful in the future for compensating for some git bugs or such,
therefore such a usage is permitted.</simpara>
<simpara><emphasis role="strong">WARNING</emphasis>! The rewritten history will have different object names for all
the objects and will not converge with the original branch.  You will not
be able to easily push and distribute the rewritten branch on top of the
original branch.  Please do not use this command if you do not know the
full implications, and avoid using it anyway, if a simple single commit
would suffice to fix your problem.  (See the "RECOVERING FROM UPSTREAM
REBASE" section in <xref linkend="git-rebase(1)"/> for further information about
rewriting published history.)</simpara>
<simpara>Always verify that the rewritten version is correct: The original refs,
if different from the rewritten ones, will be stored in the namespace
<emphasis>refs/original/</emphasis>.</simpara>
<simpara>Note that since this operation is very I/O expensive, it might
be a good idea to redirect the temporary directory off-disk with the
<emphasis>-d</emphasis> option, e.g. on tmpfs.  Reportedly the speedup is very noticeable.</simpara>
<simplesect id="_filters">
<title>Filters</title>
<simpara>The filters are applied in the order as listed below.  The &lt;command&gt;
argument is always evaluated in the shell context using the <emphasis>eval</emphasis> command
(with the notable exception of the commit filter, for technical reasons).
Prior to that, the $GIT_COMMIT environment variable will be set to contain
the id of the commit being rewritten.  Also, GIT_AUTHOR_NAME,
GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL,
and GIT_COMMITTER_DATE are set according to the current commit.  The values
of these variables after the filters have run, are used for the new commit.
If any evaluation of &lt;command&gt; returns a non-zero exit status, the whole
operation will be aborted.</simpara>
<simpara>A <emphasis>map</emphasis> function is available that takes an "original sha1 id" argument
and outputs a "rewritten sha1 id" if the commit has been already
rewritten, and "original sha1 id" otherwise; the <emphasis>map</emphasis> function can
return several ids on separate lines if your commit filter emitted
multiple commits.</simpara>
</simplesect>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
--env-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This filter may be used if you only need to modify the environment
        in which the commit will be performed.  Specifically, you might
        want to rewrite the author/committer name/email/time environment
        variables (see <xref linkend="git-commit(1)"/> for details).  Do not forget
        to re-export the variables.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--tree-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for rewriting the tree and its contents.
        The argument is evaluated in shell with the working
        directory set to the root of the checked out tree.  The new tree
        is then used as-is (new files are auto-added, disappeared files
        are auto-removed - neither .gitignore files nor any other ignore
        rules <emphasis role="strong">HAVE ANY EFFECT</emphasis>!).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--index-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for rewriting the index.  It is similar to the
        tree filter but does not check out the tree, which makes it much
        faster.  For hairy cases, see <xref linkend="git-update-index(1)"/>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--parent-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for rewriting the commit&#8217;s parent list.
        It will receive the parent string on stdin and shall output
        the new parent string on stdout.  The parent string is in
        the format described in <xref linkend="git-commit-tree(1)"/>: empty for
        the initial commit, "-p parent" for a normal commit and
        "-p parent1 -p parent2 -p parent3 &#8230;" for a merge commit.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--msg-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for rewriting the commit messages.
        The argument is evaluated in the shell with the original
        commit message on standard input; its standard output is
        used as the new commit message.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--commit-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for performing the commit.
        If this filter is specified, it will be called instead of the
        <emphasis>git-commit-tree</emphasis> command, with arguments of the form
        "&lt;TREE_ID&gt; [-p &lt;PARENT_COMMIT_ID&gt;]&#8230;" and the log message on
        stdin.  The commit id is expected on stdout.
</simpara>
<simpara>As a special extension, the commit filter may emit multiple
commit ids; in that case, the rewritten children of the original commit will
have all of them as parents.</simpara>
<simpara>You can use the <emphasis>map</emphasis> convenience function in this filter, and other
convenience functions, too.  For example, calling <emphasis>skip_commit "$@"</emphasis>
will leave out the current commit (but not its changes! If you want
that, use <emphasis>git-rebase</emphasis> instead).</simpara>
<simpara>You can also use the <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead of
<emphasis>git commit-tree "$@"</emphasis> if you don&#8217;t wish to keep commits with a single parent
and that makes no change to the tree.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--tag-name-filter &lt;command&gt;
</term>
<listitem>
<simpara>
        This is the filter for rewriting tag names. When passed,
        it will be called for every tag ref that points to a rewritten
        object (or to a tag object which points to a rewritten object).
        The original tag name is passed via standard input, and the new
        tag name is expected on standard output.
</simpara>
<simpara>The original tags are not deleted, but can be overwritten;
use "--tag-name-filter cat" to simply update the tags.  In this
case, be very careful and make sure you have the old tags
backed up in case the conversion has run afoul.</simpara>
<simpara>Nearly proper rewriting of tag objects is supported. If the tag has
a message attached, a new tag object will be created with the same message,
author, and timestamp. If the tag has a signature attached, the
signature will be stripped. It is by definition impossible to preserve
signatures. The reason this is "nearly" proper, is because ideally if
the tag did not change (points to the same object, has the same name, etc.)
it should retain any signature. That is not the case, signatures will always
be removed, buyer beware. There is also no support for changing the
author or timestamp (or the tag message for that matter). Tags which point
to other tags will be rewritten to point to the underlying commit.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--subdirectory-filter &lt;directory&gt;
</term>
<listitem>
<simpara>
        Only look at the history which touches the given subdirectory.
        The result will contain that directory (and only that) as its
        project root.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--prune-empty
</term>
<listitem>
<simpara>
        Some kind of filters will generate empty commits, that left the tree
        untouched.  This switch allow git-filter-branch to ignore such
        commits.  Though, this switch only applies for commits that have one
        and only one parent, it will hence keep merges points. Also, this
        option is not compatible with the use of <emphasis>--commit-filter</emphasis>. Though you
        just need to use the function <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead
        of the <emphasis>git commit-tree "$@"</emphasis> idiom in your commit filter to make that
        happen.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--original &lt;namespace&gt;
</term>
<listitem>
<simpara>
        Use this option to set the namespace where the original commits
        will be stored. The default value is <emphasis>refs/original</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-d &lt;directory&gt;
</term>
<listitem>
<simpara>
        Use this option to set the path to the temporary directory used for
        rewriting.  When applying a tree filter, the command needs to
        temporarily check out the tree to some directory, which may consume
        considerable space in case of large projects.  By default it
        does this in the <emphasis>.git-rewrite/</emphasis> directory but you can override
        that choice by this parameter.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-f
</term>
<term>
--force
</term>
<listitem>
<simpara>
        <emphasis>git-filter-branch</emphasis> refuses to start with an existing temporary
        directory or when there are already refs starting with
        <emphasis>refs/original/</emphasis>, unless forced.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;rev-list options&gt;&#8230;
</term>
<listitem>
<simpara>
        Arguments for <emphasis>git-rev-list</emphasis>.  All positive refs included by
        these options are rewritten.  You may also specify options
        such as <emphasis>--all</emphasis>, but you must use <emphasis>--</emphasis> to separate them from
        the <emphasis>git-filter-branch</emphasis> options.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_examples">
<title>Examples</title>
<simpara>Suppose you want to remove a file (containing confidential information
or copyright violation) from all commits:</simpara>
<literallayout>git filter-branch --tree-filter 'rm filename' HEAD</literallayout>
<simpara>However, if the file is absent from the tree of some commit,
a simple <literal>rm filename</literal> will fail for that tree and commit.
Thus you may instead want to use <literal>rm -f filename</literal> as the script.</simpara>
<simpara>A significantly faster version:</simpara>
<literallayout>git filter-branch --index-filter 'git rm --cached filename' HEAD</literallayout>
<simpara>Now, you will get the rewritten history saved in HEAD.</simpara>
<simpara>To rewrite the repository to look as if <literal>foodir/</literal> had been its project
root, and discard all other history:</simpara>
<literallayout>git filter-branch --subdirectory-filter foodir -- --all</literallayout>
<simpara>Thus you can, e.g., turn a library subdirectory into a repository of
its own.  Note the <literal>--</literal> that separates <emphasis>filter-branch</emphasis> options from
revision options, and the <literal>--all</literal> to rewrite all branches and tags.</simpara>
<simpara>To set a commit (which typically is at the tip of another
history) to be the parent of the current initial commit, in
order to paste the other history behind the current history:</simpara>
<literallayout>git filter-branch --parent-filter 'sed "s/^\$/-p &lt;graft-id&gt;/"' HEAD</literallayout>
<simpara>(if the parent string is empty - which happens when we are dealing with
the initial commit - add graftcommit as a parent).  Note that this assumes
history with a single root (that is, no merge without common ancestors
happened).  If this is not the case, use:</simpara>
<literallayout>git filter-branch --parent-filter \
        'test $GIT_COMMIT = &lt;commit-id&gt; &amp;&amp; echo "-p &lt;graft-id&gt;" || cat' HEAD</literallayout>
<simpara>or even simpler:</simpara>
<literallayout>echo "$commit-id $graft-id" &gt;&gt; .git/info/grafts
git filter-branch $graft-id..HEAD</literallayout>
<simpara>To remove commits authored by "Darl McBribe" from the history:</simpara>
<literallayout>git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];
        then
                skip_commit "$@";
        else
                git commit-tree "$@";
        fi' HEAD</literallayout>
<simpara>The function <emphasis>skip_commit</emphasis> is defined as follows:</simpara>
<literallayout>skip_commit()
{
        shift;
        while [ -n "$1" ];
        do
                shift;
                map "$1";
                shift;
        done;
}</literallayout>
<simpara>The shift magic first throws away the tree id and then the -p
parameters.  Note that this handles merges properly! In case Darl
committed a merge between P1 and P2, it will be propagated properly
and all children of the merge will become merge commits with P1,P2
as their parents instead of the merge commit.</simpara>
<simpara>You can rewrite the commit log messages using <literal>--msg-filter</literal>.  For
example, <emphasis>git-svn-id</emphasis> strings in a repository created by <emphasis>git-svn</emphasis> can
be removed this way:</simpara>
<literallayout>git filter-branch --msg-filter '
        sed -e "/^git-svn-id:/d"
'</literallayout>
<simpara>To restrict rewriting to only part of the history, specify a revision
range in addition to the new branch name.  The new branch name will
point to the top-most revision that a <emphasis>git-rev-list</emphasis> of this range
will print.</simpara>
<simpara><emphasis role="strong">NOTE</emphasis> the changes introduced by the commits, and which are not reverted
by subsequent commits, will still be in the rewritten branch. If you want
to throw out <emphasis>changes</emphasis> together with the commits, you should use the
interactive mode of <emphasis>git-rebase</emphasis>.</simpara>
<simpara>Consider this history:</simpara>
<literallayout>     D--E--F--G--H
    /     /
A--B-----C</literallayout>
<simpara>To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:</simpara>
<literallayout>git filter-branch ... C..H</literallayout>
<simpara>To rewrite commits E,F,G,H, use one of these:</simpara>
<literallayout>git filter-branch ... C..H --not D
git filter-branch ... D..H --not C</literallayout>
<simpara>To move the whole tree into a subdirectory, or remove it from there:</simpara>
<literallayout>git filter-branch --index-filter \
        'git ls-files -s | sed "s-\t-&amp;newsubdir/-" |
                GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                        git update-index --index-info &amp;&amp;
         mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD</literallayout>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Petr "Pasky" Baudis &lt;<ulink url="mailto:pasky@suse.cz">pasky@suse.cz</ulink>&gt;,
and the git list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by Petr Baudis and the git list.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
