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> <refspec>
11       git subtree add   -P <prefix> <repository> <refspec>
12       git subtree pull  -P <prefix> <repository> <refspec...>
13       git subtree push  -P <prefix> <repository> <refspec...>
14       git subtree merge -P <prefix> <commit>
15       git subtree split -P <prefix> [OPTIONS] [<commit>]
16

DESCRIPTION

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 .gitmodule 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

COMMANDS

59       add
60           Create the <prefix> subtree by importing its contents from the
61           given <refspec> or <repository> and remote <refspec>. A new commit
62           is created automatically, joining the imported project’s history
63           with 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
74               always have to be forward; you can use this command to
75               go back in time from v2.5 to v2.4, for example.  If your
76               merge introduces a conflict, you can resolve it in the
77               usual ways.
78
79       pull
80           Exactly like merge, but parallels git pull in that it fetches the
81           given commit 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 refspec. This
86           can be used to push your subtree to different branches of the
87           remote 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
98               printed to stdout.  This corresponds to the HEAD of the
99               newly created tree, which you can manipulate however you
100               want.
101
102               Repeated splits of exactly the same history are
103               guaranteed to be identical (ie. to produce the same
104               commit ids).  Because of this, if you add new commits
105               and then re-split, the new commits will be attached as
106               commits on top of the history you generated last time,
107               so 'git merge' and friends will work as expected.
108
109               Note that if you use '--squash' when you merge, you
110               should usually not just '--rejoin' when you split.
111

OPTIONS

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

OPTIONS FOR ADD, MERGE, PUSH, PULL

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

OPTIONS FOR SPLIT

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

EXAMPLE 1. ADD COMMAND

232       Let’s assume that you have a local repository that you would like to
233       add an external vendor library to. In this case we will add the
234       git-subtree repository as a subdirectory of your already existing
235       git-extensions repository in ~/git-extensions/:
236
237           $ git subtree add --prefix=git-subtree --squash \
238                   git://github.com/apenwarr/git-subtree.git master
239
240       master needs to be a valid remote ref and can be a different branch
241       name
242
243       You can omit the --squash flag, but doing so will increase the number
244       of commits that are incldued in your local repository.
245
246       We now have a ~/git-extensions/git-subtree directory containing code
247       from the master branch of git://github.com/apenwarr/git-subtree.git in
248       our git-extensions repository.
249

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

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

EXAMPLE 3. EXTRACT A SUBTREE USING BRANCH

309       Suppose you have a source directory with many files and subdirectories,
310       and you want to extract the lib directory to its own git project.
311       Here’s a short way to do it:
312
313       First, make the new repository wherever you want:
314
315           $ <go to the new location>
316           $ git init --bare
317
318       Back in your original directory:
319
320           $ git subtree split --prefix=lib --annotate="(split)" -b split
321
322       Then push the new branch onto the new empty repository:
323
324           $ git push <new-repo> split:master
325

AUTHOR

327       Written by Avery Pennarun <apenwarr@gmail.com[1]>
328

GIT

330       Part of the git(1) suite
331

NOTES

333        1. apenwarr@gmail.com
334           mailto:apenwarr@gmail.com
335
336
337
338Git                               11/19/2018                    GIT-SUBTREE(1)
Impressum