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