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