1git-filter-branch(1) 2==================== 3 4NAME 5---- 6git-filter-branch - Rewrite branches 7 8SYNOPSIS 9-------- 10[verse] 11'git-filter-branch' [--env-filter <command>] [--tree-filter <command>] 12 [--index-filter <command>] [--parent-filter <command>] 13 [--msg-filter <command>] [--commit-filter <command>] 14 [--tag-name-filter <command>] [--subdirectory-filter <directory>] 15 [-d <directory>] <new-branch-name> [<rev-list options>...] 16 17DESCRIPTION 18----------- 19Lets you rewrite git revision history by creating a new branch from 20your current branch, applying custom filters on each revision. 21Those filters can modify each tree (e.g. removing a file or running 22a perl rewrite on all files) or information about each commit. 23Otherwise, all information (including original commit times or merge 24information) will be preserved. 25 26The command takes the new branch name as a mandatory argument and 27the filters as optional arguments. If you specify no filters, the 28commits will be recommitted without any changes, which would normally 29have no effect and result in the new branch pointing to the same 30branch as your current branch. Nevertheless, this may be useful in 31the future for compensating for some git bugs or such, therefore 32such a usage is permitted. 33 34*WARNING*! The rewritten history will have different object names for all 35the objects and will not converge with the original branch. You will not 36be able to easily push and distribute the rewritten branch on top of the 37original branch. Please do not use this command if you do not know the 38full implications, and avoid using it anyway, if a simple single commit 39would suffice to fix your problem. 40 41Always verify that the rewritten version is correct before disposing 42the original branch. 43 44Note that since this operation is extensively I/O expensive, it might 45be a good idea to redirect the temporary directory off-disk, e.g. on 46tmpfs. Reportedly the speedup is very noticeable. 47 48 49Filters 50~~~~~~~ 51 52The filters are applied in the order as listed below. The <command> 53argument is always evaluated in shell using the 'eval' command. 54Prior to that, the $GIT_COMMIT environment variable will be set to contain 55the id of the commit being rewritten. Also, GIT_AUTHOR_NAME, 56GIT_AUTHOR_EMAIL, GIT_AUTHOR_DATE, GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, 57and GIT_COMMITTER_DATE are set according to the current commit. 58 59A 'map' function is available that takes an "original sha1 id" argument 60and outputs a "rewritten sha1 id" if the commit has been already 61rewritten, and "original sha1 id" otherwise; the 'map' function can 62return several ids on separate lines if your commit filter emitted 63multiple commits. 64 65 66OPTIONS 67------- 68 69--env-filter <command>:: 70 This is the filter for modifying the environment in which 71 the commit will be performed. Specifically, you might want 72 to rewrite the author/committer name/email/time environment 73 variables (see gitlink:git-commit[1] for details). Do not forget 74 to re-export the variables. 75 76--tree-filter <command>:: 77 This is the filter for rewriting the tree and its contents. 78 The argument is evaluated in shell with the working 79 directory set to the root of the checked out tree. The new tree 80 is then used as-is (new files are auto-added, disappeared files 81 are auto-removed - neither .gitignore files nor any other ignore 82 rules *HAVE ANY EFFECT*!). 83 84--index-filter <command>:: 85 This is the filter for rewriting the index. It is similar to the 86 tree filter but does not check out the tree, which makes it much 87 faster. For hairy cases, see gitlink:git-update-index[1]. 88 89--parent-filter <command>:: 90 This is the filter for rewriting the commit's parent list. 91 It will receive the parent string on stdin and shall output 92 the new parent string on stdout. The parent string is in 93 a format accepted by gitlink:git-commit-tree[1]: empty for 94 the initial commit, "-p parent" for a normal commit and 95 "-p parent1 -p parent2 -p parent3 ..." for a merge commit. 96 97--msg-filter <command>:: 98 This is the filter for rewriting the commit messages. 99 The argument is evaluated in the shell with the original 100 commit message on standard input; its standard output is 101 used as the new commit message. 102 103--commit-filter <command>:: 104 This is the filter for performing the commit. 105 If this filter is specified, it will be called instead of the 106 gitlink:git-commit-tree[1] command, with arguments of the form 107 "<TREE_ID> [-p <PARENT_COMMIT_ID>]..." and the log message on 108 stdin. The commit id is expected on stdout. 109+ 110As a special extension, the commit filter may emit multiple 111commit ids; in that case, ancestors of the original commit will 112have all of them as parents. 113 114--tag-name-filter <command>:: 115 This is the filter for rewriting tag names. When passed, 116 it will be called for every tag ref that points to a rewritten 117 object (or to a tag object which points to a rewritten object). 118 The original tag name is passed via standard input, and the new 119 tag name is expected on standard output. 120+ 121The original tags are not deleted, but can be overwritten; 122use "--tag-name-filter=cat" to simply update the tags. In this 123case, be very careful and make sure you have the old tags 124backed up in case the conversion has run afoul. 125+ 126Note that there is currently no support for proper rewriting of 127tag objects; in layman terms, if the tag has a message or signature 128attached, the rewritten tag won't have it. Sorry. (It is by 129definition impossible to preserve signatures at any rate.) 130 131--subdirectory-filter <directory>:: 132 Only look at the history which touches the given subdirectory. 133 The result will contain that directory (and only that) as its 134 project root. 135 136-d <directory>:: 137 Use this option to set the path to the temporary directory used for 138 rewriting. When applying a tree filter, the command needs to 139 temporary checkout the tree to some directory, which may consume 140 considerable space in case of large projects. By default it 141 does this in the '.git-rewrite/' directory but you can override 142 that choice by this parameter. 143 144<rev-list-options>:: 145 When options are given after the new branch name, they will 146 be passed to gitlink:git-rev-list[1]. Only commits in the resulting 147 output will be filtered, although the filtered commits can still 148 reference parents which are outside of that set. 149 150 151Examples 152-------- 153 154Suppose you want to remove a file (containing confidential information 155or copyright violation) from all commits: 156 157------------------------------------------------------- 158git filter-branch --tree-filter 'rm filename' newbranch 159------------------------------------------------------- 160 161A significantly faster version: 162 163------------------------------------------------------------------------------- 164git filter-branch --index-filter 'git update-index --remove filename' newbranch 165------------------------------------------------------------------------------- 166 167Now, you will get the rewritten history saved in the branch 'newbranch' 168(your current branch is left untouched). 169 170To set a commit (which typically is at the tip of another 171history) to be the parent of the current initial commit, in 172order to paste the other history behind the current history: 173 174------------------------------------------------------------------------ 175git filter-branch --parent-filter 'sed "s/^\$/-p <graft-id>/"' newbranch 176------------------------------------------------------------------------ 177 178(if the parent string is empty - therefore we are dealing with the 179initial commit - add graftcommit as a parent). Note that this assumes 180history with a single root (that is, no merge without common ancestors 181happened). If this is not the case, use: 182 183------------------------------------------------------------------------------- 184git filter-branch --parent-filter \ 185 'cat; test $GIT_COMMIT = <commit-id> && echo "-p <graft-id>"' newbranch 186------------------------------------------------------------------------------- 187 188or even simpler: 189 190----------------------------------------------- 191echo "$commit-id $graft-id" >> .git/info/grafts 192git filter-branch newbranch $graft-id.. 193----------------------------------------------- 194 195To remove commits authored by "Darl McBribe" from the history: 196 197------------------------------------------------------------------------------ 198git filter-branch --commit-filter ' 199 if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ]; 200 then 201 shift; 202 while [ -n "$1" ]; 203 do 204 shift; 205 echo "$1"; 206 shift; 207 done; 208 else 209 git commit-tree "$@"; 210 fi' newbranch 211------------------------------------------------------------------------------ 212 213The shift magic first throws away the tree id and then the -p 214parameters. Note that this handles merges properly! In case Darl 215committed a merge between P1 and P2, it will be propagated properly 216and all children of the merge will become merge commits with P1,P2 217as their parents instead of the merge commit. 218 219To restrict rewriting to only part of the history, specify a revision 220range in addition to the new branch name. The new branch name will 221point to the top-most revision that a 'git rev-list' of this range 222will print. 223 224Note that the changes introduced by the commits, and not reverted by 225subsequent commits, will still be in the rewritten branch. If you want 226to throw out _changes_ together with the commits, you should use the 227interactive mode of gitlink:git-rebase[1]. 228 229Consider this history: 230 231------------------ 232 D--E--F--G--H 233 / / 234A--B-----C 235------------------ 236 237To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use: 238 239-------------------------------- 240git filter-branch ... new-H C..H 241-------------------------------- 242 243To rewrite commits E,F,G,H, use one of these: 244 245---------------------------------------- 246git filter-branch ... new-H C..H --not D 247git filter-branch ... new-H D..H --not C 248---------------------------------------- 249 250To move the whole tree into a subdirectory, or remove it from there: 251 252--------------------------------------------------------------- 253git filter-branch --index-filter \ 254 'git ls-files -s | sed "s-\t-&newsubdir/-" | 255 GIT_INDEX_FILE=$GIT_INDEX_FILE.new \ 256 git update-index --index-info && 257 mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' directorymoved 258--------------------------------------------------------------- 259 260 261Author 262------ 263Written by Petr "Pasky" Baudis <pasky@suse.cz>, 264and the git list <git@vger.kernel.org> 265 266Documentation 267-------------- 268Documentation by Petr Baudis and the git list. 269 270GIT 271--- 272Part of the gitlink:git[7] suite