4 StGit is a command-line application that provides functionality
5 similar to htmllink:http://savannah.nongnu.org/projects/quilt/[Quilt]
6 (i.e. pushing/popping patches to/from a stack), but using Git instead
7 of +diff+ and +patch+. StGit stores its patches in a Git repository as
8 normal Git commits, and provides a number of commands to manipulate
11 This tutorial assumes you are already familiar with the basics of Git
12 (for example, branches, commits, and conflicts). For more information
13 on Git, see manlink:git[1] or htmllink:http://git.or.cz/[the Git home
20 For a full list of StGit commands:
24 For quick help on individual subcommands:
28 For more extensive help on a subcommand:
32 (The documentation is also available in htmllink:stg.html[HTML
39 StGit is not a stand-alone program -- it operates on a Git repository
40 that you have already created, using +git init+ or +git clone+. So get
41 one of those; if you don't have one at hand, try for example
43 $ git clone http://homepage.ntlworld.com/cmarinas/stgit.git
46 Before you can create StGit patches, you have to run stglink:init[]:
50 This initializes the StGit metadata for the current branch. (So if you
51 want to have StGit patches in another branch too, you need to run +stg
52 init+ again in that branch.)
54 NOTE: As a shortcut, stglink:clone[] will run +git clone+ followed by
61 Now we're ready to create our first patch:
63 $ stg new my-first-patch
65 This will create a patch called +my-first-patch+, and open an editor
66 to let you edit the patch's commit message. (If you don't give a name
67 on the command line, StGit will make one up based on the first line of
68 the commit message.) This patch is empty, as stglink:show[] will tell
73 But it won't stay that way for long! Open one of the files in your
74 favorite text editor, change something, and save. You now have some
75 local changes in your tree:
80 Then stgsublink:refresh[] the patch:
84 And voilĂ -- the patch is no longer empty:
87 commit 3de32068c600d40d8af2a9cf1f1c762570ae9610
88 Author: Audrey U. Thor <author@example.com>
89 Date: Sat Oct 4 16:10:54 2008 +0200
91 Tell the world that I've made a patch
93 diff --git a/stgit/main.py b/stgit/main.py
94 index e324179..6398958 100644
97 @@ -171,6 +171,7 @@ def _main():
98 sys.exit(ret or utils.STGIT_SUCCESS)
101 + print 'My first patch!'
106 (I'm assuming you're already familiar with
107 htmllink:http://en.wikipedia.org/wiki/Diff#Unified_format[unified
108 diff] patches like this from Git, but it's really quite simple; in
109 this example, I've added the +$$print 'My first patch!'$$+ line to the
110 file +stgit/main.py+, at around line 171.)
112 Since the patch is also a regular Git commit, you can also look at it
113 with regular Git tools such as manlink:gitk[].
115 Creating another patch
116 ----------------------
118 We want to make another improvement, so let's create a new patch for
121 $ echo 'Audrey U. Thor' > AUTHORS
122 $ stg new credit --message 'Give me some credit'
125 Note that we can give the commit message on the command line, and that
126 it doesn't matter whether we run stglink:new[] before or after we edit
129 So now we have two patches:
131 $ stg series --description
132 + my-first-patch # This is my first patch
133 > credit # Give me some credit
135 stglink:series[] lists the patches from bottom to top; +$$+$$+ means
136 that a patch is 'applied', and +>+ that it is the 'current', or
139 If we want to make further changes to the topmost patch, we just edit
140 the files and run +stg refresh+. But what if we wanted to change
141 +my-first-patch+? The simplest way is to stgsublink:pop[] the +credit+
142 patch, so that +my-first-patch+ becomes topmost again:
145 Checking for changes in the working directory ... done
146 Popping patch "credit" ... done
147 Now at patch "my-first-patch"
148 $ stg series --description
149 > my-first-patch # This is my first patch
150 - credit # Give me some credit
152 stglink:series[] now shows that +my-first-patch+ is topmost again,
153 which means that stglink:refresh[] will update it with any changes we
156 The minus sign says that +credit+ is 'unapplied' -- this means that
157 it's been temporarily put aside. If you look at the +AUTHORS+ file,
158 you'll see that our change to it is gone; and tools such as
159 manlink:gitk[] will not show it, because it's been edited out of the
160 Git history. But it's just one stglink:push[] command away from being
164 Checking for changes in the working directory ... done
165 Fast-forwarded patch "credit"
166 Now at patch "credit"
168 NOTE: You can omit the patch name argument to stglink:push[] and
169 stglink:pop[]. If you do, you will push the next unapplied patch, and
170 pop the topmost patch, respectively.
172 NOTE: There are at least two more ways to update a non-topmost patch.
173 One is to use stglink:refresh[] with the +$$--patch$$+ flag, the other
174 to create a new patch for the update and then merge it into the other
175 patch with stglink:coalesce[].
178 Keeping commit messages up to date
179 ----------------------------------
181 Since StGit is all about creating readable Git history (or a readable
182 patch series, which is essentially the same thing), one thing you'll
183 want to pay attention to is the commit messages of your patches.
184 stglink:new[] asks you for a commit message when you create a new
185 patch, but as time goes by and you refresh the patch again and again,
186 chances are that the original commit message isn't quite correct
187 anymore. Fortunately, editing the commit message is very easy:
189 $ stg edit <patch-name>
191 In addition to stglink:edit[], you can also give the +$$--edit$$+ flag
192 to stglink:refresh[] -- that way, you get to change the commit message
193 and update the patch at the same time. Use whichever feels most
196 NOTE: stglink:edit[] has a +$$--diff$$+ flag, which gives you the diff
197 text and not just the commit message in your editor. Be aware, though,
198 that if you change the diff so that it no longer applies, the edit
199 will be saved to a file instead of being carried out. If you're not
200 comfortable editing diffs, just treat +$$--diff$$+ as a way to get to
201 'see' the diff while you edit the commit message.
203 If the patch changes considerably, it might even deserve a new name.
204 stglink:rename[] is your friend there.
210 Normally, when you pop a patch, change something, and then later push
211 it again, StGit sorts out everything for you automatically. For
212 example, let's create two patches that modify different files:
214 $ stg clone http://homepage.ntlworld.com/cmarinas/stgit.git stgit
216 $ stg new first --message 'First patch'
217 $ echo '- Do something' >> TODO
219 $ stg new second --message 'Second patch'
220 $ echo '- Install something' >> INSTALL
227 and then push them in the opposite order:
229 $ stg push second first
234 StGit had no problems reordering these patches for us, since they
235 didn't touch the same file. But it would have worked just fine even if
236 they had touched the same file, as long as they didn't change the same
237 part of the file. But what if they did? Let's find out.
240 Checking for changes in the working directory ... done
241 Popping patch "first" ... done
242 Now at patch "second"
243 $ echo '- Do something else' >> TODO
246 Now, both patches add a new line at the end of +TODO+. So what happens
247 when we try to have them both applied?
250 Pushing patch "first" ...
251 CONFLICT (content): Merge conflict in TODO
252 Error: The merge failed during "push".
253 Revert the operation with "stg undo".
254 stg push: 1 conflict(s)
256 StGit is telling us that it couldn't figure out how to push +first+ on
257 top of +second+, now that they both modify +TODO+. We can take a look
258 at the situation with stglink:status[]:
266 As we were told by stglink:push[], the conflict is in the file +TODO+.
267 (If the patch was bigger and touched multiple files, they would all be
268 listed here; prefixed with +C+ if they had conflicts, and +M+ if StGit
269 managed to automatically resolve everything in the file.)
271 NOTE: +TODO.ancestor+, +TODO.current+, and +TODO.patched+ are the
272 three versions of the file that StGit tried to merge. The +.current+
273 file is the version before the patch was applied, +.patched+ is the
274 version in the patch we tried to push, and +.ancestor+ the version
275 that contains neither of the added lines.
277 At this point, we have two options:
279 1. Undo the failed merge with stglink:undo[]. (Remember to use the
280 +$$--hard$$+ flag, since the unresolved conflict means the
281 worktree is not clean.)
283 2. Manually resolve the conflict.
285 To resolve the conflict, open +TODO+ in your favorite editor. It ends
288 ----------------------------------------------------------------------
289 - numeric shortcuts for naming patches near top (eg. +1, -2)
290 - (config?) parameter for number of patches included by "series -s"
296 ----------------------------------------------------------------------
298 The 'conflict markers' +<<<<<<<+, +=======+, and +>>>>>>>+ indicate
299 which lines were already there (+current+) and which were added by the
300 patch (+patched+). Edit the file so that it looks like it should; in
301 this case, we want something like this:
303 ----------------------------------------------------------------------
304 - numeric shortcuts for naming patches near top (eg. +1, -2)
305 - (config?) parameter for number of patches included by "series -s"
308 ----------------------------------------------------------------------
310 Note that ``looks like it should'' includes removing the conflict
313 Now that we've resolved the conflict, we just need to tell StGit about
320 +TODO+ is listed as being modified, not in conflict. And we know from
321 before how to deal with modified files:
325 The conflict is now resolved. We can see that +first+ now looks a
326 little different; it no longer adds a line at the end of the file:
329 commit 8e3ae5f6fa6e9a5f831353524da5e0b91727338e
330 Author: Audrey U. Thor <author@example.com>
331 Date: Sun Oct 5 14:43:42 2008 +0200
335 diff --git a/TODO b/TODO
336 index 812d236..4ef3841 100644
339 @@ -24,4 +24,5 @@ The future, when time allows or if someone else does them:
340 they have scripts for moving the changes in one to the others)
341 - numeric shortcuts for naming patches near top (eg. +1, -2)
342 - (config?) parameter for number of patches included by "series -s"
347 Workflow: Development branch
348 ============================
350 One common use of StGit is to ``polish'' a Git branch before you
351 publish it for others to see. Such history falsification can often be
352 a 'good' thing -- when you (or someone else) needs to look at what you
353 did six months later, you are not really interested in all the false
354 starts and the steps needed to corect them. What you want is the final
355 solution, presented in a way that makes it easy to read and
358 Of course, there are limits. Editing the last few days' worth of
359 history is probably a good idea; editing the last few months' probably
360 isn't. A rule of thumb might be to not mess with history old enough
361 that you don't remember the details anymore. And rewriting history
362 that you have published for others to see (and base their own work on)
363 usually just makes everyone more confused, not less.
365 So, let's take a concrete example. Say that you're hacking on StGit,
366 and have made several Git commits as your work progressed, with commit
367 messages such as ``Improve the snarfle cache'', ``Remove debug
368 printout'', ``New snarfle cache test'', ``Oops, spell function name
369 correctly'', ``Fix documentation error'', and ``More snarfle cache''.
371 Now, this is the actual history, but for obvious reasons, this isn't
372 the kind of history you'd ideally want to find when you six months
373 from now try to figure out exactly where that elusive snarfle cache
374 bug was introduced. So let's turn this into the history we can be
375 proud of. The first step is to make StGit patches out of all those Git
378 $ stg uncommit --number 6
379 Uncommitting 6 patches ...
380 Now at patch "more-snarfle-cache"
382 $ stg series --description
383 + improve-the-snarfle-cache # Improve the snarfle cache
384 + remove-debug-printout # Remove debug printout
385 + new-snarfle-cache-test # New snarfle cache test
386 + oops-spell-function-name-corre # Oops, spell function name correctly
387 + fix-documentation-error # Fix documentation error
388 > more-snarfle-cache # More snarfle cache
390 As you can see, stglink:uncommit[] adds StGit metadata to the last few
391 Git commits, turning them into StGit patches so that we can do stuff
394 NOTE: With the +$$--number$$+ flag, stglink:uncommit[] uncommits that
395 many commits and generates names for them based on their commit
396 messages. If you like, you can instead list the patch names you want
399 At this point, there are a number of things we could do:
401 * Continue developing, and take advantage of e.g. stglink:goto[] or
402 +stg refresh $$--patch$$+ to stick updates in the right patch to
405 * Use e.g. stglink:float[], stglink:sink[], stglink:push[], and
406 stglink:pop[] to reorder patches.
408 * Use stglink:coalesce[] to merge two or more patches into one.
409 stgsublink:coalesce[] pushes and pops so that the patches to be
410 merged are consecutive and unrelated patches aren't in the way,
411 then makes one big patch out of the patches to be merged, and
412 finally pushes the other patches back.
414 Of course, as always when there is pushing involved, there is the
415 possibility of conflicts. If a push results in a conflict, the
416 operation will be halted, and we'll be given the option of either
417 resolving the conflict or undoing.
419 Once we feel that the history is as good as it's going to get, we can
420 remove the StGit metadata, turning the patches back into regular Git
425 TIP: stglink:commit[] can also commit specific patches (named on the
426 command line), leaving the rest alone. This can be used to retire
427 patches as they mature, while keeping the newer and more volatile
431 Workflow: Tracking branch
432 =========================
435 Rebasing a patch series
436 -----------------------
441 Getting patches upstream
442 ------------------------
444 TODO:: export, mail, ...
453 Other stuff that needs to be placed somewhere
454 =============================================
460 TODO:: undo, redo, log, reset
463 Interoperating with Git
464 -----------------------
468 * git commit + repair
470 * git reset HEAD~n + repair
472 * don't do git rebase or git merge, because it won't work
478 TODO:: This section needs revising. I've only fixed the formatting.
479 Most of it should go under "Workflow: Tracking branch"
481 As mentioned in the introduction, StGit stores modifications to your
482 working tree in the form of Git commits. This means if you want to
483 apply your changes to a tree not managed by Git, or send your changes
484 to someone else in e-mail, you need to convert your StGit patches into
485 normal textual diffs that can be applied with the GNU patch command.
486 stglink:diff[] is a powerful way to generate and view textual diffs of
487 patches managed by StGit.
489 To view a diff of the topmost patch:
493 Observe that this does not show any changes in the working directory
494 that have not been saved by a stgsublink:refresh[]. To view just the
495 changes you've made since the last refresh, use:
499 If you want to see the changes made by the patch combined with any
500 unsaved changes in the working directory, try:
502 $ stg diff -r /bottom
504 You can also show the changes to any patch in your stack with:
506 $ stg diff -r <patch>/
508 Use this command to view all the changes in your stack up through the
513 stglink:diff[] supports a number of other features that are very
514 useful. Be sure to take a look at the help information for this
515 command. To convert your StGit patches into patch files:
517 $ stg export [--range=[<patch1>[:<patch2>]]] [<dir-name>]
519 stglink:export[] supports options to automatically number the patches
520 (+-n+) or add the +.diff+ extension (+-d+). If you don't tell
521 stgsublink:export[] where to put the patches, it will create directory
522 named +patch-<branchname>+ in your current directory, and store the
523 patches there. To e-mail a patch or range of patches:
525 $ stg mail [--to=...] (--all | --range=[<patch1>[:<patch2>]] | <patch>)
527 stglink:mail[] has a lot of options, so read the output of +stg mail
528 -h+ for more information.
530 You can also import an existing GNU diff patch file as a new StGit
531 patch with a single command. stglink:import[] will automatically parse
532 through the patch file and extract a patch description. Use:
534 $ stg import [<file>]
536 This is the equivalent of
542 Sometimes the patch file won't apply cleanly. In that case,
543 stglink:import[] will leave you with an empty StGit patch, to which
544 you then apply the patch file by hand using "patch -i" and your
547 To merge a GNU diff file (defaulting to the standard input) into the
552 This command supports a +$$--threeway$$+ option which applies the
553 patch onto the bottom of the topmost one and performs a three-way
560 TODO:: This section needs revising. I've only fixed the formatting.
562 stglink:export[] and stglink:mail[] use templates for generating the
563 patch files or e-mails. The default templates are installed under
564 +<prefix>/share/stgit/templates/+ and, combined with the extra options
565 available for these commands, should be enough for most users. The
566 template format uses the standard Python string formatting rules. The
567 variables available are listed in the the manual pages for each
568 command. stglink:mail[] can also send an initial 'cover' e-mail for
569 which there is no default template. The
570 +<prefix>/share/stgit/examples/firstmail.tmpl+ file can be used as an
571 example. A default description for new patches can be defined in the
572 +.git/ patchdescr.tmpl+ file. This is useful for things like