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