1GIT-SUBTREE(1)                    Git Manual                    GIT-SUBTREE(1)
2
3
4

NAME

6       git-subtree - Merge subtrees together and split repository into
7       subtrees
8

SYNOPSIS

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
17

DESCRIPTION

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

COMMANDS

60       add
61           Create the <prefix> subtree by importing its contents from the
62           given <commit> or <repository> and remote <ref>. A new commit is
63           created automatically, joining the imported project’s history with
64           your own. With --squash, imports only a single commit from the
65           subproject, rather than its entire history.
66
67       merge
68           Merge recent changes up to <commit> into the <prefix> subtree. As
69           with normal git merge, this doesn’t remove your own local changes;
70           it just merges those changes into the latest <commit>. With
71           --squash, creates only one commit that contains all the changes,
72           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       pull
80           Exactly like merge, but parallels git pull in that it fetches the
81           given ref from the specified remote repository.
82
83       push
84           Does a split (see below) using the <prefix> supplied and then does
85           a git push to push the result to the repository and ref. This can
86           be used to push your subtree to different branches of the remote
87           repository.
88
89       split
90           Extract a new, synthetic project history from the history of the
91           <prefix> subtree. The new history includes only the commits
92           (including merges) that affected <prefix>, and each of those
93           commits now has the contents of <prefix> at the root of the project
94           instead of in a subdirectory. Thus, the newly created history is
95           suitable for export as a separate git repository.
96
97           After splitting successfully, a single commit id is printed to
98           stdout. This corresponds to the HEAD of the newly created tree,
99           which you can manipulate however you want.
100
101           Repeated splits of exactly the same history are guaranteed to be
102           identical (i.e. to produce the same commit ids). Because of this,
103           if you add new commits and then re-split, the new commits will be
104           attached as commits on top of the history you generated last time,
105           so git merge and friends will work as expected.
106
107           Note that if you use --squash when you merge, you should usually
108           not just --rejoin when you split.
109

OPTIONS

111       -q, --quiet
112           Suppress unnecessary output messages on stderr.
113
114       -d, --debug
115           Produce even more unnecessary output messages on stderr.
116
117       -P <prefix>, --prefix=<prefix>
118           Specify the path in the repository to the subtree you want to
119           manipulate. This option is mandatory for all commands.
120
121       -m <message>, --message=<message>
122           This option is only valid for add, merge and pull (unsure). Specify
123           <message> as the commit message for the merge commit.
124

OPTIONS FOR ADD, MERGE, PUSH, PULL

126       --squash
127           This option is only valid for add, merge, and pull commands.
128
129           Instead of merging the entire history from the subtree project,
130           produce only a single commit that contains all the differences you
131           want to merge, and then merge that new commit into your project.
132
133           Using this option helps to reduce log clutter. People rarely want
134           to see every change that happened between v1.0 and v1.1 of the
135           library they’re using, since none of the interim versions were ever
136           included in their application.
137
138           Using --squash also helps avoid problems when the same subproject
139           is included multiple times in the same project, or is removed and
140           then re-added. In such a case, it doesn’t make sense to combine the
141           histories anyway, since it’s unclear which part of the history
142           belongs to which subtree.
143
144           Furthermore, with --squash, you can switch back and forth between
145           different versions of a subtree, rather than strictly forward.  git
146           subtree merge --squash always adjusts the subtree to match the
147           exactly specified commit, even if getting to that commit would
148           require undoing some changes that were added earlier.
149
150           Whether or not you use --squash, changes made in your local
151           repository remain intact and can be later split and send upstream
152           to the subproject.
153

OPTIONS FOR SPLIT

155       --annotate=<annotation>
156           This option is only valid for the split command.
157
158           When generating synthetic history, add <annotation> as a prefix to
159           each commit message. Since we’re creating new commits with the same
160           commit message, but possibly different content, from the original
161           commits, this can help to differentiate them and avoid confusion.
162
163           Whenever you split, you need to use the same <annotation>, or else
164           you don’t have a guarantee that the new re-created history will be
165           identical to the old one. That will prevent merging from working
166           correctly. git subtree tries to make it work anyway, particularly
167           if you use --rejoin, but it may not always be effective.
168
169       -b <branch>, --branch=<branch>
170           This option is only valid for the split command.
171
172           After generating the synthetic history, create a new branch called
173           <branch> that contains the new history. This is suitable for
174           immediate pushing upstream. <branch> must not already exist.
175
176       --ignore-joins
177           This option is only valid for the split command.
178
179           If you use --rejoin, git subtree attempts to optimize its history
180           reconstruction to generate only the new commits since the last
181           --rejoin.  --ignore-join disables this behaviour, forcing it to
182           regenerate the entire history. In a large project, this can take a
183           long time.
184
185       --onto=<onto>
186           This option is only valid for the split command.
187
188           If your subtree was originally imported using something other than
189           git subtree, its history may not match what git subtree is
190           expecting. In that case, you can specify the commit id <onto> that
191           corresponds to the first revision of the subproject’s history that
192           was imported into your project, and git subtree will attempt to
193           build its history from there.
194
195           If you used git subtree add, you should never need this option.
196
197       --rejoin
198           This option is only valid for the split command.
199
200           After splitting, merge the newly created synthetic history back
201           into your main project. That way, future splits can search only the
202           part of history that has been added since the most recent --rejoin.
203
204           If your split commits end up merged into the upstream subproject,
205           and then you want to get the latest upstream version, this will
206           allow git’s merge algorithm to more intelligently avoid conflicts
207           (since it knows these synthetic commits are already part of the
208           upstream repository).
209
210           Unfortunately, using this option results in git log showing an
211           extra copy of every new commit that was created (the original, and
212           the synthetic one).
213
214           If you do all your merges with --squash, don’t use --rejoin when
215           you split, because you don’t want the subproject’s history to be
216           part of your project anyway.
217

EXAMPLE 1. ADD COMMAND

219       Let’s assume that you have a local repository that you would like to
220       add an external vendor library to. In this case we will add the
221       git-subtree repository as a subdirectory of your already existing
222       git-extensions repository in ~/git-extensions/:
223
224           $ git subtree add --prefix=git-subtree --squash \
225                   git://github.com/apenwarr/git-subtree.git master
226
227       master needs to be a valid remote ref and can be a different branch
228       name
229
230       You can omit the --squash flag, but doing so will increase the number
231       of commits that are included in your local repository.
232
233       We now have a ~/git-extensions/git-subtree directory containing code
234       from the master branch of git://github.com/apenwarr/git-subtree.git in
235       our git-extensions repository.
236

EXAMPLE 2. EXTRACT A SUBTREE USING COMMIT, MERGE AND PULL

238       Let’s use the repository for the git source code as an example. First,
239       get your own copy of the git.git repository:
240
241           $ git clone git://git.kernel.org/pub/scm/git/git.git test-git
242           $ cd test-git
243
244       gitweb (commit 1130ef3) was merged into git as of commit 0a8f4f0, after
245       which it was no longer maintained separately. But imagine it had been
246       maintained separately, and we wanted to extract git’s changes to gitweb
247       since that time, to share with the upstream. You could do this:
248
249           $ git subtree split --prefix=gitweb --annotate='(split) ' \
250                   0a8f4f0^.. --onto=1130ef3 --rejoin \
251                   --branch gitweb-latest
252           $ gitk gitweb-latest
253           $ git push git@github.com:whatever/gitweb.git gitweb-latest:master
254
255       (We use 0a8f4f0^.. because that means "all the changes from 0a8f4f0 to
256       the current version, including 0a8f4f0 itself.")
257
258       If gitweb had originally been merged using git subtree add (or a
259       previous split had already been done with --rejoin specified) then you
260       can do all your splits without having to remember any weird commit ids:
261
262           $ git subtree split --prefix=gitweb --annotate='(split) ' --rejoin \
263                   --branch gitweb-latest2
264
265       And you can merge changes back in from the upstream project just as
266       easily:
267
268           $ git subtree pull --prefix=gitweb \
269                   git@github.com:whatever/gitweb.git master
270
271       Or, using --squash, you can actually rewind to an earlier version of
272       gitweb:
273
274           $ git subtree merge --prefix=gitweb --squash gitweb-latest~10
275
276       Then make some changes:
277
278           $ date >gitweb/myfile
279           $ git add gitweb/myfile
280           $ git commit -m 'created myfile'
281
282       And fast forward again:
283
284           $ git subtree merge --prefix=gitweb --squash gitweb-latest
285
286       And notice that your change is still intact:
287
288           $ ls -l gitweb/myfile
289
290       And you can split it out and look at your changes versus the standard
291       gitweb:
292
293           git log gitweb-latest..$(git subtree split --prefix=gitweb)
294

EXAMPLE 3. EXTRACT A SUBTREE USING BRANCH

296       Suppose you have a source directory with many files and subdirectories,
297       and you want to extract the lib directory to its own git project.
298       Here’s a short way to do it:
299
300       First, make the new repository wherever you want:
301
302           $ <go to the new location>
303           $ git init --bare
304
305       Back in your original directory:
306
307           $ git subtree split --prefix=lib --annotate="(split)" -b split
308
309       Then push the new branch onto the new empty repository:
310
311           $ git push <new-repo> split:master
312

AUTHOR

314       Written by Avery Pennarun <apenwarr@gmail.com[1]>
315

GIT

317       Part of the git(1) suite
318

NOTES

320        1. apenwarr@gmail.com
321           mailto:apenwarr@gmail.com
322
323
324
325Git 2.24.1                        12/10/2019                    GIT-SUBTREE(1)
Impressum