Merge branch 'stable'
[stgit] / Documentation / tutorial.txt
... / ...
CommitLineData
1StGIT Tutorial
2##############
3
4
5StGIT is a Python application that provides functionality similar to
6quilt (i.e. pushing/popping patches to/from a stack) using GIT instead
7of 'diff' and 'patch'. StGIT stores its patches in a GIT repository as
8normal GIT commit objects.
9StGIT is not an SCM interface on top of GIT. For standard SCM
10operations, either use GIT's porcelain commands or the Cogito tool.
11StGIT is available for download at http://www.procode.org/stgit/ .
12This tutorial assumes you are already familiar with GIT. For more
13information on GIT, see the GIT_tutorial or git(7) .
14
15
16Basic Operation
17===============
18
19Help
20----
21
22For a full list of StGIT commands:
23
24 stg help
25
26For help on individual subcommands:
27
28 stg <cmd> (-h | --help)
29
30
31Repository initialisation
32-------------------------
33
34In stand-alone mode, StGIT is used in conjunction with a GIT repository
35that is already initialised (using 'git-init-db'). StGIT cannot be used
36outside of a GIT repository.
37Any branch in a GIT repository may be managed by StGIT. Each branch
38managed by StGIT contains an independent series of StGIT patches.
39To initialise an existing GIT branch to be managed by StGIT, cd into the
40top of your GIT repository, check out the branch you'd like to manage
41with StGIT, and type:
42
43 stg init
44
45Run the 'stg init' command for any pre-existing GIT branches intended to
46be used with StGIT.
47You can switch between GIT branches with:
48
49 stg branch [<branch name>]
50
51This checks out the named branch and places you at the topmost applied
52StGIT patch in that branch.
53Alternately, you can create branches using only StGIT commands, which
54will automatically prepare them for use with StGIT:
55
56 stg branch --create <new branch>
57
58
59Working with remote repositories
60--------------------------------
61
62With a single command, StGIT can create and initialize a GIT repository
63which mirrors a remote GIT repository. This is known as cloning. All GIT
64transports are supported.
65To clone a repository, use:
66
67 stg clone <repository> <local-dir>
68
69This creates a fresh local repository, initialises a GIT database in it,
70pulls the latest version of the remote, and creates and initialises a
71'master' branch for use with StGIT.
72At any time you can pull the latest changes from the remote repository.
73By default, StGIT pulls from the location stored in .git/branches/
74origin, and updates the base of the current branch.
75To pull the latest changes from a remote repository, use:
76
77 stg pull [<branch> or 'origin']
78
79This command removes all applied StGIT patches from the current branch,
80updates the branch's base commit, then attempts to re-apply the patches.
81Any merge conflicts will halt this process, allowing you to clean up the
82conflicts and continue (see below).
83If the maintainer of the remote repository includes one of your patches
84in the published repository that you pull from, StGIT can usually
85recognize that an incoming patch from the remote matches one of yours,
86and it turns your local version into an empty patch.
87To automatically delete empty patches after a pull, use:
88
89 stg clean
90
91As a convention, you should avoid working in the 'master' branch and use
92it only as a reference, since it reflects someone else's work. If you
93decide to publish your GIT repository, you'll want your own work
94separated into its own branch to make it convenient for others to pull
95just your patches.
96
97Getting started: creating a patch
98---------------------------------
99
100Changes to your working directory are saved in a patch. An StGIT patch
101is simply a saved set of modifications to your working directory, plus a
102saved description. To create an empty StGIT patch in the current branch:
103
104 stg new <name>
105
106To save the changes you've made (that is, to refresh a patch), use:
107
108 stg refresh
109
110To discard changes in your working directory, use:
111
112 git checkout -f
113
114This restores your working directory to the state it was in the last
115time the patch was refreshed.
116Modified files that haven't been saved via a refresh operation can be
117viewed with:
118
119 stg status
120
121You can view modified files that have already been saved into a patch:
122
123 stg files
124
125The 'stg refresh' command automatically notes changes to files that
126already exist in the working directory (it also notices if you remove
127them), but you have to tell StGIT explicitly if you add or rename a
128file:
129
130 git add new-file
131
132to add a file, and
133
134 mv old-file new-file
135 git add new-file
136
137or simply
138
139 git mv old-file new-file
140
141to move a file.
142
143
144Stack manipulation: managing multiple patches
145---------------------------------------------
146
147StGIT can manage more than one patch at a time. A series of StGIT
148patches in a GIT branch are known collectively as a stack. The new patch
149you created above is now the topmost patch in your stack. You can always
150see the name of the topmost (current) patch with:
151
152 stg top
153
154The topmost patch is used as the default patch for most StGIT
155operations. It is the default target of the 'stg refresh' command, for
156example.
157Patches that are pushed onto the stack are referred to as applied, and
158patches that are popped off the stack are referred to as unapplied.
159To push/pop a patch to/from a stack:
160
161 stg push [--all | <name>]
162 stg pop [--all]
163
164The last patch you pushed is the topmost patch. This patch is always in
165the applied list; StGIT can't operate on an unapplied patch unless you
166apply it first.
167You can display the order of patches in a stack with one of these
168commands:
169
170 stg series
171 stg applied
172 stg unapplied
173
174By default the 'stg push' command applies the first patch in the
175unapplied list, but you can push any patch in the unapplied list by
176giving the name of the patch. This is useful if you want to reorder the
177patches in a stack.
178During a push operation, merge conflicts can occur (especially if you
179are changing the order of the patches in your stack). If the push causes
180merge conflicts, they need to be fixed and 'stg resolved' run (see
181below). A 'push' operation can also be reverted with 'stg push --undo'.
182A few more stack basics; to rename a patch:
183
184 stg rename <old-name> <new-name>
185
186To delete a patch:
187
188 stg delete <name>
189
190This permanently discards the named patch. In other words, the patch no
191longer appears in either the applied or unapplied lists, and cannot be
192reapplied to the series.
193You may want to make patches in your stack a permanent part of your GIT
194repository, for example if you are publishing your repository to others.
195To do this, use:
196
197 stg commit
198
199This merges all applied patches in your patch series into the GIT
200repository and removes them from your stack. Use this command only if
201you want to permanently store the applied patches and no longer manage
202them with StGIT.
203
204Converting between StGIT patches and text diffs
205-----------------------------------------------
206
207As mentioned in the introduction, StGIT stores modifications to your
208working tree in the form of GIT commits. This means if you want to apply
209your changes to a tree not managed by GIT, or send your changes to
210someone else in e-mail, you need to convert your StGIT patches into
211normal textual diffs that can be applied with the GNU 'patch' command.
212The 'stg diff' command is a powerful way to generate and view textual
213diffs of patches managed by StGIT.
214To view a diff of the topmost patch:
215
216 stg diff -r /
217
218Observe that this does not show any changes in the working directory
219that have not been saved by a 'refresh'. To view just the changes you've
220made since the last refresh, use:
221
222 stg diff -r /top
223
224If you want to see the changes made by the patch combined with any
225unsaved changes in the working directory, try:
226
227 stg diff -r /bottom
228
229You can also show the changes to any patch in your stack with:
230
231 stg diff -r <patch>/
232
233Use this command to view all the changes in your stack up through the
234current patch:
235
236 stg diff -r base
237
238The 'stg diff' command supports a number of other features that are very
239useful. Be sure to take a look at the help information for this command.
240To convert your StGIT patches into patch files:
241
242 stg export [--range=[<patch1>[:<patch2>]]] [<dir-name>]
243
244The 'export' command supports options to automatically number the
245patches (-n) or add the '.diff' extension (-d). If you don't tell "stg
246export" where to put the patches, it will create directory named "patch-
247branchname" in your current directory, and store the patches there.
248To e-mail a patch or range of patches:
249
250 stg mail [--to=...] (--all | --range=[<patch1>[:<patch2>]] | <patch>)
251
252"stg mail" has a lot of options, so read the output of "stg mail -h" for
253more information.
254You can also import an existing GNU diff patch file as a new StGIT patch
255with a single command. "stg import" will automatically parse through the
256patch file and extract a patch description. Use:
257
258 stg import [<file>]
259
260This is the equivalent of "stg new" followed by "patch -i <file>", then
261"stg refresh -e".
262Sometimes the patch file won't apply cleanly. In that case, "stg import"
263will leave you with an empty StGIT patch, to which you then apply the
264patch file by hand using "patch -i" and your favorite editor.
265To merge a GNU diff file (defaulting to the standard input) into the
266topmost patch:
267
268 stg fold [<file>]
269
270This command supports a '--threeway' option which applies the patch onto
271the bottom of the topmost one and performs a three-way merge.
272
273
274Advanced Usage
275==============
276
277Handling merge conflicts
278------------------------
279
280Pushing a patch on the stack can fail if the patch cannot be applied
281cleanly. This usually happens if there are overlapping changes in the
282tree, the patch depends on another patch which is not applied, or if a
283patch was not merged upstream in the exact form it was sent.
284The 'push' operation stops after the first patch with conflicts. The
285'status' command shows the conflict files by marking them with a 'C'. If
286the 'keeporig' options is set to 'yes' (the default), the original files
287involved in the merge operations are left in the tree as <file>.older,
288<file>.local and <file>.remote for better analysis of the conflict. If
289'diff3' is used as the merger (the default), markers are added to the
290conflicted files as well.
291Run the 'resolved' command to mark the conflicts resolved and remove the
292temporary merge files from the working tree. Then run the 'refresh'
293command to update the StGIT patch with the modifications you made to
294resolve the conflict.
295
296
297Configuration file
298------------------
299
300StGIT tries to read the configuration options from the following files:
301/etc/stgitrc, ~/.stgitrc and .git/stgitrc. The latter overrides the
302options in the former files. If no file is found, the defaults are used.
303An example configuration file with options description can be found in
304the examples/ directory. Most users would probably only define the
305'smtpserver' option used by the 'mail' command.
306The gitmergeonefile.py script does the three-way merging on individual
307files using the tool specified by the 'merger' option. The user can
308specify a smarter tool to be used.
309
310
311Templates
312---------
313
314The 'export' and 'mail' commands use templates for generating the patch
315files or e-mails. The default templates are installed under <prefix>/
316share/stgit/templates/ and, combined with the extra options available
317for the commands, should be enough for most users. The template format
318uses the standard Python string formatting rules. The variables
319available are shown in the the help message for the commands.
320The 'mail' command can also send an initial e-mail for which there is no
321default template. The <prefix>/share/stgit/examples/firstmail.tmpl file
322can be used as an example.
323A default description for new patches can be defined in the .git/
324patchdescr.tmpl file. This is useful for things like signed-off-by
325lines.
326
327
328Merging two patches into one
329----------------------------
330
331There is no command to do this directly at the moment but one can export
332the patch to be merged and use the 'stg fold' command on the generated
333diff file. Assuming that the merged patch was not already applied, the
334operation will succeed. Pushing the merged patch onto the stack will
335result in an empty patch (StGIT notifying the user) that can be safely
336deleted.
337
338
339Technical Information
340=====================
341
342A bit of StGIT patch theory
343---------------------------
344
345We assume that a patch is a diff between two nodes - bottom and top. A
346node is a commit SHA1 id or tree SHA1 id in the GIT terminology:
347
348 P - patch
349 N - node
350
351 P = diff(Nt, Nb)
352
353 Nb - bottom (start) node
354 Nt - top (end) node
355 Nf - first node (for log generation)
356
357For an ordered stack of patches:
358
359 P1 = diff(N1, N0)
360 P2 = diff(N2, N1)
361 ...
362
363
364 Ps = P1 + P2 + P3 + ... = diff(Nst, Nsb)
365
366 Ps - the big patch of the whole stack
367 Nsb - bottom stack node (= N0)
368 Nst - top stack node (= Nn)
369
370Applying (pushing) a patch on the stack (Nst can differ from Nb) is done
371by diff3 merging. The new patch becomes:
372
373 P' = diff(Nt', Nb')
374 Nb' = Nst
375 Nt' = diff3(Nst, Nb, Nt)
376
377(note that the diff3 parameters order is: branch1, ancestor, branch2)
378The above operation allows easy patch re-ordering.
379Removing (popping) a patch from the stack is done by simply setting the
380Nst to Nb.
381
382
383Layout of the .git directory
384----------------------------
385
386 HEAD -> refs/heads/<something>
387 objects/
388 ??/
389 ...
390 refs/
391 heads/
392 master - the master commit id
393 ...
394 tags/
395 ...
396 branches/
397 ...
398 patches/
399 master/
400 applied - list of applied patches
401 unapplied - list of not-yet applied patches
402 current - name of the topmost patch
403 patch1/
404 bottom - the bottom id of the patch
405 top - the top id of the patch
406 description - the patch description
407 authname - author's name
408 authemail - author's e-mail
409 commname - committer's name
410 commemail - committer's e-mail
411 patch2/
412 ...
413 ...
414 ...