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 [<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>
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

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 <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>
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       split [<local-commit>]
80           Extract a new, synthetic project history from the history of the
81           <prefix> subtree of <local-commit>, or of HEAD if no <local-commit>
82           is given. The new history includes only the commits (including
83           merges) that affected <prefix>, and each of those commits now has
84           the contents of <prefix> at the root of the project instead of in a
85           subdirectory. Thus, the newly created history is suitable for
86           export as a separate git repository.
87
88           After splitting successfully, a single commit ID is printed to
89           stdout. This corresponds to the HEAD of the newly created tree,
90           which you can manipulate however you want.
91
92           Repeated splits of exactly the same history are guaranteed to be
93           identical (i.e. to produce the same commit IDs) as long as the
94           settings passed to split (such as --annotate) are the same. Because
95           of this, if you add new commits and then re-split, the new commits
96           will be attached as commits on top of the history you generated
97           last time, so git merge and friends will work as expected.
98
99       pull <repository> <remote-ref>
100           Exactly like merge, but parallels git pull in that it fetches the
101           given ref from the specified remote repository.
102
103       push <repository> [+][<local-commit>:]<remote-ref>
104           Does a split using the <prefix> subtree of <local-commit> and then
105           does a git push to push the result to the <repository> and
106           <remote-ref>. This can be used to push your subtree to different
107           branches of the remote repository. Just as with split, if no
108           <local-commit> is given, then HEAD is used. The optional leading +
109           is ignored.
110

OPTIONS FOR ALL COMMANDS

112       -q, --quiet
113           Suppress unnecessary output messages on stderr.
114
115       -d, --debug
116           Produce even more unnecessary output messages on stderr.
117
118       -P <prefix>, --prefix=<prefix>
119           Specify the path in the repository to the subtree you want to
120           manipulate. This option is mandatory for all commands.
121

OPTIONS FOR ADD AND MERGE (ALSO: PULL, SPLIT --REJOIN, AND PUSH --REJOIN)

123       These options for add and merge may also be given to pull (which wraps
124       merge), split --rejoin (which wraps either add or merge as
125       appropriate), and push --rejoin (which wraps split --rejoin).
126
127       --squash
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
153       -m <message>, --message=<message>
154           Specify <message> as the commit message for the merge commit.
155

OPTIONS FOR SPLIT (ALSO: PUSH)

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

EXAMPLE 1. ADD COMMAND

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

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

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

EXAMPLE 3. EXTRACT A SUBTREE USING A BRANCH

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

AUTHOR

307       Written by Avery Pennarun <apenwarr@gmail.com[1]>
308

GIT

310       Part of the git(1) suite
311

NOTES

313        1. apenwarr@gmail.com
314           mailto:apenwarr@gmail.com
315
316
317
318Git 2.33.1                        2021-10-12                    GIT-SUBTREE(1)
Impressum