1 <?xml version="1.0" encoding="UTF-8"?>
\r
2 <!DOCTYPE sect2 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
\r
4 <sect2 lang="en" id="git-filter-branch(1)">
\r
5 <title>git-filter-branch(1)</title>
\r
7 <primary>git-filter-branch(1)</primary>
\r
9 <simplesect id="git-filter-branch(1)__name">
\r
11 <simpara>git-filter-branch - Rewrite branches</simpara>
\r
13 <simplesect id="git-filter-branch(1)__synopsis">
\r
14 <title>SYNOPSIS</title>
\r
16 <literallayout><emphasis>git filter-branch</emphasis> [--env-filter <command>] [--tree-filter <command>]
\r
17 [--index-filter <command>] [--parent-filter <command>]
\r
18 [--msg-filter <command>] [--commit-filter <command>]
\r
19 [--tag-name-filter <command>] [--subdirectory-filter <directory>]
\r
21 [--original <namespace>] [-d <directory>] [-f | --force]
\r
22 [--] [<rev-list options>…]</literallayout>
\r
25 <simplesect id="git-filter-branch(1)__description">
\r
26 <title>DESCRIPTION</title>
\r
27 <simpara>Lets you rewrite Git revision history by rewriting the branches mentioned
\r
28 in the <rev-list options>, applying custom filters on each revision.
\r
29 Those filters can modify each tree (e.g. removing a file or running
\r
30 a perl rewrite on all files) or information about each commit.
\r
31 Otherwise, all information (including original commit times or merge
\r
32 information) will be preserved.</simpara>
\r
33 <simpara>The command will only rewrite the <emphasis>positive</emphasis> refs mentioned in the
\r
34 command line (e.g. if you pass <emphasis>a..b</emphasis>, only <emphasis>b</emphasis> will be rewritten).
\r
35 If you specify no filters, the commits will be recommitted without any
\r
36 changes, which would normally have no effect. Nevertheless, this may be
\r
37 useful in the future for compensating for some Git bugs or such,
\r
38 therefore such a usage is permitted.</simpara>
\r
39 <simpara><emphasis role="strong">NOTE</emphasis>: This command honors <emphasis>.git/info/grafts</emphasis> file and refs in
\r
40 the <emphasis>refs/replace/</emphasis> namespace.
\r
41 If you have any grafts or replacement refs defined, running this command
\r
42 will make them permanent.</simpara>
\r
43 <simpara><emphasis role="strong">WARNING</emphasis>! The rewritten history will have different object names for all
\r
44 the objects and will not converge with the original branch. You will not
\r
45 be able to easily push and distribute the rewritten branch on top of the
\r
46 original branch. Please do not use this command if you do not know the
\r
47 full implications, and avoid using it anyway, if a simple single commit
\r
48 would suffice to fix your problem. (See the "RECOVERING FROM UPSTREAM
\r
49 REBASE" section in <xref linkend="git-rebase(1)" /> for further information about
\r
50 rewriting published history.)</simpara>
\r
51 <simpara>Always verify that the rewritten version is correct: The original refs,
\r
52 if different from the rewritten ones, will be stored in the namespace
\r
53 <emphasis>refs/original/</emphasis>.</simpara>
\r
54 <simpara>Note that since this operation is very I/O expensive, it might
\r
55 be a good idea to redirect the temporary directory off-disk with the
\r
56 <emphasis>-d</emphasis> option, e.g. on tmpfs. Reportedly the speedup is very noticeable.</simpara>
\r
57 <section id="git-filter-branch(1)__filters">
\r
58 <title>Filters</title>
\r
59 <simpara>The filters are applied in the order as listed below. The <command>
\r
60 argument is always evaluated in the shell context using the <emphasis>eval</emphasis> command
\r
61 (with the notable exception of the commit filter, for technical reasons).
\r
62 Prior to that, the $GIT_COMMIT environment variable will be set to contain
\r
63 the id of the commit being rewritten. Also, GIT_AUTHOR_NAME,
\r
64 GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL,
\r
65 and GIT_COMMITTER_DATE are taken from the current commit and exported to
\r
66 the environment, in order to affect the author and committer identities of
\r
67 the replacement commit created by <xref linkend="git-commit-tree(1)" /> after the
\r
68 filters have run.</simpara>
\r
69 <simpara>If any evaluation of <command> returns a non-zero exit status, the whole
\r
70 operation will be aborted.</simpara>
\r
71 <simpara>A <emphasis>map</emphasis> function is available that takes an "original sha1 id" argument
\r
72 and outputs a "rewritten sha1 id" if the commit has been already
\r
73 rewritten, and "original sha1 id" otherwise; the <emphasis>map</emphasis> function can
\r
74 return several ids on separate lines if your commit filter emitted
\r
75 multiple commits.</simpara>
\r
78 <simplesect id="git-filter-branch(1)__options">
\r
79 <title>OPTIONS</title>
\r
83 --env-filter <command>
\r
87 This filter may be used if you only need to modify the environment
\r
88 in which the commit will be performed. Specifically, you might
\r
89 want to rewrite the author/committer name/email/time environment
\r
90 variables (see <xref linkend="git-commit-tree(1)" /> for details). Do not forget
\r
91 to re-export the variables.
\r
97 --tree-filter <command>
\r
101 This is the filter for rewriting the tree and its contents.
\r
102 The argument is evaluated in shell with the working
\r
103 directory set to the root of the checked out tree. The new tree
\r
104 is then used as-is (new files are auto-added, disappeared files
\r
105 are auto-removed - neither .gitignore files nor any other ignore
\r
106 rules <emphasis role="strong">HAVE ANY EFFECT</emphasis>!).
\r
112 --index-filter <command>
\r
116 This is the filter for rewriting the index. It is similar to the
\r
117 tree filter but does not check out the tree, which makes it much
\r
118 faster. Frequently used with <emphasis>git rm --cached
\r
119 --ignore-unmatch ...</emphasis>, see EXAMPLES below. For hairy
\r
120 cases, see <xref linkend="git-update-index(1)" />.
\r
126 --parent-filter <command>
\r
130 This is the filter for rewriting the commit's parent list.
\r
131 It will receive the parent string on stdin and shall output
\r
132 the new parent string on stdout. The parent string is in
\r
133 the format described in <xref linkend="git-commit-tree(1)" />: empty for
\r
134 the initial commit, "-p parent" for a normal commit and
\r
135 "-p parent1 -p parent2 -p parent3 …" for a merge commit.
\r
141 --msg-filter <command>
\r
145 This is the filter for rewriting the commit messages.
\r
146 The argument is evaluated in the shell with the original
\r
147 commit message on standard input; its standard output is
\r
148 used as the new commit message.
\r
154 --commit-filter <command>
\r
158 This is the filter for performing the commit.
\r
159 If this filter is specified, it will be called instead of the
\r
160 <emphasis>git commit-tree</emphasis> command, with arguments of the form
\r
161 "<TREE_ID> [(-p <PARENT_COMMIT_ID>)…]" and the log message on
\r
162 stdin. The commit id is expected on stdout.
\r
164 <simpara>As a special extension, the commit filter may emit multiple
\r
165 commit ids; in that case, the rewritten children of the original commit will
\r
166 have all of them as parents.</simpara>
\r
167 <simpara>You can use the <emphasis>map</emphasis> convenience function in this filter, and other
\r
168 convenience functions, too. For example, calling <emphasis>skip_commit "$@"</emphasis>
\r
169 will leave out the current commit (but not its changes! If you want
\r
170 that, use <emphasis>git rebase</emphasis> instead).</simpara>
\r
171 <simpara>You can also use the <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead of
\r
172 <emphasis>git commit-tree "$@"</emphasis> if you don't wish to keep commits with a single parent
\r
173 and that makes no change to the tree.</simpara>
\r
178 --tag-name-filter <command>
\r
182 This is the filter for rewriting tag names. When passed,
\r
183 it will be called for every tag ref that points to a rewritten
\r
184 object (or to a tag object which points to a rewritten object).
\r
185 The original tag name is passed via standard input, and the new
\r
186 tag name is expected on standard output.
\r
188 <simpara>The original tags are not deleted, but can be overwritten;
\r
189 use "--tag-name-filter cat" to simply update the tags. In this
\r
190 case, be very careful and make sure you have the old tags
\r
191 backed up in case the conversion has run afoul.</simpara>
\r
192 <simpara>Nearly proper rewriting of tag objects is supported. If the tag has
\r
193 a message attached, a new tag object will be created with the same message,
\r
194 author, and timestamp. If the tag has a signature attached, the
\r
195 signature will be stripped. It is by definition impossible to preserve
\r
196 signatures. The reason this is "nearly" proper, is because ideally if
\r
197 the tag did not change (points to the same object, has the same name, etc.)
\r
198 it should retain any signature. That is not the case, signatures will always
\r
199 be removed, buyer beware. There is also no support for changing the
\r
200 author or timestamp (or the tag message for that matter). Tags which point
\r
201 to other tags will be rewritten to point to the underlying commit.</simpara>
\r
206 --subdirectory-filter <directory>
\r
210 Only look at the history which touches the given subdirectory.
\r
211 The result will contain that directory (and only that) as its
\r
212 project root. Implies <xref linkend="git-filter-branch(1)_Remap_to_ancestor"/>.
\r
222 Some kind of filters will generate empty commits, that left the tree
\r
223 untouched. This switch allow git-filter-branch to ignore such
\r
224 commits. Though, this switch only applies for commits that have one
\r
225 and only one parent, it will hence keep merges points. Also, this
\r
226 option is not compatible with the use of <emphasis>--commit-filter</emphasis>. Though you
\r
227 just need to use the function <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead
\r
228 of the <emphasis>git commit-tree "$@"</emphasis> idiom in your commit filter to make that
\r
235 --original <namespace>
\r
239 Use this option to set the namespace where the original commits
\r
240 will be stored. The default value is <emphasis>refs/original</emphasis>.
\r
246 -d <directory>
\r
250 Use this option to set the path to the temporary directory used for
\r
251 rewriting. When applying a tree filter, the command needs to
\r
252 temporarily check out the tree to some directory, which may consume
\r
253 considerable space in case of large projects. By default it
\r
254 does this in the <emphasis>.git-rewrite/</emphasis> directory but you can override
\r
255 that choice by this parameter.
\r
268 <emphasis>git filter-branch</emphasis> refuses to start with an existing temporary
\r
269 directory or when there are already refs starting with
\r
270 <emphasis>refs/original/</emphasis>, unless forced.
\r
276 <rev-list options>…
\r
280 Arguments for <emphasis>git rev-list</emphasis>. All positive refs included by
\r
281 these options are rewritten. You may also specify options
\r
282 such as <emphasis>--all</emphasis>, but you must use <emphasis>--</emphasis> to separate them from
\r
283 the <emphasis>git filter-branch</emphasis> options. Implies <xref linkend="git-filter-branch(1)_Remap_to_ancestor"/>.
\r
288 <section id="git-filter-branch(1)_Remap_to_ancestor">
\r
289 <title>Remap to ancestor</title>
\r
290 <simpara>By using <xref linkend="rev-list(1)" /> arguments, e.g., path limiters, you can limit the
\r
291 set of revisions which get rewritten. However, positive refs on the command
\r
292 line are distinguished: we don't let them be excluded by such limiters. For
\r
293 this purpose, they are instead rewritten to point at the nearest ancestor that
\r
294 was not excluded.</simpara>
\r
297 <simplesect id="git-filter-branch(1)__examples">
\r
298 <title>Examples</title>
\r
299 <simpara>Suppose you want to remove a file (containing confidential information
\r
300 or copyright violation) from all commits:</simpara>
\r
301 <screen>git filter-branch --tree-filter 'rm filename' HEAD</screen>
\r
302 <simpara>However, if the file is absent from the tree of some commit,
\r
303 a simple <emphasis>rm filename</emphasis> will fail for that tree and commit.
\r
304 Thus you may instead want to use <emphasis>rm -f filename</emphasis> as the script.</simpara>
\r
305 <simpara>Using <emphasis>--index-filter</emphasis> with <emphasis>git rm</emphasis> yields a significantly faster
\r
306 version. Like with using <emphasis>rm filename</emphasis>, <emphasis>git rm --cached filename</emphasis>
\r
307 will fail if the file is absent from the tree of a commit. If you
\r
308 want to "completely forget" a file, it does not matter when it entered
\r
309 history, so we also add <emphasis>--ignore-unmatch</emphasis>:</simpara>
\r
310 <screen>git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD</screen>
\r
311 <simpara>Now, you will get the rewritten history saved in HEAD.</simpara>
\r
312 <simpara>To rewrite the repository to look as if <emphasis>foodir/</emphasis> had been its project
\r
313 root, and discard all other history:</simpara>
\r
314 <screen>git filter-branch --subdirectory-filter foodir -- --all</screen>
\r
315 <simpara>Thus you can, e.g., turn a library subdirectory into a repository of
\r
316 its own. Note the <emphasis>--</emphasis> that separates <emphasis>filter-branch</emphasis> options from
\r
317 revision options, and the <emphasis>--all</emphasis> to rewrite all branches and tags.</simpara>
\r
318 <simpara>To set a commit (which typically is at the tip of another
\r
319 history) to be the parent of the current initial commit, in
\r
320 order to paste the other history behind the current history:</simpara>
\r
321 <screen>git filter-branch --parent-filter 'sed "s/^\$/-p <graft-id>/"' HEAD</screen>
\r
322 <simpara>(if the parent string is empty - which happens when we are dealing with
\r
323 the initial commit - add graftcommit as a parent). Note that this assumes
\r
324 history with a single root (that is, no merge without common ancestors
\r
325 happened). If this is not the case, use:</simpara>
\r
326 <screen>git filter-branch --parent-filter \
\r
327 'test $GIT_COMMIT = <commit-id> && echo "-p <graft-id>" || cat' HEAD</screen>
\r
328 <simpara>or even simpler:</simpara>
\r
329 <screen>echo "$commit-id $graft-id" >> .git/info/grafts
\r
330 git filter-branch $graft-id..HEAD</screen>
\r
331 <simpara>To remove commits authored by "Darl McBribe" from the history:</simpara>
\r
332 <screen>git filter-branch --commit-filter '
\r
333 if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];
\r
337 git commit-tree "$@";
\r
339 <simpara>The function <emphasis>skip_commit</emphasis> is defined as follows:</simpara>
\r
340 <screen>skip_commit()
\r
350 <simpara>The shift magic first throws away the tree id and then the -p
\r
351 parameters. Note that this handles merges properly! In case Darl
\r
352 committed a merge between P1 and P2, it will be propagated properly
\r
353 and all children of the merge will become merge commits with P1,P2
\r
354 as their parents instead of the merge commit.</simpara>
\r
355 <simpara><emphasis role="strong">NOTE</emphasis> the changes introduced by the commits, and which are not reverted
\r
356 by subsequent commits, will still be in the rewritten branch. If you want
\r
357 to throw out <emphasis>changes</emphasis> together with the commits, you should use the
\r
358 interactive mode of <emphasis>git rebase</emphasis>.</simpara>
\r
359 <simpara>You can rewrite the commit log messages using <emphasis>--msg-filter</emphasis>. For
\r
360 example, <emphasis>git svn-id</emphasis> strings in a repository created by <emphasis>git svn</emphasis> can
\r
361 be removed this way:</simpara>
\r
362 <screen>git filter-branch --msg-filter '
\r
363 sed -e "/^git-svn-id:/d"
\r
365 <simpara>If you need to add <emphasis>Acked-by</emphasis> lines to, say, the last 10 commits (none
\r
366 of which is a merge), use this command:</simpara>
\r
367 <screen>git filter-branch --msg-filter '
\r
369 echo "Acked-by: Bugs Bunny <bunny@bugzilla.org>"
\r
370 ' HEAD~10..HEAD</screen>
\r
371 <simpara>The <emphasis>--env-filter</emphasis> option can be used to modify committer and/or author
\r
372 identity. For example, if you found out that your commits have the wrong
\r
373 identity due to a misconfigured user.email, you can make a correction,
\r
374 before publishing the project, like this:</simpara>
\r
375 <screen>git filter-branch --env-filter '
\r
376 if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
\r
378 GIT_AUTHOR_EMAIL=john@example.com
\r
379 export GIT_AUTHOR_EMAIL
\r
381 if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
\r
383 GIT_COMMITTER_EMAIL=john@example.com
\r
384 export GIT_COMMITTER_EMAIL
\r
386 ' -- --all</screen>
\r
387 <simpara>To restrict rewriting to only part of the history, specify a revision
\r
388 range in addition to the new branch name. The new branch name will
\r
389 point to the top-most revision that a <emphasis>git rev-list</emphasis> of this range
\r
390 will print.</simpara>
\r
391 <simpara>Consider this history:</simpara>
\r
392 <screen> D--E--F--G--H
\r
394 A--B-----C</screen>
\r
395 <simpara>To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:</simpara>
\r
396 <screen>git filter-branch ... C..H</screen>
\r
397 <simpara>To rewrite commits E,F,G,H, use one of these:</simpara>
\r
398 <screen>git filter-branch ... C..H --not D
\r
399 git filter-branch ... D..H --not C</screen>
\r
400 <simpara>To move the whole tree into a subdirectory, or remove it from there:</simpara>
\r
401 <screen>git filter-branch --index-filter \
\r
402 'git ls-files -s | sed "s-\t\"*-&newsubdir/-" |
\r
403 GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
\r
404 git update-index --index-info &&
\r
405 mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD</screen>
\r
407 <simplesect id="git-filter-branch(1)__checklist_for_shrinking_a_repository">
\r
408 <title>Checklist for Shrinking a Repository</title>
\r
409 <simpara>git-filter-branch can be used to get rid of a subset of files,
\r
410 usually with some combination of <emphasis>--index-filter</emphasis> and
\r
411 <emphasis>--subdirectory-filter</emphasis>. People expect the resulting repository to
\r
412 be smaller than the original, but you need a few more steps to
\r
413 actually make it smaller, because Git tries hard not to lose your
\r
414 objects until you tell it to. First make sure that:</simpara>
\r
418 You really removed all variants of a filename, if a blob was moved
\r
419 over its lifetime. <emphasis>git log --name-only --follow --all -- filename</emphasis>
\r
420 can help you find renames.
\r
425 You really filtered all refs: use <emphasis>--tag-name-filter cat -- --all</emphasis>
\r
426 when calling git-filter-branch.
\r
430 <simpara>Then there are two ways to get a smaller repository. A safer way is
\r
431 to clone, that keeps your original intact.</simpara>
\r
435 Clone it with <emphasis>git clone file:///path/to/repo</emphasis>. The clone
\r
436 will not have the removed objects. See <xref linkend="git-clone(1)" />. (Note
\r
437 that cloning with a plain path just hardlinks everything!)
\r
441 <simpara>If you really don't want to clone it, for whatever reasons, check the
\r
442 following points instead (in this order). This is a very destructive
\r
443 approach, so <emphasis role="strong">make a backup</emphasis> or go back to cloning it. You have been
\r
448 Remove the original refs backed up by git-filter-branch: say <emphasis>git
\r
449 for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git
\r
450 update-ref -d</emphasis>.
\r
455 Expire all reflogs with <emphasis>git reflog expire --expire=now --all</emphasis>.
\r
460 Garbage collect all unreferenced objects with <emphasis>git gc --prune=now</emphasis>
\r
461 (or if your git-gc is not new enough to support arguments to
\r
462 <emphasis>--prune</emphasis>, use <emphasis>git repack -ad; git prune</emphasis> instead).
\r
467 <simplesect id="git-filter-branch(1)__notes">
\r
468 <title>Notes</title>
\r
469 <simpara>git-filter-branch allows you to make complex shell-scripted rewrites
\r
470 of your Git history, but you probably don't need this flexibility if
\r
471 you're simply <emphasis>removing unwanted data</emphasis> like large files or passwords.
\r
472 For those operations you may want to consider
\r
473 <ulink url="http://rtyley.github.io/bfg-repo-cleaner/[The">http://rtyley.github.io/bfg-repo-cleaner/[The</ulink> BFG Repo-Cleaner],
\r
474 a JVM-based alternative to git-filter-branch, typically at least
\r
475 10-50x faster for those use-cases, and with quite different
\r
476 characteristics:</simpara>
\r
480 Any particular version of a file is cleaned exactly <emphasis>once</emphasis>. The BFG,
\r
481 unlike git-filter-branch, does not give you the opportunity to
\r
482 handle a file differently based on where or when it was committed
\r
483 within your history. This constraint gives the core performance
\r
484 benefit of The BFG, and is well-suited to the task of cleansing bad
\r
485 data - you don't care <emphasis>where</emphasis> the bad data is, you just want it
\r
486 <emphasis>gone</emphasis>.
\r
491 By default The BFG takes full advantage of multi-core machines,
\r
492 cleansing commit file-trees in parallel. git-filter-branch cleans
\r
493 commits sequentially (i.e. in a single-threaded manner), though it
\r
494 <emphasis>is</emphasis> possible to write filters that include their own parallelism,
\r
495 in the scripts executed against each commit.
\r
500 The <ulink url="http://rtyley.github.io/bfg-repo-cleaner/#examples[command">http://rtyley.github.io/bfg-repo-cleaner/#examples[command</ulink> options]
\r
501 are much more restrictive than git-filter branch, and dedicated just
\r
502 to the tasks of removing unwanted data- e.g:
\r
503 <emphasis>--strip-blobs-bigger-than 1M</emphasis>.
\r
508 <simplesect id="git-filter-branch(1)__git">
\r
510 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
\r