Tutorial: Talk about conflicts when introducing StGit
[stgit] / Documentation / tutorial.txt
CommitLineData
ce0a1f86 1StGit tutorial
4625b604 2##############
d1c8fcd7 3
ce0a1f86
KH
4StGit is a command-line application that provides functionality
5similar to htmllink:http://savannah.nongnu.org/projects/quilt/[Quilt]
6(i.e. pushing/popping patches to/from a stack), but using Git instead
7of +diff+ and +patch+. StGit stores its patches in a Git repository as
8normal Git commits, and provides a number of commands to manipulate
9them in various ways.
d1c8fcd7 10
ce0a1f86
KH
11This tutorial assumes you are already familiar with the basics of Git
12(for example, branches, commits, and conflicts). For more information
13on Git, see manlink:git[1] or htmllink:http://git.or.cz/[the Git home
14page].
d1c8fcd7 15
d1c8fcd7 16
ce0a1f86
KH
17Help
18====
19
20For a full list of StGit commands:
21
22 $ stg help
23
24For quick help on individual subcommands:
25
26 $ stg help <cmd>
27
28For more extensive help on a subcommand:
29
30 $ man stg-<cmd>
31
32(The documentation is also available in htmllink:stg.html[HTML
33format].)
34
35
36Getting started
c2428e5a 37===============
d1c8fcd7 38
ce0a1f86
KH
39StGit is not a stand-alone program -- it operates on a Git repository
40that you have already created, using +git init+ or +git clone+. So get
41one of those; if you don't have one at hand, try for example
42
43 $ git clone http://homepage.ntlworld.com/cmarinas/stgit.git
44 $ cd stgit
d1c8fcd7 45
ce0a1f86 46Before you can create StGit patches, you have to run stglink:init[]:
d1c8fcd7 47
ce0a1f86 48 $ stg init
d1c8fcd7 49
ce0a1f86
KH
50This initializes the StGit metadata for the current branch. (So if you
51want to have StGit patches in another branch too, you need to run +stg
52init+ again in that branch.)
d1c8fcd7 53
ce0a1f86
KH
54NOTE: As a shortcut, stglink:clone[] will run +git clone+ followed by
55+stg init+ for you.
d1c8fcd7 56
d1c8fcd7 57
ce0a1f86
KH
58Creating a patch
59----------------
d1c8fcd7 60
ce0a1f86 61Now we're ready to create our first patch:
d1c8fcd7 62
ce0a1f86 63 $ stg new my-first-patch
d1c8fcd7 64
ce0a1f86
KH
65This will create a patch called +my-first-patch+, and open an editor
66to let you edit the patch's commit message. (If you don't give a name
67on the command line, StGit will make one up based on the first line of
68the commit message.) This patch is empty, as stglink:show[] will tell
69you:
c2428e5a 70
ce0a1f86 71 $ stg show
c2428e5a 72
ce0a1f86
KH
73But it won't stay that way for long! Open one of the files in your
74favorite text editor, change something, and save. You now have some
75local changes in your tree:
c2428e5a 76
ce0a1f86
KH
77 $ stg status
78 M stgit/main.py
c2428e5a 79
ce0a1f86 80Then stgsublink:refresh[] the patch:
c2428e5a 81
ce0a1f86 82 $ stg refresh
d1c8fcd7 83
ce0a1f86 84And voilà -- the patch is no longer empty:
c2428e5a 85
ce0a1f86
KH
86 $ stg show
87 commit 3de32068c600d40d8af2a9cf1f1c762570ae9610
88 Author: Audrey U. Thor <author@example.com>
89 Date: Sat Oct 4 16:10:54 2008 +0200
c2428e5a 90
ce0a1f86 91 Tell the world that I've made a patch
d1c8fcd7 92
ce0a1f86
KH
93 diff --git a/stgit/main.py b/stgit/main.py
94 index e324179..6398958 100644
95 --- a/stgit/main.py
96 +++ b/stgit/main.py
97 @@ -171,6 +171,7 @@ def _main():
98 sys.exit(ret or utils.STGIT_SUCCESS)
d1c8fcd7 99
ce0a1f86
KH
100 def main():
101 + print 'My first patch!'
102 try:
103 _main()
104 finally:
d1c8fcd7 105
ce0a1f86
KH
106(I'm assuming you're already familiar with patches like this from Git,
107but it's really quite simple; in this example, I've added the +$$print
108'My first patch!'$$+ line to the file +stgit/main.py+, at around line
109171.)
c2428e5a 110
ce0a1f86
KH
111Since the patch is also a regular Git commit, you can also look at it
112with regular Git tools such as manlink:gitk[].
d1c8fcd7 113
ce0a1f86
KH
114Creating another patch
115----------------------
c2428e5a 116
ce0a1f86
KH
117We want to make another improvement, so let's create a new patch for
118it:
d1c8fcd7 119
ce0a1f86
KH
120 $ echo 'Audrey U. Thor' > AUTHORS
121 $ stg new credit --message 'Give me some credit'
122 $ stg refresh
d1c8fcd7 123
ce0a1f86
KH
124Note that we can give the commit message on the command line, and that
125it doesn't matter whether we run stglink:new[] before or after we edit
126the files.
d1c8fcd7 127
ce0a1f86 128So now we have two patches:
d1c8fcd7 129
ce0a1f86
KH
130 $ stg series --description
131 + my-first-patch # This is my first patch
132 > credit # Give me some credit
d1c8fcd7 133
ce0a1f86
KH
134stglink:series[] lists the patches from bottom to top; +$$+$$+ means
135that a patch is 'applied', and +>+ that it is the 'current', or
136topmost, patch.
d1c8fcd7 137
ce0a1f86
KH
138If we want to make further changes to the topmost patch, we just edit
139the files and run +stg refresh+. But what if we wanted to change
140+my-first-patch+? The simplest way is to stgsublink:pop[] the +credit+
141patch, so that +my-first-patch+ becomes topmost again:
d1c8fcd7 142
ce0a1f86
KH
143 $ stg pop credit
144 Checking for changes in the working directory ... done
145 Popping patch "credit" ... done
146 Now at patch "my-first-patch"
147 $ stg series --description
148 > my-first-patch # This is my first patch
149 - credit # Give me some credit
d1c8fcd7 150
ce0a1f86
KH
151stglink:series[] now shows that +my-first-patch+ is topmost again,
152which means that stglink:refresh[] will update it with any changes we
153make.
d1c8fcd7 154
ce0a1f86
KH
155The minus sign says that +credit+ is 'unapplied' -- this means that
156it's been temporarily put aside. If you look at the +AUTHORS+ file,
157you'll see that our change to it is gone; and tools such as
158manlink:gitk[] will not show it, because it's been edited out of the
159Git history. But it's just one stglink:push[] command away from being
160restored:
d1c8fcd7 161
ce0a1f86
KH
162 $ stg push credit
163 Checking for changes in the working directory ... done
164 Fast-forwarded patch "credit"
165 Now at patch "credit"
d1c8fcd7 166
ce0a1f86
KH
167NOTE: You can omit the patch name argument to stglink:push[] and
168stglink:pop[]. If you do, you will push the next unapplied patch, and
169pop the topmost patch, respectively.
c2428e5a 170
ce0a1f86
KH
171NOTE: There are at least two more ways to update a non-topmost patch.
172One is to use stglink:refresh[] with the +$$--patch$$+ flag, the other
173to create a new patch for the update and then merge it into the other
174patch with stglink:coalesce[].
c2428e5a 175
18eec969 176
ce0a1f86
KH
177Keeping commit messages up to date
178----------------------------------
18eec969 179
ce0a1f86
KH
180Since StGit is all about creating readable Git history (or a readable
181patch series, which is essentially the same thing), one thing you'll
182want to pay attention to is the commit messages of your patches.
183stglink:new[] asks you for a commit message when you create a new
184patch, but as time goes by and you refresh the patch again and again,
185chances are that the original commit message isn't quite correct
186anymore. Fortunately, editing the commit message is very easy:
187
188 $ stg edit <patch-name>
189
190In addition to stglink:edit[], you can also give the +$$--edit$$+ flag
191to stglink:refresh[] -- that way, you get to change the commit message
192and update the patch at the same time. Use whichever feels most
193natural to you.
194
195NOTE: stglink:edit[] has a +$$--diff$$+ flag, which gives you the diff
196text and not just the commit message in your editor. Be aware, though,
197that if you change the diff so that it no longer applies, the edit
198will be saved to a file instead of being carried out. If you're not
199comfortable editing diffs, just treat +$$--diff$$+ as a way to get to
200'see' the diff while you edit the commit message.
201
202If the patch changes considerably, it might even deserve a new name.
203stglink:rename[] is your friend there.
18eec969 204
c2428e5a 205
ce0a1f86
KH
206Conflicts
207---------
208
209Normally, when you pop a patch, change something, and then later push
210it again, StGit sorts out everything for you automatically. For
211example, let's create two patches that modify different files:
212
213 $ stg clone http://homepage.ntlworld.com/cmarinas/stgit.git stgit
214 $ cd stgit
215 $ stg new first --message 'First patch'
216 $ echo '- Do something' >> TODO
217 $ stg refresh
218 $ stg new second --message 'Second patch'
219 $ echo '- Install something' >> INSTALL
220 $ stg refresh
221
222then pop them both:
223
224 $ stg pop --all
225
226and then push them in the opposite order:
227
228 $ stg push second first
229 $ stg series
230 + second
231 > first
232
233StGit had no problems reordering these patches for us, since they
234didn't touch the same file. But it would have worked just fine even if
235they had touched the same file, as long as they didn't change the same
236part of the file. But what if they did? Let's find out.
237
238 $ stg pop
239 Checking for changes in the working directory ... done
240 Popping patch "first" ... done
241 Now at patch "second"
242 $ echo '- Do something else' >> TODO
243 $ stg refresh
244
245Now, both patches add a new line at the end of +TODO+. So what happens
246when we try to have them both applied?
247
248 $ stg push
249 Pushing patch "first" ...
250 CONFLICT (content): Merge conflict in TODO
251 Error: The merge failed during "push".
252 Revert the operation with "stg undo".
253 stg push: 1 conflict(s)
254
255StGit is telling us that it couldn't figure out how to push +first+ on
256top of +second+, now that they both modify +TODO+. We can take a look
257at the situation with stglink:status[]:
258
259 $ stg status
260 ? TODO.ancestor
261 ? TODO.current
262 ? TODO.patched
263 C TODO
264
265As we were told by stglink:push[], the conflict is in the file +TODO+.
266(If the patch was bigger and touched multiple files, they would all be
267listed here; prefixed with +C+ if they had conflicts, and +M+ if StGit
268managed to automatically resolve everything in the file.)
269
270NOTE: +TODO.ancestor+, +TODO.current+, and +TODO.patched+ are the
271three versions of the file that StGit tried to merge. The +.current+
272file is the version before the patch was applied, +.patched+ is the
273version in the patch we tried to push, and +.ancestor+ the version
274that contains neither of the added lines.
275
276At this point, we have two options:
277
278 1. Undo the failed merge with stglink:undo[]. (Remember to use the
279 +$$--hard$$+ flag, since the unresolved conflict means the
280 worktree is not clean.)
281
282 2. Manually resolve the conflict.
283
284To resolve the conflict, open +TODO+ in your favorite editor. It ends
285like this:
286
287----------------------------------------------------------------------
288- numeric shortcuts for naming patches near top (eg. +1, -2)
289- (config?) parameter for number of patches included by "series -s"
290<<<<<<< current:TODO
291- Do something else
292=======
293- Do something
294>>>>>>> patched:TODO
295----------------------------------------------------------------------
296
297The 'conflict markers' +<<<<<<<+, +=======+, and +>>>>>>>+ indicate
298which lines were already there (+current+) and which were added by the
299patch (+patched+). Edit the file so that it looks like it should; in
300this case, we want something like this:
301
302----------------------------------------------------------------------
303- numeric shortcuts for naming patches near top (eg. +1, -2)
304- (config?) parameter for number of patches included by "series -s"
305- Do something
306- Do something else
307----------------------------------------------------------------------
308
309Note that ``looks like it should'' includes removing the conflict
310markers.
311
312Now that we've resolved the conflict, we just need to tell StGit about
313it:
314
315 $ stg resolved TODO
316 $ stg status
317 M TODO
318
319+TODO+ is listed as being modified, not in conflict. And we know from
320before how to deal with modified files:
321
322 $ stg refresh
323
324The conflict is now resolved. We can see that +first+ now looks a
325little different; it no longer adds a line at the end of the file:
326
327 $ stg show
328 commit 8e3ae5f6fa6e9a5f831353524da5e0b91727338e
329 Author: Audrey U. Thor <author@example.com>
330 Date: Sun Oct 5 14:43:42 2008 +0200
331
332 First patch
333
334 diff --git a/TODO b/TODO
335 index 812d236..4ef3841 100644
336 --- a/TODO
337 +++ b/TODO
338 @@ -24,4 +24,5 @@ The future, when time allows or if someone else does them:
339 they have scripts for moving the changes in one to the others)
340 - numeric shortcuts for naming patches near top (eg. +1, -2)
341 - (config?) parameter for number of patches included by "series -s"
342 +- Do something
343 - Do something else
344
345
346Workflow: Development branch
347============================
348
349One common use of StGit is to ``polish'' a Git branch before you
350publish it for others to see. Such history falsification can often be
351a 'good' thing -- when you (or someone else) needs to look at what you
352did six months later, you are not really interested in all the false
353starts and the steps needed to corect them. What you want is the final
354solution, presented in a way that makes it easy to read and
355understand.
356
357Of course, there are limits. Editing the last few days' worth of
358history is probably a good idea; editing the last few months' probably
359isn't. A rule of thumb might be to not mess with history old enough
360that you don't remember the details anymore. And rewriting history
361that you have published for others to see (and base their own work on)
362usually just makes everyone more confused, not less.
363
364So, let's take a concrete example. Say that you're hacking on StGit,
365and have made several Git commits as your work progressed, with commit
366messages such as ``Improve the snarfle cache'', ``Remove debug
367printout'', ``New snarfle cache test'', ``Oops, spell function name
368correctly'', ``Fix documentation error'', and ``More snarfle cache''.
369
370Now, this is the actual history, but for obvious reasons, this isn't
371the kind of history you'd ideally want to find when you six months
372from now try to figure out exactly where that elusive snarfle cache
373bug was introduced. So let's turn this into the history we can be
374proud of. The first step is to make StGit patches out of all those Git
375commits:
376
377 $ stg uncommit --number 6
378 Uncommitting 6 patches ...
379 Now at patch "more-snarfle-cache"
380 done
381 $ stg series --description
382 + improve-the-snarfle-cache # Improve the snarfle cache
383 + remove-debug-printout # Remove debug printout
384 + new-snarfle-cache-test # New snarfle cache test
385 + oops-spell-function-name-corre # Oops, spell function name correctly
386 + fix-documentation-error # Fix documentation error
387 > more-snarfle-cache # More snarfle cache
388
389As you can see, stglink:uncommit[] adds StGit metadata to the last few
390Git commits, turning them into StGit patches so that we can do stuff
391with them.
392
393NOTE: With the +$$--number$$+ flag, stglink:uncommit[] uncommits that
394many commits and generates names for them based on their commit
395messages. If you like, you can instead list the patch names you want
396on the command line.
397
398At this point, there are a number of things we could do:
399
400 * Continue developing, and take advantage of e.g. stglink:goto[] or
401 +stg refresh $$--patch$$+ to stick updates in the right patch to
402 begin with.
403
404 * Use e.g. stglink:float[], stglink:sink[], stglink:push[], and
405 stglink:pop[] to reorder patches.
406
407 * Use stglink:coalesce[] to merge two or more patches into one.
408 stgsublink:coalesce[] pushes and pops so that the patches to be
409 merged are consecutive and unrelated patches aren't in the way,
410 then makes one big patch out of the patches to be merged, and
411 finally pushes the other patches back.
412+
413Of course, as always when there is pushing involved, there is the
414possibility of conflicts. If a push results in a conflict, the
415operation will be halted, and we'll be given the option of either
416resolving the conflict or undoing.
417
418Once we feel that the history is as good as it's going to get, we can
419remove the StGit metadata, turning the patches back into regular Git
420commits again:
421
422 $ stg commit --all
423
424TIP: stglink:commit[] can also commit specific patches (named on the
425command line), leaving the rest alone. This can be used to retire
426patches as they mature, while keeping the newer and more volatile
427patches as patches.
428
429
430Workflow: Tracking branch
431=========================
432
433
434Rebasing a patch series
435-----------------------
436
437TODO:: rebase, ...
438
439
440Getting patches upstream
441------------------------
442
443TODO:: export, mail, ...
c2428e5a 444
c2428e5a 445
ce0a1f86
KH
446Importing patches
447-----------------
c2428e5a 448
ce0a1f86 449TODO:: import, ...
c2428e5a 450
c2428e5a 451
ce0a1f86
KH
452Other stuff that needs to be placed somewhere
453=============================================
454
455
456Undo
457----
d1c8fcd7 458
ce0a1f86 459TODO:: undo, redo, log, reset
d1c8fcd7 460
d1c8fcd7 461
ce0a1f86
KH
462Interoperating with Git
463-----------------------
d1c8fcd7 464
ce0a1f86 465TODO::
d1c8fcd7 466
ce0a1f86 467* git commit + repair
d1c8fcd7 468
ce0a1f86 469* git reset HEAD~n + repair
d1c8fcd7 470
ce0a1f86 471* don't do git rebase or git merge, because it won't work
d1c8fcd7 472
d1c8fcd7 473
ce0a1f86
KH
474Patch stuff
475-----------
d1c8fcd7 476
ce0a1f86
KH
477TODO:: This section needs revising. I've only fixed the formatting.
478Most of it should go under "Workflow: Tracking branch"
479
480As mentioned in the introduction, StGit stores modifications to your
481working tree in the form of Git commits. This means if you want to
482apply your changes to a tree not managed by Git, or send your changes
483to someone else in e-mail, you need to convert your StGit patches into
484normal textual diffs that can be applied with the GNU patch command.
485stglink:diff[] is a powerful way to generate and view textual diffs of
486patches managed by StGit.
d1c8fcd7 487
c2428e5a 488To view a diff of the topmost patch:
d1c8fcd7 489
ce0a1f86 490 $ stg diff -r /
d1c8fcd7 491
c2428e5a 492Observe that this does not show any changes in the working directory
ce0a1f86
KH
493that have not been saved by a stgsublink:refresh[]. To view just the
494changes you've made since the last refresh, use:
d1c8fcd7 495
ce0a1f86 496 $ stg diff -r /top
d1c8fcd7 497
c2428e5a
CM
498If you want to see the changes made by the patch combined with any
499unsaved changes in the working directory, try:
d1c8fcd7 500
ce0a1f86 501 $ stg diff -r /bottom
d1c8fcd7 502
c2428e5a 503You can also show the changes to any patch in your stack with:
d1c8fcd7 504
ce0a1f86 505 $ stg diff -r <patch>/
d1c8fcd7 506
c2428e5a
CM
507Use this command to view all the changes in your stack up through the
508current patch:
509
ce0a1f86 510 $ stg diff -r base
d1c8fcd7 511
ce0a1f86
KH
512stglink:diff[] supports a number of other features that are very
513useful. Be sure to take a look at the help information for this
514command. To convert your StGit patches into patch files:
d1c8fcd7 515
ce0a1f86 516 $ stg export [--range=[<patch1>[:<patch2>]]] [<dir-name>]
d1c8fcd7 517
ce0a1f86
KH
518stglink:export[] supports options to automatically number the patches
519(+-n+) or add the +.diff+ extension (+-d+). If you don't tell
520stgsublink:export[] where to put the patches, it will create directory
521named +patch-<branchname>+ in your current directory, and store the
522patches there. To e-mail a patch or range of patches:
c2428e5a 523
ce0a1f86 524 $ stg mail [--to=...] (--all | --range=[<patch1>[:<patch2>]] | <patch>)
c2428e5a 525
ce0a1f86
KH
526stglink:mail[] has a lot of options, so read the output of +stg mail
527-h+ for more information.
c2428e5a 528
ce0a1f86
KH
529You can also import an existing GNU diff patch file as a new StGit
530patch with a single command. stglink:import[] will automatically parse
531through the patch file and extract a patch description. Use:
d1c8fcd7 532
ce0a1f86 533 $ stg import [<file>]
d1c8fcd7 534
ce0a1f86 535This is the equivalent of
d1c8fcd7 536
ce0a1f86
KH
537 $ stg new
538 $ patch -i <file>
539 $ stg refresh -e
d1c8fcd7 540
ce0a1f86
KH
541Sometimes the patch file won't apply cleanly. In that case,
542stglink:import[] will leave you with an empty StGit patch, to which
543you then apply the patch file by hand using "patch -i" and your
544favorite editor.
d1c8fcd7 545
ce0a1f86
KH
546To merge a GNU diff file (defaulting to the standard input) into the
547topmost patch:
d1c8fcd7 548
ce0a1f86 549 $ stg fold [<file>]
c2428e5a 550
ce0a1f86
KH
551This command supports a +$$--threeway$$+ option which applies the
552patch onto the bottom of the topmost one and performs a three-way
553merge.
27373fe0 554
c2428e5a 555
27373fe0
CM
556Templates
557---------
d1c8fcd7 558
ce0a1f86
KH
559TODO:: This section needs revising. I've only fixed the formatting.
560
561stglink:export[] and stglink:mail[] use templates for generating the
562patch files or e-mails. The default templates are installed under
563+<prefix>/share/stgit/templates/+ and, combined with the extra options
564available for these commands, should be enough for most users. The
565template format uses the standard Python string formatting rules. The
566variables available are listed in the the manual pages for each
567command. stglink:mail[] can also send an initial 'cover' e-mail for
568which there is no default template. The
569+<prefix>/share/stgit/examples/firstmail.tmpl+ file can be used as an
570example. A default description for new patches can be defined in the
571+.git/ patchdescr.tmpl+ file. This is useful for things like
572signed-off-by lines.