6 git-subtree - Merge subtrees together and split repository into subtrees
12 'git subtree' [<options>] -P <prefix> add <local-commit>
13 'git subtree' [<options>] -P <prefix> add <repository> <remote-ref>
14 'git subtree' [<options>] -P <prefix> merge <local-commit>
15 'git subtree' [<options>] -P <prefix> split [<local-commit>]
18 'git subtree' [<options>] -P <prefix> pull <repository> <remote-ref>
19 'git subtree' [<options>] -P <prefix> push <repository> <remote-ref>
23 Subtrees allow subprojects to be included within a subdirectory
24 of the main project, optionally including the subproject's
27 For example, you could include the source code for a library
28 as a subdirectory of your application.
30 Subtrees are not to be confused with submodules, which are meant for
31 the same task. Unlike submodules, subtrees do not need any special
32 constructions (like '.gitmodules' files or gitlinks) be present in
33 your repository, and do not force end-users of your
34 repository to do anything special or to understand how subtrees
35 work. A subtree is just a subdirectory that can be
36 committed to, branched, and merged along with your project in
39 They are also not to be confused with using the subtree merge
40 strategy. The main difference is that, besides merging
41 the other project as a subdirectory, you can also extract the
42 entire history of a subdirectory from your project and make it
43 into a standalone project. Unlike the subtree merge strategy
44 you can alternate back and forth between these
45 two operations. If the standalone library gets updated, you can
46 automatically merge the changes into your project; if you
47 update the library inside your project, you can "split" the
48 changes back out again and merge them back into the library
51 For example, if a library you made for one application ends up being
52 useful elsewhere, you can extract its entire history and publish
53 that as its own git repository, without accidentally
54 intermingling the history of your application project.
57 In order to keep your commit messages clean, we recommend that
58 people split their commits between the subtrees and the main
59 project as much as possible. That is, if you make a change that
60 affects both the library and the main application, commit it in
61 two pieces. That way, when you split the library commits out
62 later, their descriptions will still make sense. But if this
63 isn't important to you, it's not *necessary*. 'git subtree' will
64 simply leave out the non-library-related parts of the commit
65 when it splits it out into the subproject later.
71 add <repository> <remote-ref>::
72 Create the <prefix> subtree by importing its contents
73 from the given <local-commit> or <repository> and <remote-ref>.
74 A new commit is created automatically, joining the imported
75 project's history with your own. With '--squash', import
76 only a single commit from the subproject, rather than its
79 merge <local-commit>::
80 Merge recent changes up to <local-commit> into the <prefix>
81 subtree. As with normal 'git merge', this doesn't
82 remove your own local changes; it just merges those
83 changes into the latest <local-commit>. With '--squash',
84 create only one commit that contains all the changes,
85 rather than merging in the entire history.
87 If you use '--squash', the merge direction doesn't always have to be
88 forward; you can use this command to go back in time from v2.5 to v2.4,
89 for example. If your merge introduces a conflict, you can resolve it in
92 split [<local-commit>]::
93 Extract a new, synthetic project history from the
94 history of the <prefix> subtree of <local-commit>, or of
95 HEAD if no <local-commit> is given. The new history
96 includes only the commits (including merges) that
97 affected <prefix>, and each of those commits now has the
98 contents of <prefix> at the root of the project instead
99 of in a subdirectory. Thus, the newly created history
100 is suitable for export as a separate git repository.
102 After splitting successfully, a single commit ID is printed to stdout.
103 This corresponds to the HEAD of the newly created tree, which you can
104 manipulate however you want.
106 Repeated splits of exactly the same history are guaranteed to be
107 identical (i.e. to produce the same commit IDs) as long as the
108 settings passed to 'split' (such as '--annotate') are the same.
109 Because of this, if you add new commits and then re-split, the new
110 commits will be attached as commits on top of the history you
111 generated last time, so 'git merge' and friends will work as expected.
113 pull <repository> <remote-ref>::
114 Exactly like 'merge', but parallels 'git pull' in that
115 it fetches the given ref from the specified remote
118 push <repository> <remote-ref>::
119 Does a 'split' using the <prefix> subtree of HEAD and then
120 does a 'git push' to push the result to the <repository> and
121 <remote-ref>. This can be used to push your subtree to
122 different branches of the remote repository.
124 OPTIONS FOR ALL COMMANDS
125 ------------------------
128 Suppress unnecessary output messages on stderr.
132 Produce even more unnecessary output messages on stderr.
136 Specify the path in the repository to the subtree you
137 want to manipulate. This option is mandatory
140 OPTIONS FOR 'add' AND 'merge' (ALSO: 'pull' AND 'split --rejoin')
141 -----------------------------------------------------------------
142 These options for 'add' and 'merge' may also be given to 'pull' (which
143 wraps 'merge') and 'split --rejoin' (which wraps either 'add' or
144 'merge' as appropriate).
147 Instead of merging the entire history from the subtree project, produce
148 only a single commit that contains all the differences you want to
149 merge, and then merge that new commit into your project.
151 Using this option helps to reduce log clutter. People rarely want to see
152 every change that happened between v1.0 and v1.1 of the library they're
153 using, since none of the interim versions were ever included in their
156 Using '--squash' also helps avoid problems when the same subproject is
157 included multiple times in the same project, or is removed and then
158 re-added. In such a case, it doesn't make sense to combine the
159 histories anyway, since it's unclear which part of the history belongs
162 Furthermore, with '--squash', you can switch back and forth between
163 different versions of a subtree, rather than strictly forward. 'git
164 subtree merge --squash' always adjusts the subtree to match the exactly
165 specified commit, even if getting to that commit would require undoing
166 some changes that were added earlier.
168 Whether or not you use '--squash', changes made in your local repository
169 remain intact and can be later split and send upstream to the
173 --message=<message>::
174 Specify <message> as the commit message for the merge commit.
178 These options are only valid for 'split'.
180 --annotate=<annotation>::
181 When generating synthetic history, add <annotation> as a prefix to each
182 commit message. Since we're creating new commits with the same commit
183 message, but possibly different content, from the original commits, this
184 can help to differentiate them and avoid confusion.
186 Whenever you split, you need to use the same <annotation>, or else you
187 don't have a guarantee that the new re-created history will be identical
188 to the old one. That will prevent merging from working correctly. git
189 subtree tries to make it work anyway, particularly if you use '--rejoin',
190 but it may not always be effective.
194 After generating the synthetic history, create a new branch called
195 <branch> that contains the new history. This is suitable for immediate
196 pushing upstream. <branch> must not already exist.
199 If you use '--rejoin', git subtree attempts to optimize its history
200 reconstruction to generate only the new commits since the last
201 '--rejoin'. '--ignore-joins' disables this behavior, forcing it to
202 regenerate the entire history. In a large project, this can take a long
206 If your subtree was originally imported using something other than git
207 subtree, its history may not match what git subtree is expecting. In
208 that case, you can specify the commit ID <onto> that corresponds to the
209 first revision of the subproject's history that was imported into your
210 project, and git subtree will attempt to build its history from there.
212 If you used 'git subtree add', you should never need this option.
215 After splitting, merge the newly created synthetic history back into
216 your main project. That way, future splits can search only the part of
217 history that has been added since the most recent '--rejoin'.
219 If your split commits end up merged into the upstream subproject, and
220 then you want to get the latest upstream version, this will allow git's
221 merge algorithm to more intelligently avoid conflicts (since it knows
222 these synthetic commits are already part of the upstream repository).
224 Unfortunately, using this option results in 'git log' showing an extra
225 copy of every new commit that was created (the original, and the
228 If you do all your merges with '--squash', make sure you also use
229 '--squash' when you 'split --rejoin'.
232 EXAMPLE 1. 'add' command
233 ------------------------
234 Let's assume that you have a local repository that you would like
235 to add an external vendor library to. In this case we will add the
236 git-subtree repository as a subdirectory of your already existing
237 git-extensions repository in ~/git-extensions/:
239 $ git subtree add --prefix=git-subtree --squash \
240 git://github.com/apenwarr/git-subtree.git master
242 'master' needs to be a valid remote ref and can be a different branch
245 You can omit the '--squash' flag, but doing so will increase the number
246 of commits that are included in your local repository.
248 We now have a ~/git-extensions/git-subtree directory containing code
249 from the master branch of git://github.com/apenwarr/git-subtree.git
250 in our git-extensions repository.
252 EXAMPLE 2. Extract a subtree using 'commit', 'merge' and 'pull'
253 ---------------------------------------------------------------
254 Let's use the repository for the git source code as an example.
255 First, get your own copy of the git.git repository:
257 $ git clone git://git.kernel.org/pub/scm/git/git.git test-git
260 gitweb (commit 1130ef3) was merged into git as of commit
261 0a8f4f0, after which it was no longer maintained separately.
262 But imagine it had been maintained separately, and we wanted to
263 extract git's changes to gitweb since that time, to share with
264 the upstream. You could do this:
266 $ git subtree split --prefix=gitweb --annotate='(split) ' \
267 0a8f4f0^.. --onto=1130ef3 --rejoin \
268 --branch gitweb-latest
270 $ git push git@github.com:whatever/gitweb.git gitweb-latest:master
272 (We use '0a8f4f0^..' because that means "all the changes from
273 0a8f4f0 to the current version, including 0a8f4f0 itself.")
275 If gitweb had originally been merged using 'git subtree add' (or
276 a previous split had already been done with '--rejoin' specified)
277 then you can do all your splits without having to remember any
280 $ git subtree split --prefix=gitweb --annotate='(split) ' --rejoin \
281 --branch gitweb-latest2
283 And you can merge changes back in from the upstream project just
286 $ git subtree pull --prefix=gitweb \
287 git@github.com:whatever/gitweb.git master
289 Or, using '--squash', you can actually rewind to an earlier
292 $ git subtree merge --prefix=gitweb --squash gitweb-latest~10
294 Then make some changes:
296 $ date >gitweb/myfile
297 $ git add gitweb/myfile
298 $ git commit -m 'created myfile'
300 And fast forward again:
302 $ git subtree merge --prefix=gitweb --squash gitweb-latest
304 And notice that your change is still intact:
306 $ ls -l gitweb/myfile
308 And you can split it out and look at your changes versus
311 git log gitweb-latest..$(git subtree split --prefix=gitweb)
313 EXAMPLE 3. Extract a subtree using a branch
314 -------------------------------------------
315 Suppose you have a source directory with many files and
316 subdirectories, and you want to extract the lib directory to its own
317 git project. Here's a short way to do it:
319 First, make the new repository wherever you want:
321 $ <go to the new location>
324 Back in your original directory:
326 $ git subtree split --prefix=lib --annotate="(split)" -b split
328 Then push the new branch onto the new empty repository:
330 $ git push <new-repo> split:master
335 Written by Avery Pennarun <apenwarr@gmail.com>
340 Part of the linkgit:git[1] suite