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