updated git doc
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / git-filter-branch.xml
blobb027fe27f3e9f31dd1f50c30b6fa26644b5c9568
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-filter-branch(1)">\r
5 <articleinfo>\r
6     <title>git-filter-branch(1)</title>\r
7 <indexterm>\r
8 <primary>git-filter-branch(1)</primary>\r
9 </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-filter-branch - Rewrite branches</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <blockquote>\r
18 <literallayout><emphasis>git filter-branch</emphasis> [--env-filter &lt;command&gt;] [--tree-filter &lt;command&gt;]\r
19         [--index-filter &lt;command&gt;] [--parent-filter &lt;command&gt;]\r
20         [--msg-filter &lt;command&gt;] [--commit-filter &lt;command&gt;]\r
21         [--tag-name-filter &lt;command&gt;] [--subdirectory-filter &lt;directory&gt;]\r
22         [--prune-empty]\r
23         [--original &lt;namespace&gt;] [-d &lt;directory&gt;] [-f | --force]\r
24         [--] [&lt;rev-list options&gt;&#8230;]</literallayout>\r
25 </blockquote>\r
26 </simplesect>\r
27 <simplesect id="_description">\r
28 <title>DESCRIPTION</title>\r
29 <simpara>Lets you rewrite git revision history by rewriting the branches mentioned\r
30 in the &lt;rev-list options&gt;, applying custom filters on each revision.\r
31 Those filters can modify each tree (e.g. removing a file or running\r
32 a perl rewrite on all files) or information about each commit.\r
33 Otherwise, all information (including original commit times or merge\r
34 information) will be preserved.</simpara>\r
35 <simpara>The command will only rewrite the <emphasis>positive</emphasis> refs mentioned in the\r
36 command line (e.g. if you pass <emphasis>a..b</emphasis>, only <emphasis>b</emphasis> will be rewritten).\r
37 If you specify no filters, the commits will be recommitted without any\r
38 changes, which would normally have no effect.  Nevertheless, this may be\r
39 useful in the future for compensating for some git bugs or such,\r
40 therefore such a usage is permitted.</simpara>\r
41 <simpara><emphasis role="strong">NOTE</emphasis>: This command honors <emphasis>.git/info/grafts</emphasis> and <emphasis>.git/refs/replace/</emphasis>.\r
42 If you have any grafts or replacement refs defined, running this command\r
43 will make them permanent.</simpara>\r
44 <simpara><emphasis role="strong">WARNING</emphasis>! The rewritten history will have different object names for all\r
45 the objects and will not converge with the original branch.  You will not\r
46 be able to easily push and distribute the rewritten branch on top of the\r
47 original branch.  Please do not use this command if you do not know the\r
48 full implications, and avoid using it anyway, if a simple single commit\r
49 would suffice to fix your problem.  (See the "RECOVERING FROM UPSTREAM\r
50 REBASE" section in <xref linkend="git-rebase(1)" /> for further information about\r
51 rewriting published history.)</simpara>\r
52 <simpara>Always verify that the rewritten version is correct: The original refs,\r
53 if different from the rewritten ones, will be stored in the namespace\r
54 <emphasis>refs/original/</emphasis>.</simpara>\r
55 <simpara>Note that since this operation is very I/O expensive, it might\r
56 be a good idea to redirect the temporary directory off-disk with the\r
57 <emphasis>-d</emphasis> option, e.g. on tmpfs.  Reportedly the speedup is very noticeable.</simpara>\r
58 <section id="_filters">\r
59 <title>Filters</title>\r
60 <simpara>The filters are applied in the order as listed below.  The &lt;command&gt;\r
61 argument is always evaluated in the shell context using the <emphasis>eval</emphasis> command\r
62 (with the notable exception of the commit filter, for technical reasons).\r
63 Prior to that, the $GIT_COMMIT environment variable will be set to contain\r
64 the id of the commit being rewritten.  Also, GIT_AUTHOR_NAME,\r
65 GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL,\r
66 and GIT_COMMITTER_DATE are set according to the current commit.  The values\r
67 of these variables after the filters have run, are used for the new commit.\r
68 If any evaluation of &lt;command&gt; returns a non-zero exit status, the whole\r
69 operation will be aborted.</simpara>\r
70 <simpara>A <emphasis>map</emphasis> function is available that takes an "original sha1 id" argument\r
71 and outputs a "rewritten sha1 id" if the commit has been already\r
72 rewritten, and "original sha1 id" otherwise; the <emphasis>map</emphasis> function can\r
73 return several ids on separate lines if your commit filter emitted\r
74 multiple commits.</simpara>\r
75 </section>\r
76 </simplesect>\r
77 <simplesect id="_options">\r
78 <title>OPTIONS</title>\r
79 <variablelist>\r
80 <varlistentry>\r
81 <term>\r
82 --env-filter &lt;command&gt;\r
83 </term>\r
84 <listitem>\r
85 <simpara>\r
86         This filter may be used if you only need to modify the environment\r
87         in which the commit will be performed.  Specifically, you might\r
88         want to rewrite the author/committer name/email/time environment\r
89         variables (see <xref linkend="git-commit-tree(1)" /> for details).  Do not forget\r
90         to re-export the variables.\r
91 </simpara>\r
92 </listitem>\r
93 </varlistentry>\r
94 <varlistentry>\r
95 <term>\r
96 --tree-filter &lt;command&gt;\r
97 </term>\r
98 <listitem>\r
99 <simpara>\r
100         This is the filter for rewriting the tree and its contents.\r
101         The argument is evaluated in shell with the working\r
102         directory set to the root of the checked out tree.  The new tree\r
103         is then used as-is (new files are auto-added, disappeared files\r
104         are auto-removed - neither .gitignore files nor any other ignore\r
105         rules <emphasis role="strong">HAVE ANY EFFECT</emphasis>!).\r
106 </simpara>\r
107 </listitem>\r
108 </varlistentry>\r
109 <varlistentry>\r
110 <term>\r
111 --index-filter &lt;command&gt;\r
112 </term>\r
113 <listitem>\r
114 <simpara>\r
115         This is the filter for rewriting the index.  It is similar to the\r
116         tree filter but does not check out the tree, which makes it much\r
117         faster.  Frequently used with <emphasis>git rm --cached\r
118         --ignore-unmatch ...</emphasis>, see EXAMPLES below.  For hairy\r
119         cases, see <xref linkend="git-update-index(1)" />.\r
120 </simpara>\r
121 </listitem>\r
122 </varlistentry>\r
123 <varlistentry>\r
124 <term>\r
125 --parent-filter &lt;command&gt;\r
126 </term>\r
127 <listitem>\r
128 <simpara>\r
129         This is the filter for rewriting the commit's parent list.\r
130         It will receive the parent string on stdin and shall output\r
131         the new parent string on stdout.  The parent string is in\r
132         the format described in <xref linkend="git-commit-tree(1)" />: empty for\r
133         the initial commit, "-p parent" for a normal commit and\r
134         "-p parent1 -p parent2 -p parent3 &#8230;" for a merge commit.\r
135 </simpara>\r
136 </listitem>\r
137 </varlistentry>\r
138 <varlistentry>\r
139 <term>\r
140 --msg-filter &lt;command&gt;\r
141 </term>\r
142 <listitem>\r
143 <simpara>\r
144         This is the filter for rewriting the commit messages.\r
145         The argument is evaluated in the shell with the original\r
146         commit message on standard input; its standard output is\r
147         used as the new commit message.\r
148 </simpara>\r
149 </listitem>\r
150 </varlistentry>\r
151 <varlistentry>\r
152 <term>\r
153 --commit-filter &lt;command&gt;\r
154 </term>\r
155 <listitem>\r
156 <simpara>\r
157         This is the filter for performing the commit.\r
158         If this filter is specified, it will be called instead of the\r
159         <emphasis>git commit-tree</emphasis> command, with arguments of the form\r
160         "&lt;TREE_ID&gt; [(-p &lt;PARENT_COMMIT_ID&gt;)&#8230;]" and the log message on\r
161         stdin.  The commit id is expected on stdout.\r
162 </simpara>\r
163 <simpara>As a special extension, the commit filter may emit multiple\r
164 commit ids; in that case, the rewritten children of the original commit will\r
165 have all of them as parents.</simpara>\r
166 <simpara>You can use the <emphasis>map</emphasis> convenience function in this filter, and other\r
167 convenience functions, too.  For example, calling <emphasis>skip_commit "$@"</emphasis>\r
168 will leave out the current commit (but not its changes! If you want\r
169 that, use <emphasis>git rebase</emphasis> instead).</simpara>\r
170 <simpara>You can also use the <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead of\r
171 <emphasis>git commit-tree "$@"</emphasis> if you don't wish to keep commits with a single parent\r
172 and that makes no change to the tree.</simpara>\r
173 </listitem>\r
174 </varlistentry>\r
175 <varlistentry>\r
176 <term>\r
177 --tag-name-filter &lt;command&gt;\r
178 </term>\r
179 <listitem>\r
180 <simpara>\r
181         This is the filter for rewriting tag names. When passed,\r
182         it will be called for every tag ref that points to a rewritten\r
183         object (or to a tag object which points to a rewritten object).\r
184         The original tag name is passed via standard input, and the new\r
185         tag name is expected on standard output.\r
186 </simpara>\r
187 <simpara>The original tags are not deleted, but can be overwritten;\r
188 use "--tag-name-filter cat" to simply update the tags.  In this\r
189 case, be very careful and make sure you have the old tags\r
190 backed up in case the conversion has run afoul.</simpara>\r
191 <simpara>Nearly proper rewriting of tag objects is supported. If the tag has\r
192 a message attached, a new tag object will be created with the same message,\r
193 author, and timestamp. If the tag has a signature attached, the\r
194 signature will be stripped. It is by definition impossible to preserve\r
195 signatures. The reason this is "nearly" proper, is because ideally if\r
196 the tag did not change (points to the same object, has the same name, etc.)\r
197 it should retain any signature. That is not the case, signatures will always\r
198 be removed, buyer beware. There is also no support for changing the\r
199 author or timestamp (or the tag message for that matter). Tags which point\r
200 to other tags will be rewritten to point to the underlying commit.</simpara>\r
201 </listitem>\r
202 </varlistentry>\r
203 <varlistentry>\r
204 <term>\r
205 --subdirectory-filter &lt;directory&gt;\r
206 </term>\r
207 <listitem>\r
208 <simpara>\r
209         Only look at the history which touches the given subdirectory.\r
210         The result will contain that directory (and only that) as its\r
211         project root. Implies <xref linkend="Remap_to_ancestor"/>.\r
212 </simpara>\r
213 </listitem>\r
214 </varlistentry>\r
215 <varlistentry>\r
216 <term>\r
217 --prune-empty\r
218 </term>\r
219 <listitem>\r
220 <simpara>\r
221         Some kind of filters will generate empty commits, that left the tree\r
222         untouched.  This switch allow git-filter-branch to ignore such\r
223         commits.  Though, this switch only applies for commits that have one\r
224         and only one parent, it will hence keep merges points. Also, this\r
225         option is not compatible with the use of <emphasis>--commit-filter</emphasis>. Though you\r
226         just need to use the function <emphasis>git_commit_non_empty_tree "$@"</emphasis> instead\r
227         of the <emphasis>git commit-tree "$@"</emphasis> idiom in your commit filter to make that\r
228         happen.\r
229 </simpara>\r
230 </listitem>\r
231 </varlistentry>\r
232 <varlistentry>\r
233 <term>\r
234 --original &lt;namespace&gt;\r
235 </term>\r
236 <listitem>\r
237 <simpara>\r
238         Use this option to set the namespace where the original commits\r
239         will be stored. The default value is <emphasis>refs/original</emphasis>.\r
240 </simpara>\r
241 </listitem>\r
242 </varlistentry>\r
243 <varlistentry>\r
244 <term>\r
245 -d &lt;directory&gt;\r
246 </term>\r
247 <listitem>\r
248 <simpara>\r
249         Use this option to set the path to the temporary directory used for\r
250         rewriting.  When applying a tree filter, the command needs to\r
251         temporarily check out the tree to some directory, which may consume\r
252         considerable space in case of large projects.  By default it\r
253         does this in the <emphasis>.git-rewrite/</emphasis> directory but you can override\r
254         that choice by this parameter.\r
255 </simpara>\r
256 </listitem>\r
257 </varlistentry>\r
258 <varlistentry>\r
259 <term>\r
260 -f\r
261 </term>\r
262 <term>\r
263 --force\r
264 </term>\r
265 <listitem>\r
266 <simpara>\r
267         <emphasis>git filter-branch</emphasis> refuses to start with an existing temporary\r
268         directory or when there are already refs starting with\r
269         <emphasis>refs/original/</emphasis>, unless forced.\r
270 </simpara>\r
271 </listitem>\r
272 </varlistentry>\r
273 <varlistentry>\r
274 <term>\r
275 &lt;rev-list options&gt;&#8230;\r
276 </term>\r
277 <listitem>\r
278 <simpara>\r
279         Arguments for <emphasis>git rev-list</emphasis>.  All positive refs included by\r
280         these options are rewritten.  You may also specify options\r
281         such as <emphasis>--all</emphasis>, but you must use <emphasis>--</emphasis> to separate them from\r
282         the <emphasis>git filter-branch</emphasis> options. Implies <xref linkend="Remap_to_ancestor"/>.\r
283 </simpara>\r
284 </listitem>\r
285 </varlistentry>\r
286 </variablelist>\r
287 <section id="Remap_to_ancestor">\r
288 <title>Remap to ancestor</title>\r
289 <simpara>By using <xref linkend="rev-list(1)" /> arguments, e.g., path limiters, you can limit the\r
290 set of revisions which get rewritten. However, positive refs on the command\r
291 line are distinguished: we don't let them be excluded by such limiters. For\r
292 this purpose, they are instead rewritten to point at the nearest ancestor that\r
293 was not excluded.</simpara>\r
294 </section>\r
295 </simplesect>\r
296 <simplesect id="_examples">\r
297 <title>Examples</title>\r
298 <simpara>Suppose you want to remove a file (containing confidential information\r
299 or copyright violation) from all commits:</simpara>\r
300 <screen>git filter-branch --tree-filter 'rm filename' HEAD</screen>\r
301 <simpara>However, if the file is absent from the tree of some commit,\r
302 a simple <emphasis>rm filename</emphasis> will fail for that tree and commit.\r
303 Thus you may instead want to use <emphasis>rm -f filename</emphasis> as the script.</simpara>\r
304 <simpara>Using <emphasis>--index-filter</emphasis> with <emphasis>git rm</emphasis> yields a significantly faster\r
305 version.  Like with using <emphasis>rm filename</emphasis>, <emphasis>git rm --cached filename</emphasis>\r
306 will fail if the file is absent from the tree of a commit.  If you\r
307 want to "completely forget" a file, it does not matter when it entered\r
308 history, so we also add <emphasis>--ignore-unmatch</emphasis>:</simpara>\r
309 <screen>git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD</screen>\r
310 <simpara>Now, you will get the rewritten history saved in HEAD.</simpara>\r
311 <simpara>To rewrite the repository to look as if <emphasis>foodir/</emphasis> had been its project\r
312 root, and discard all other history:</simpara>\r
313 <screen>git filter-branch --subdirectory-filter foodir -- --all</screen>\r
314 <simpara>Thus you can, e.g., turn a library subdirectory into a repository of\r
315 its own.  Note the <emphasis>--</emphasis> that separates <emphasis>filter-branch</emphasis> options from\r
316 revision options, and the <emphasis>--all</emphasis> to rewrite all branches and tags.</simpara>\r
317 <simpara>To set a commit (which typically is at the tip of another\r
318 history) to be the parent of the current initial commit, in\r
319 order to paste the other history behind the current history:</simpara>\r
320 <screen>git filter-branch --parent-filter 'sed "s/^\$/-p &lt;graft-id&gt;/"' HEAD</screen>\r
321 <simpara>(if the parent string is empty - which happens when we are dealing with\r
322 the initial commit - add graftcommit as a parent).  Note that this assumes\r
323 history with a single root (that is, no merge without common ancestors\r
324 happened).  If this is not the case, use:</simpara>\r
325 <screen>git filter-branch --parent-filter \\r
326         'test $GIT_COMMIT = &lt;commit-id&gt; &amp;&amp; echo "-p &lt;graft-id&gt;" || cat' HEAD</screen>\r
327 <simpara>or even simpler:</simpara>\r
328 <screen>echo "$commit-id $graft-id" &gt;&gt; .git/info/grafts\r
329 git filter-branch $graft-id..HEAD</screen>\r
330 <simpara>To remove commits authored by "Darl McBribe" from the history:</simpara>\r
331 <screen>git filter-branch --commit-filter '\r
332         if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];\r
333         then\r
334                 skip_commit "$@";\r
335         else\r
336                 git commit-tree "$@";\r
337         fi' HEAD</screen>\r
338 <simpara>The function <emphasis>skip_commit</emphasis> is defined as follows:</simpara>\r
339 <screen>skip_commit()\r
341         shift;\r
342         while [ -n "$1" ];\r
343         do\r
344                 shift;\r
345                 map "$1";\r
346                 shift;\r
347         done;\r
348 }</screen>\r
349 <simpara>The shift magic first throws away the tree id and then the -p\r
350 parameters.  Note that this handles merges properly! In case Darl\r
351 committed a merge between P1 and P2, it will be propagated properly\r
352 and all children of the merge will become merge commits with P1,P2\r
353 as their parents instead of the merge commit.</simpara>\r
354 <simpara>You can rewrite the commit log messages using <emphasis>--msg-filter</emphasis>.  For\r
355 example, <emphasis>git svn-id</emphasis> strings in a repository created by <emphasis>git svn</emphasis> can\r
356 be removed this way:</simpara>\r
357 <screen>git filter-branch --msg-filter '\r
358         sed -e "/^git-svn-id:/d"\r
359 '</screen>\r
360 <simpara>To restrict rewriting to only part of the history, specify a revision\r
361 range in addition to the new branch name.  The new branch name will\r
362 point to the top-most revision that a <emphasis>git rev-list</emphasis> of this range\r
363 will print.</simpara>\r
364 <simpara>If you need to add <emphasis>Acked-by</emphasis> lines to, say, the last 10 commits (none\r
365 of which is a merge), use this command:</simpara>\r
366 <screen>git filter-branch --msg-filter '\r
367         cat &amp;&amp;\r
368         echo "Acked-by: Bugs Bunny &lt;bunny@bugzilla.org&gt;"\r
369 ' HEAD~10..HEAD</screen>\r
370 <simpara><emphasis role="strong">NOTE</emphasis> the changes introduced by the commits, and which are not reverted\r
371 by subsequent commits, will still be in the rewritten branch. If you want\r
372 to throw out <emphasis>changes</emphasis> together with the commits, you should use the\r
373 interactive mode of <emphasis>git rebase</emphasis>.</simpara>\r
374 <simpara>Consider this history:</simpara>\r
375 <screen>     D--E--F--G--H\r
376     /     /\r
377 A--B-----C</screen>\r
378 <simpara>To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:</simpara>\r
379 <screen>git filter-branch ... C..H</screen>\r
380 <simpara>To rewrite commits E,F,G,H, use one of these:</simpara>\r
381 <screen>git filter-branch ... C..H --not D\r
382 git filter-branch ... D..H --not C</screen>\r
383 <simpara>To move the whole tree into a subdirectory, or remove it from there:</simpara>\r
384 <screen>git filter-branch --index-filter \\r
385         'git ls-files -s | sed "s-\t\"*-&amp;newsubdir/-" |\r
386                 GIT_INDEX_FILE=$GIT_INDEX_FILE.new \\r
387                         git update-index --index-info &amp;&amp;\r
388          mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD</screen>\r
389 </simplesect>\r
390 <simplesect id="_checklist_for_shrinking_a_repository">\r
391 <title>Checklist for Shrinking a Repository</title>\r
392 <simpara>git-filter-branch is often used to get rid of a subset of files,\r
393 usually with some combination of <emphasis>--index-filter</emphasis> and\r
394 <emphasis>--subdirectory-filter</emphasis>.  People expect the resulting repository to\r
395 be smaller than the original, but you need a few more steps to\r
396 actually make it smaller, because git tries hard not to lose your\r
397 objects until you tell it to.  First make sure that:</simpara>\r
398 <itemizedlist>\r
399 <listitem>\r
400 <simpara>\r
401 You really removed all variants of a filename, if a blob was moved\r
402   over its lifetime.  <emphasis>git log --name-only --follow --all -- filename</emphasis>\r
403   can help you find renames.\r
404 </simpara>\r
405 </listitem>\r
406 <listitem>\r
407 <simpara>\r
408 You really filtered all refs: use <emphasis>--tag-name-filter cat -- --all</emphasis>\r
409   when calling git-filter-branch.\r
410 </simpara>\r
411 </listitem>\r
412 </itemizedlist>\r
413 <simpara>Then there are two ways to get a smaller repository.  A safer way is\r
414 to clone, that keeps your original intact.</simpara>\r
415 <itemizedlist>\r
416 <listitem>\r
417 <simpara>\r
418 Clone it with <emphasis>git clone file:///path/to/repo</emphasis>.  The clone\r
419   will not have the removed objects.  See <xref linkend="git-clone(1)" />.  (Note\r
420   that cloning with a plain path just hardlinks everything!)\r
421 </simpara>\r
422 </listitem>\r
423 </itemizedlist>\r
424 <simpara>If you really don't want to clone it, for whatever reasons, check the\r
425 following points instead (in this order).  This is a very destructive\r
426 approach, so <emphasis role="strong">make a backup</emphasis> or go back to cloning it.  You have been\r
427 warned.</simpara>\r
428 <itemizedlist>\r
429 <listitem>\r
430 <simpara>\r
431 Remove the original refs backed up by git-filter-branch: say <emphasis>git\r
432   for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git\r
433   update-ref -d</emphasis>.\r
434 </simpara>\r
435 </listitem>\r
436 <listitem>\r
437 <simpara>\r
438 Expire all reflogs with <emphasis>git reflog expire --expire=now --all</emphasis>.\r
439 </simpara>\r
440 </listitem>\r
441 <listitem>\r
442 <simpara>\r
443 Garbage collect all unreferenced objects with <emphasis>git gc --prune=now</emphasis>\r
444   (or if your git-gc is not new enough to support arguments to\r
445   <emphasis>--prune</emphasis>, use <emphasis>git repack -ad; git prune</emphasis> instead).\r
446 </simpara>\r
447 </listitem>\r
448 </itemizedlist>\r
449 </simplesect>\r
450 <simplesect id="_git">\r
451 <title>GIT</title>\r
452 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>\r
453 </simplesect>\r
454 </article>\r