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