1 <?xml version="1.0" encoding="UTF-8"?>
\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
\r
4 <article lang="en" id="git-filter-branch(1)">
\r
6 <title>git-filter-branch(1)</title>
\r
8 <primary>git-filter-branch(1)</primary>
\r
11 <simplesect id="_name">
\r
13 <simpara>git-filter-branch - Rewrite branches</simpara>
\r
15 <simplesect id="_synopsis">
\r
16 <title>SYNOPSIS</title>
\r
18 <literallayout><emphasis>git filter-branch</emphasis> [--env-filter <command>] [--tree-filter <command>]
\r
19 [--index-filter <command>] [--parent-filter <command>]
\r
20 [--msg-filter <command>] [--commit-filter <command>]
\r
21 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
\r
22 [--original <namespace>] [-d <directory>] [-f | --force]
\r
23 [--] [<rev-list options>…]</literallayout>
\r
26 <simplesect id="_description">
\r
27 <title>DESCRIPTION</title>
\r
28 <simpara>Lets you rewrite git revision history by rewriting the branches mentioned
\r
29 in the <rev-list options>, applying custom filters on each revision.
\r
30 Those filters can modify each tree (e.g. removing a file or running
\r
31 a perl rewrite on all files) or information about each commit.
\r
32 Otherwise, all information (including original commit times or merge
\r
33 information) will be preserved.</simpara>
\r
34 <simpara>The command will only rewrite the <emphasis>positive</emphasis> refs mentioned in the
\r
35 command line (e.g. if you pass <emphasis>a..b</emphasis>, only <emphasis>b</emphasis> will be rewritten).
\r
36 If you specify no filters, the commits will be recommitted without any
\r
37 changes, which would normally have no effect. Nevertheless, this may be
\r
38 useful in the future for compensating for some git bugs or such,
\r
39 therefore such a usage is permitted.</simpara>
\r
40 <simpara><emphasis role="strong">WARNING</emphasis>! The rewritten history will have different object names for all
\r
41 the objects and will not converge with the original branch. You will not
\r
42 be able to easily push and distribute the rewritten branch on top of the
\r
43 original branch. Please do not use this command if you do not know the
\r
44 full implications, and avoid using it anyway, if a simple single commit
\r
45 would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM
\r
46 REBASE" section in <xref linkend="git-rebase(1)"/> for further information about
\r
47 rewriting published history.)</simpara>
\r
48 <simpara>Always verify that the rewritten version is correct: The original refs,
\r
49 if different from the rewritten ones, will be stored in the namespace
\r
50 <emphasis>refs/original/</emphasis>.</simpara>
\r
51 <simpara>Note that since this operation is very I/O expensive, it might
\r
52 be a good idea to redirect the temporary directory off-disk with the
\r
53 <emphasis>-d</emphasis> option, e.g. on tmpfs. Reportedly the speedup is very noticeable.</simpara>
\r
54 <simplesect id="_filters">
\r
55 <title>Filters</title>
\r
56 <simpara>The filters are applied in the order as listed below. The <command>
\r
57 argument is always evaluated in the shell context using the <emphasis>eval</emphasis> command
\r
58 (with the notable exception of the commit filter, for technical reasons).
\r
59 Prior to that, the $GIT_COMMIT environment variable will be set to contain
\r
60 the id of the commit being rewritten. Also, GIT_AUTHOR_NAME,
\r
61 GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL,
\r
62 and GIT_COMMITTER_DATE are set according to the current commit. The values
\r
63 of these variables after the filters have run, are used for the new commit.
\r
64 If any evaluation of <command> returns a non-zero exit status, the whole
\r
65 operation will be aborted.</simpara>
\r
66 <simpara>A <emphasis>map</emphasis> function is available that takes an "original sha1 id" argument
\r
67 and outputs a "rewritten sha1 id" if the commit has been already
\r
68 rewritten, and "original sha1 id" otherwise; the <emphasis>map</emphasis> function can
\r
69 return several ids on separate lines if your commit filter emitted
\r
70 multiple commits.</simpara>
\r
73 <simplesect id="_options">
\r
74 <title>OPTIONS</title>
\r
78 --env-filter <command>
\r
82 This filter may be used if you only need to modify the environment
\r
83 in which the commit will be performed. Specifically, you might
\r
84 want to rewrite the author/committer name/email/time environment
\r
85 variables (see <xref linkend="git-commit(1)"/> for details). Do not forget
\r
86 to re-export the variables.
\r
92 --tree-filter <command>
\r
96 This is the filter for rewriting the tree and its contents.
\r
97 The argument is evaluated in shell with the working
\r
98 directory set to the root of the checked out tree. The new tree
\r
99 is then used as-is (new files are auto-added, disappeared files
\r
100 are auto-removed - neither .gitignore files nor any other ignore
\r
101 rules <emphasis role="strong">HAVE ANY EFFECT</emphasis>!).
\r
107 --index-filter <command>
\r
111 This is the filter for rewriting the index. It is similar to the
\r
112 tree filter but does not check out the tree, which makes it much
\r
113 faster. For hairy cases, see <xref linkend="git-update-index(1)"/>.
\r
119 --parent-filter <command>
\r
123 This is the filter for rewriting the commit’s parent list.
\r
124 It will receive the parent string on stdin and shall output
\r
125 the new parent string on stdout. The parent string is in
\r
126 the format described in <xref linkend="git-commit-tree(1)"/>: empty for
\r
127 the initial commit, "-p parent" for a normal commit and
\r
128 "-p parent1 -p parent2 -p parent3 …" for a merge commit.
\r
134 --msg-filter <command>
\r
138 This is the filter for rewriting the commit messages.
\r
139 The argument is evaluated in the shell with the original
\r
140 commit message on standard input; its standard output is
\r
141 used as the new commit message.
\r
147 --commit-filter <command>
\r
151 This is the filter for performing the commit.
\r
152 If this filter is specified, it will be called instead of the
\r
153 <emphasis>git-commit-tree</emphasis> command, with arguments of the form
\r
154 "<TREE_ID> [-p <PARENT_COMMIT_ID>]…" and the log message on
\r
155 stdin. The commit id is expected on stdout.
\r
157 <simpara>As a special extension, the commit filter may emit multiple
\r
158 commit ids; in that case, the rewritten children of the original commit will
\r
159 have all of them as parents.</simpara>
\r
160 <simpara>You can use the <emphasis>map</emphasis> convenience function in this filter, and other
\r
161 convenience functions, too. For example, calling <emphasis>skip_commit "$@"</emphasis>
\r
162 will leave out the current commit (but not its changes! If you want
\r
163 that, use <emphasis>git-rebase</emphasis> instead).</simpara>
\r
164 <simpara>You can also use the <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead of
\r
165 <emphasis>git commit-tree "$@"</emphasis> if you don’t wish to keep commits with a single parent
\r
166 and that makes no change to the tree.</simpara>
\r
171 --tag-name-filter <command>
\r
175 This is the filter for rewriting tag names. When passed,
\r
176 it will be called for every tag ref that points to a rewritten
\r
177 object (or to a tag object which points to a rewritten object).
\r
178 The original tag name is passed via standard input, and the new
\r
179 tag name is expected on standard output.
\r
181 <simpara>The original tags are not deleted, but can be overwritten;
\r
182 use "--tag-name-filter cat" to simply update the tags. In this
\r
183 case, be very careful and make sure you have the old tags
\r
184 backed up in case the conversion has run afoul.</simpara>
\r
185 <simpara>Nearly proper rewriting of tag objects is supported. If the tag has
\r
186 a message attached, a new tag object will be created with the same message,
\r
187 author, and timestamp. If the tag has a signature attached, the
\r
188 signature will be stripped. It is by definition impossible to preserve
\r
189 signatures. The reason this is "nearly" proper, is because ideally if
\r
190 the tag did not change (points to the same object, has the same name, etc.)
\r
191 it should retain any signature. That is not the case, signatures will always
\r
192 be removed, buyer beware. There is also no support for changing the
\r
193 author or timestamp (or the tag message for that matter). Tags which point
\r
194 to other tags will be rewritten to point to the underlying commit.</simpara>
\r
199 --subdirectory-filter <directory>
\r
203 Only look at the history which touches the given subdirectory.
\r
204 The result will contain that directory (and only that) as its
\r
215 Some kind of filters will generate empty commits, that left the tree
\r
216 untouched. This switch allow git-filter-branch to ignore such
\r
217 commits. Though, this switch only applies for commits that have one
\r
218 and only one parent, it will hence keep merges points. Also, this
\r
219 option is not compatible with the use of <emphasis>--commit-filter</emphasis>. Though you
\r
220 just need to use the function <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead
\r
221 of the <emphasis>git commit-tree "$@"</emphasis> idiom in your commit filter to make that
\r
228 --original <namespace>
\r
232 Use this option to set the namespace where the original commits
\r
233 will be stored. The default value is <emphasis>refs/original</emphasis>.
\r
239 -d <directory>
\r
243 Use this option to set the path to the temporary directory used for
\r
244 rewriting. When applying a tree filter, the command needs to
\r
245 temporarily check out the tree to some directory, which may consume
\r
246 considerable space in case of large projects. By default it
\r
247 does this in the <emphasis>.git-rewrite/</emphasis> directory but you can override
\r
248 that choice by this parameter.
\r
261 <emphasis>git-filter-branch</emphasis> refuses to start with an existing temporary
\r
262 directory or when there are already refs starting with
\r
263 <emphasis>refs/original/</emphasis>, unless forced.
\r
269 <rev-list options>…
\r
273 Arguments for <emphasis>git-rev-list</emphasis>. All positive refs included by
\r
274 these options are rewritten. You may also specify options
\r
275 such as <emphasis>--all</emphasis>, but you must use <emphasis>--</emphasis> to separate them from
\r
276 the <emphasis>git-filter-branch</emphasis> options.
\r
282 <simplesect id="_examples">
\r
283 <title>Examples</title>
\r
284 <simpara>Suppose you want to remove a file (containing confidential information
\r
285 or copyright violation) from all commits:</simpara>
\r
286 <literallayout>git filter-branch --tree-filter 'rm filename' HEAD</literallayout>
\r
287 <simpara>However, if the file is absent from the tree of some commit,
\r
288 a simple <literal>rm filename</literal> will fail for that tree and commit.
\r
289 Thus you may instead want to use <literal>rm -f filename</literal> as the script.</simpara>
\r
290 <simpara>A significantly faster version:</simpara>
\r
291 <literallayout>git filter-branch --index-filter 'git rm --cached filename' HEAD</literallayout>
\r
292 <simpara>Now, you will get the rewritten history saved in HEAD.</simpara>
\r
293 <simpara>To rewrite the repository to look as if <literal>foodir/</literal> had been its project
\r
294 root, and discard all other history:</simpara>
\r
295 <literallayout>git filter-branch --subdirectory-filter foodir -- --all</literallayout>
\r
296 <simpara>Thus you can, e.g., turn a library subdirectory into a repository of
\r
297 its own. Note the <literal>--</literal> that separates <emphasis>filter-branch</emphasis> options from
\r
298 revision options, and the <literal>--all</literal> to rewrite all branches and tags.</simpara>
\r
299 <simpara>To set a commit (which typically is at the tip of another
\r
300 history) to be the parent of the current initial commit, in
\r
301 order to paste the other history behind the current history:</simpara>
\r
302 <literallayout>git filter-branch --parent-filter 'sed "s/^\$/-p <graft-id>/"' HEAD</literallayout>
\r
303 <simpara>(if the parent string is empty - which happens when we are dealing with
\r
304 the initial commit - add graftcommit as a parent). Note that this assumes
\r
305 history with a single root (that is, no merge without common ancestors
\r
306 happened). If this is not the case, use:</simpara>
\r
307 <literallayout>git filter-branch --parent-filter \
\r
308 'test $GIT_COMMIT = <commit-id> && echo "-p <graft-id>" || cat' HEAD</literallayout>
\r
309 <simpara>or even simpler:</simpara>
\r
310 <literallayout>echo "$commit-id $graft-id" >> .git/info/grafts
\r
311 git filter-branch $graft-id..HEAD</literallayout>
\r
312 <simpara>To remove commits authored by "Darl McBribe" from the history:</simpara>
\r
313 <literallayout>git filter-branch --commit-filter '
\r
314 if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];
\r
318 git commit-tree "$@";
\r
319 fi' HEAD</literallayout>
\r
320 <simpara>The function <emphasis>skip_commit</emphasis> is defined as follows:</simpara>
\r
321 <literallayout>skip_commit()
\r
331 <simpara>The shift magic first throws away the tree id and then the -p
\r
332 parameters. Note that this handles merges properly! In case Darl
\r
333 committed a merge between P1 and P2, it will be propagated properly
\r
334 and all children of the merge will become merge commits with P1,P2
\r
335 as their parents instead of the merge commit.</simpara>
\r
336 <simpara>You can rewrite the commit log messages using <literal>--msg-filter</literal>. For
\r
337 example, <emphasis>git-svn-id</emphasis> strings in a repository created by <emphasis>git-svn</emphasis> can
\r
338 be removed this way:</simpara>
\r
339 <literallayout>git filter-branch --msg-filter '
\r
340 sed -e "/^git-svn-id:/d"
\r
342 <simpara>To restrict rewriting to only part of the history, specify a revision
\r
343 range in addition to the new branch name. The new branch name will
\r
344 point to the top-most revision that a <emphasis>git-rev-list</emphasis> of this range
\r
345 will print.</simpara>
\r
346 <simpara><emphasis role="strong">NOTE</emphasis> the changes introduced by the commits, and which are not reverted
\r
347 by subsequent commits, will still be in the rewritten branch. If you want
\r
348 to throw out <emphasis>changes</emphasis> together with the commits, you should use the
\r
349 interactive mode of <emphasis>git-rebase</emphasis>.</simpara>
\r
350 <simpara>Consider this history:</simpara>
\r
351 <literallayout> D--E--F--G--H
\r
353 A--B-----C</literallayout>
\r
354 <simpara>To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:</simpara>
\r
355 <literallayout>git filter-branch ... C..H</literallayout>
\r
356 <simpara>To rewrite commits E,F,G,H, use one of these:</simpara>
\r
357 <literallayout>git filter-branch ... C..H --not D
\r
358 git filter-branch ... D..H --not C</literallayout>
\r
359 <simpara>To move the whole tree into a subdirectory, or remove it from there:</simpara>
\r
360 <literallayout>git filter-branch --index-filter \
\r
361 'git ls-files -s | sed "s-\t-&newsubdir/-" |
\r
362 GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
\r
363 git update-index --index-info &&
\r
364 mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' HEAD</literallayout>
\r
366 <simplesect id="_author">
\r
367 <title>Author</title>
\r
368 <simpara>Written by Petr "Pasky" Baudis <<ulink url="mailto:pasky@suse.cz">pasky@suse.cz</ulink>>,
\r
369 and the git list <<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>></simpara>
\r
371 <simplesect id="_documentation">
\r
372 <title>Documentation</title>
\r
373 <simpara>Documentation by Petr Baudis and the git list.</simpara>
\r
375 <simplesect id="_git">
\r
377 <simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
\r