1git-subtree(1) 2============== 3 4NAME 5---- 6git-subtree - Merge subtrees together and split repository into subtrees 7 8 9SYNOPSIS 10-------- 11[verse] 12'git subtree' add -P <prefix> <commit> 13'git subtree' pull -P <prefix> <repository> <refspec...> 14'git subtree' push -P <prefix> <repository> <refspec...> 15'git subtree' merge -P <prefix> <commit> 16'git subtree' split -P <prefix> [OPTIONS] [<commit>] 17 18 19DESCRIPTION 20----------- 21Subtrees allow subprojects to be included within a subdirectory 22of the main project, optionally including the subproject's 23entire history. 24 25For example, you could include the source code for a library 26as a subdirectory of your application. 27 28Subtrees are not to be confused with submodules, which are meant for 29the same task. Unlike submodules, subtrees do not need any special 30constructions (like .gitmodule files or gitlinks) be present in 31your repository, and do not force end-users of your 32repository to do anything special or to understand how subtrees 33work. A subtree is just a subdirectory that can be 34committed to, branched, and merged along with your project in 35any way you want. 36 37They are also not to be confused with using the subtree merge 38strategy. The main difference is that, besides merging 39the other project as a subdirectory, you can also extract the 40entire history of a subdirectory from your project and make it 41into a standalone project. Unlike the subtree merge strategy 42you can alternate back and forth between these 43two operations. If the standalone library gets updated, you can 44automatically merge the changes into your project; if you 45update the library inside your project, you can "split" the 46changes back out again and merge them back into the library 47project. 48 49For example, if a library you made for one application ends up being 50useful elsewhere, you can extract its entire history and publish 51that as its own git repository, without accidentally 52intermingling the history of your application project. 53 54[TIP] 55In order to keep your commit messages clean, we recommend that 56people split their commits between the subtrees and the main 57project as much as possible. That is, if you make a change that 58affects both the library and the main application, commit it in 59two pieces. That way, when you split the library commits out 60later, their descriptions will still make sense. But if this 61isn't important to you, it's not *necessary*. git subtree will 62simply leave out the non-library-related parts of the commit 63when it splits it out into the subproject later. 64 65 66COMMANDS 67-------- 68add:: 69 Create the <prefix> subtree by importing its contents 70 from the given <refspec> or <repository> and remote <refspec>. 71 A new commit is created automatically, joining the imported 72 project's history with your own. With '--squash', imports 73 only a single commit from the subproject, rather than its 74 entire history. 75 76merge:: 77 Merge recent changes up to <commit> into the <prefix> 78 subtree. As with normal 'git merge', this doesn't 79 remove your own local changes; it just merges those 80 changes into the latest <commit>. With '--squash', 81 creates only one commit that contains all the changes, 82 rather than merging in the entire history. 83 84 If you use '--squash', the merge direction doesn't 85 always have to be forward; you can use this command to 86 go back in time from v2.5 to v2.4, for example. If your 87 merge introduces a conflict, you can resolve it in the 88 usual ways. 89 90pull:: 91 Exactly like 'merge', but parallels 'git pull' in that 92 it fetches the given commit from the specified remote 93 repository. 94 95push:: 96 Does a 'split' (see above) using the <prefix> supplied 97 and then does a 'git push' to push the result to the 98 repository and refspec. This can be used to push your 99 subtree to different branches of the remote repository. 100 101split:: 102 Extract a new, synthetic project history from the 103 history of the <prefix> subtree. The new history 104 includes only the commits (including merges) that 105 affected <prefix>, and each of those commits now has the 106 contents of <prefix> at the root of the project instead 107 of in a subdirectory. Thus, the newly created history 108 is suitable for export as a separate git repository. 109 110 After splitting successfully, a single commit id is 111 printed to stdout. This corresponds to the HEAD of the 112 newly created tree, which you can manipulate however you 113 want. 114 115 Repeated splits of exactly the same history are 116 guaranteed to be identical (ie. to produce the same 117 commit ids). Because of this, if you add new commits 118 and then re-split, the new commits will be attached as 119 commits on top of the history you generated last time, 120 so 'git merge' and friends will work as expected. 121 122 Note that if you use '--squash' when you merge, you 123 should usually not just '--rejoin' when you split. 124 125 126OPTIONS 127------- 128-q:: 129--quiet:: 130 Suppress unnecessary output messages on stderr. 131 132-d:: 133--debug:: 134 Produce even more unnecessary output messages on stderr. 135 136-P <prefix>:: 137--prefix=<prefix>:: 138 Specify the path in the repository to the subtree you 139 want to manipulate. This option is mandatory 140 for all commands. 141 142-m <message>:: 143--message=<message>:: 144 This option is only valid for add, merge and pull (unsure). 145 Specify <message> as the commit message for the merge commit. 146 147 148OPTIONS FOR add, merge, push, pull 149---------------------------------- 150--squash:: 151 This option is only valid for add, merge, push and pull 152 commands. 153 154 Instead of merging the entire history from the subtree 155 project, produce only a single commit that contains all 156 the differences you want to merge, and then merge that 157 new commit into your project. 158 159 Using this option helps to reduce log clutter. People 160 rarely want to see every change that happened between 161 v1.0 and v1.1 of the library they're using, since none of the 162 interim versions were ever included in their application. 163 164 Using '--squash' also helps avoid problems when the same 165 subproject is included multiple times in the same 166 project, or is removed and then re-added. In such a 167 case, it doesn't make sense to combine the histories 168 anyway, since it's unclear which part of the history 169 belongs to which subtree. 170 171 Furthermore, with '--squash', you can switch back and 172 forth between different versions of a subtree, rather 173 than strictly forward. 'git subtree merge --squash' 174 always adjusts the subtree to match the exactly 175 specified commit, even if getting to that commit would 176 require undoing some changes that were added earlier. 177 178 Whether or not you use '--squash', changes made in your 179 local repository remain intact and can be later split 180 and send upstream to the subproject. 181 182 183OPTIONS FOR split 184----------------- 185--annotate=<annotation>:: 186 This option is only valid for the split command. 187 188 When generating synthetic history, add <annotation> as a 189 prefix to each commit message. Since we're creating new 190 commits with the same commit message, but possibly 191 different content, from the original commits, this can help 192 to differentiate them and avoid confusion. 193 194 Whenever you split, you need to use the same 195 <annotation>, or else you don't have a guarantee that 196 the new re-created history will be identical to the old 197 one. That will prevent merging from working correctly. 198 git subtree tries to make it work anyway, particularly 199 if you use --rejoin, but it may not always be effective. 200 201-b <branch>:: 202--branch=<branch>:: 203 This option is only valid for the split command. 204 205 After generating the synthetic history, create a new 206 branch called <branch> that contains the new history. 207 This is suitable for immediate pushing upstream. 208 <branch> must not already exist. 209 210--ignore-joins:: 211 This option is only valid for the split command. 212 213 If you use '--rejoin', git subtree attempts to optimize 214 its history reconstruction to generate only the new 215 commits since the last '--rejoin'. '--ignore-join' 216 disables this behaviour, forcing it to regenerate the 217 entire history. In a large project, this can take a 218 long time. 219 220--onto=<onto>:: 221 This option is only valid for the split command. 222 223 If your subtree was originally imported using something 224 other than git subtree, its history may not match what 225 git subtree is expecting. In that case, you can specify 226 the commit id <onto> that corresponds to the first 227 revision of the subproject's history that was imported 228 into your project, and git subtree will attempt to build 229 its history from there. 230 231 If you used 'git subtree add', you should never need 232 this option. 233 234--rejoin:: 235 This option is only valid for the split command. 236 237 After splitting, merge the newly created synthetic 238 history back into your main project. That way, future 239 splits can search only the part of history that has 240 been added since the most recent --rejoin. 241 242 If your split commits end up merged into the upstream 243 subproject, and then you want to get the latest upstream 244 version, this will allow git's merge algorithm to more 245 intelligently avoid conflicts (since it knows these 246 synthetic commits are already part of the upstream 247 repository). 248 249 Unfortunately, using this option results in 'git log' 250 showing an extra copy of every new commit that was 251 created (the original, and the synthetic one). 252 253 If you do all your merges with '--squash', don't use 254 '--rejoin' when you split, because you don't want the 255 subproject's history to be part of your project anyway. 256 257 258EXAMPLE 1. Add command 259---------------------- 260Let's assume that you have a local repository that you would like 261to add an external vendor library to. In this case we will add the 262git-subtree repository as a subdirectory of your already existing 263git-extensions repository in ~/git-extensions/: 264 265 $ git subtree add --prefix=git-subtree --squash \ 266 git://github.com/apenwarr/git-subtree.git master 267 268'master' needs to be a valid remote ref and can be a different branch 269name 270 271You can omit the --squash flag, but doing so will increase the number 272of commits that are incldued in your local repository. 273 274We now have a ~/git-extensions/git-subtree directory containing code 275from the master branch of git://github.com/apenwarr/git-subtree.git 276in our git-extensions repository. 277 278EXAMPLE 2. Extract a subtree using commit, merge and pull 279--------------------------------------------------------- 280Let's use the repository for the git source code as an example. 281First, get your own copy of the git.git repository: 282 283 $ git clone git://git.kernel.org/pub/scm/git/git.git test-git 284 $ cd test-git 285 286gitweb (commit 1130ef3) was merged into git as of commit 2870a8f4f0, after which it was no longer maintained separately. 288But imagine it had been maintained separately, and we wanted to 289extract git's changes to gitweb since that time, to share with 290the upstream. You could do this: 291 292 $ git subtree split --prefix=gitweb --annotate='(split) ' \ 293 0a8f4f0^.. --onto=1130ef3 --rejoin \ 294 --branch gitweb-latest 295 $ gitk gitweb-latest 296 $ git push git@github.com:whatever/gitweb.git gitweb-latest:master 297 298(We use '0a8f4f0^..' because that means "all the changes from 2990a8f4f0 to the current version, including 0a8f4f0 itself.") 300 301If gitweb had originally been merged using 'git subtree add' (or 302a previous split had already been done with --rejoin specified) 303then you can do all your splits without having to remember any 304weird commit ids: 305 306 $ git subtree split --prefix=gitweb --annotate='(split) ' --rejoin \ 307 --branch gitweb-latest2 308 309And you can merge changes back in from the upstream project just 310as easily: 311 312 $ git subtree pull --prefix=gitweb \ 313 git@github.com:whatever/gitweb.git master 314 315Or, using '--squash', you can actually rewind to an earlier 316version of gitweb: 317 318 $ git subtree merge --prefix=gitweb --squash gitweb-latest~10 319 320Then make some changes: 321 322 $ date >gitweb/myfile 323 $ git add gitweb/myfile 324 $ git commit -m 'created myfile' 325 326And fast forward again: 327 328 $ git subtree merge --prefix=gitweb --squash gitweb-latest 329 330And notice that your change is still intact: 331 332 $ ls -l gitweb/myfile 333 334And you can split it out and look at your changes versus 335the standard gitweb: 336 337 git log gitweb-latest..$(git subtree split --prefix=gitweb) 338 339EXAMPLE 3. Extract a subtree using branch 340----------------------------------------- 341Suppose you have a source directory with many files and 342subdirectories, and you want to extract the lib directory to its own 343git project. Here's a short way to do it: 344 345First, make the new repository wherever you want: 346 347 $ <go to the new location> 348 $ git init --bare 349 350Back in your original directory: 351 352 $ git subtree split --prefix=lib --annotate="(split)" -b split 353 354Then push the new branch onto the new empty repository: 355 356 $ git push <new-repo> split:master 357 358 359AUTHOR 360------ 361Written by Avery Pennarun <apenwarr@gmail.com> 362 363 364GIT 365--- 366Part of the linkgit:git[1] suite