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