comparison templates/guide/index.html @ 397:29d4b5e45423

Use flask to render site and get rid of submodules We don't want to use statically generated html files anymore. We are using flask to do the routing and render the templates for now. This means we also get rid of the submoduels and put everything together in templates/.
author David Soria Parra <davidsp@fb.com>
date Fri, 07 Mar 2014 14:47:13 -0800
parents
children b40d4f1881cf
comparison
equal deleted inserted replaced
396:b3036c323c2d 397:29d4b5e45423
1 {% extends "base.html" %}
2
3 {% block content %}
4
5 <h1>Learning Mercurial in Workflows</h1>
6
7 <p>With Mercurial you can use a multitude of different workflows. This page shows some of them, including their use cases. It is intended to make it easy for beginners of version tracking to get going instantly and learn completely incrementally. It doesn't explain the concepts used, because there are already many other great resources doing that. </p>
8
9 <p>Alternatives to this guide and further reading: </p>
10
11 <ul>
12 <li> <a title="Mercurial Tutorial" href="http://www.selenic.com/mercurial/wiki/Tutorial">Tutorial</a> - a more exhaustive tutorial. </li>
13 <li><a title="Mercurial: The definitive Guide" href="http://hgbook.red-bean.com/">Mercurial: The Definitive Guide</a> - a very detailed description of Mercurial including <a title="Behind the Scenes" href="http://hgbook.red-bean.com/read/behind-the-scenes.html">behind the scenes</a>, an indepth article on the design of Mercurial.</li>
14 <li><a title="Understanding Mercurial" href="http://www.selenic.com/mercurial/wiki/UnderstandingMercurial">Understanding Mercurial</a> - the concepts behind Mercurial.</li>
15 </ul>
16
17 <div class="note">
18 <p class="note-title">Note:</p>
19 This guide doesn't require any prior knowledge of version control systems (though subversion users will likely feel at home quite quickly). Basic command line abilities are helpful, because we'll use the command line client. <!--If you already know other systems, please check our transition guides: svn, cvs, git, bzr -->
20 </div>
21 <h1 id="basic_workflow">Basic workflows</h1>
22
23 <p><em>We go from simple to more complex workflows. Those further down build on previous workflows.</em></p>
24
25 <h2 id="log_keeping">Log keeping</h2>
26
27 <h3>Use Case</h3>
28
29 <p>The first workflow is also the easiest one: You want to use Mercurial to be able to <strong>look back when you did which changes</strong>.</p>
30
31 <p>This workflow only requires an installed Mercurial and write access to some file storage (you almost definitely have that :) ). It shows the basic techniques for more complex workflows.</p>
32
33 <h3>Workflow</h3>
34
35 <h5>Prepare Mercurial</h5>
36
37 <p>As first step, you should teach Mercurial your name. For that you open the file ~/.hgrc (or mercurial.ini in your home directory for Windows) with a text-editor and add the ui section (user interaction) with your username:</p>
38
39 <pre>[ui]
40 username = Mr. Johnson &lt;johnson@smith.com&gt;
41
42 </pre>
43
44 <h5>Initialize the project</h5>
45
46 <p>Now you add a new folder in which you want to work:</p>
47
48 <pre>$ hg init project
49
50 </pre>
51
52 <h5>Add files and track them</h5>
53
54 <p>Enter the project folder, create some files, then add and commit them.</p>
55
56 <pre>$ cd project
57 $ echo 'print("Hello")' > hello.py
58 $ hg add
59 $ hg commit
60 (enter the commit message)
61
62 </pre>
63
64 <div class="output">output of hg add:
65 adding hello.py
66 </div>
67
68 <div class="output">display of hg commit (with your message):
69 Initial commit.
70
71 HG: Enter commit message. Lines beginning with 'HG:' are removed.
72 HG: Leave message empty to abort commit.
73 HG: --
74 HG: user: Mr. Johnson &lt;johnson@smith.com&gt;
75 HG: branch 'default'
76 HG: added hello.py
77 </div>
78
79 <p>You can then look into your initial history with <em>hg log</em>:</p>
80
81 <pre>$ hg log
82
83 </pre>
84
85 <div class="output">output of hg log:
86 changeset: 0:a5ecbf5799c8
87 user: Mr. Johnson <johnson@smith.com>
88 date: Sun Nov 20 11:00:00 2011 +0100
89 summary: Initial commit.
90 </div>
91
92
93 <div class="note">
94 <p class="note-title">Note:</p>
95 You can also go into an existing directory with files and init the repository there.
96
97 <pre>$ cd project
98 $ hg init
99
100 </pre>
101
102 <p>Also you can add only specific files instead of all files in the directory. Mercurial will then track only these files and won't know about the others. The following tells mercurial to track all files whose names begin with "file0" as well as file10, file11 and file12.</p>
103
104 <pre>$ hg add file0* file10 file11 file12
105
106 </pre>
107 </div>
108
109 <h5>Save changes</h5>
110
111 <p>First do some changes:</p>
112
113 <pre>$ echo 'print("Hello World") > hello.py
114
115 </pre>
116
117 <p>see which files changed, which have been added or removed, and which aren't tracked yet</p>
118
119 <pre>$ hg status
120
121 </pre>
122
123 <div class="output">output of hg status:
124 M hello.py
125 </div>
126
127
128 <p>see the exact changes</p>
129
130 <pre>$ hg diff
131
132 </pre>
133
134 <div class="output">output of hg diff:
135 diff --git a/hello.py b/hello.py
136 --- a/hello.py
137 +++ b/hello.py
138 @@ -1,1 +1,1 @@
139 -print("Hello")
140 +print("Hello World")
141 </div>
142
143
144 <p><em>commit</em> the changes.</p>
145
146 <pre>$ hg commit
147
148 </pre>
149
150 <p>now an editor pops up and asks you for a commit message. Upon saving and closing the editor, your changes have been stored by Mercurial.</p>
151
152 <div class="output">display of hg commit (with your message):
153 Say Hello World, not just Hello.
154
155 HG: Enter commit message. Lines beginning with 'HG:' are removed.
156 HG: Leave message empty to abort commit.
157 HG: --
158 HG: user: Mr. Johnson &lt;johnson@smith.com&gt;
159 HG: branch 'default'
160 HG: changed hello.py
161 </div>
162
163 <p>Your history now looks like this:</p>
164
165 <div class="output">output of hg log:
166 changeset: 1:487d7a20ccbc
167 user: Mr. Johnson <johnson@smith.com>
168 date: Sun Nov 20 11:11:00 2011 +0100
169 summary: Say Hello World, not just Hello.
170
171 changeset: 0:a5ecbf5799c8
172 user: Mr. Johnson <johnson@smith.com>
173 date: Sun Nov 20 11:00:00 2011 +0100
174 summary: Initial commit.
175 </div>
176
177
178 <div class="note">
179 <p class="note-title">Note:</p>
180 You can also supply the commit message directly via <em>hg commit -m 'MESSAGE'</em>.
181 </div>
182
183 <h5>Copy and move files</h5>
184
185 <p>When you copy or move files, you should tell Mercurial to do the copy or move for you, so it can track the relationship between the files.</p>
186
187 <p>Remember to <em>commit</em> after moving or copying. From the basic commands only <em>commit</em> creates a new revision</p>
188
189 <pre>$ hg cp hello.py copy
190 $ hg mv hello.py target
191 $ hg diff # see the changes
192 $ hg commit
193 (enter the commit message)
194
195 </pre>
196
197 <p>Now you have two files, "copy" and "target", and Mercurial knows how they are related.</p>
198
199 <div class="output">output of hg diff (before the commit):
200 diff --git a/hello.py b/copy
201 rename from hello.py
202 rename to copy
203 diff --git a/hello.py b/target
204 copy from hello.py
205 copy to target
206 </div>
207
208 <div class="note">
209 <p class="note-title">Note:</p>
210 Should you forget to do the explicit copy or move, you can still tell Mercurial to detect the changes via <em>hg addremove --similarity 100</em>. Just use <em>hg help addremove</em> for details.
211 </div>
212
213 <h5>Check your history</h5>
214
215 <pre>$ hg log
216
217 </pre>
218
219 <p>This prints a list of changesets along with their date, the user who committed them (you) and their commit message. </p>
220
221 <div class="output">output of hg log:
222 changeset: 2:70eb0ca9d264
223 tag: tip
224 user: Mr. Johnson <johnson@smith.com>
225 date: Sun Nov 20 11:20:00 2011 +0100
226 summary: Copy and move.
227
228 changeset: 1:487d7a20ccbc
229 user: Mr. Johnson <johnson@smith.com>
230 date: Sun Nov 20 11:11:00 2011 +0100
231 summary: Say Hello World, not just Hello.
232
233 changeset: 0:a5ecbf5799c8
234 user: Mr. Johnson <johnson@smith.com>
235 date: Sun Nov 20 11:00:00 2011 +0100
236 summary: Initial commit.
237 </div>
238
239 <p>To see a certain revision, you can use the <em>-r</em> switch (--revision). To also see the diff of the displayed revisions, there's the <em>-p</em> switch (--patch)</p>
240
241 <pre>$ hg log -p -r 3
242
243 </pre>
244
245 <h2 id="lone_developer">Lone developer with nonlinear history</h2>
246
247 <h3>Use case</h3>
248
249 <p>The second workflow is still very easy: You're a lone developer and you want to use Mercurial to <strong>keep track of your own changes</strong> and <strong>optimize your workflow</strong>.</p>
250
251 <p>It works just like the log keeping workflow, with the difference that you go back to earlier changes at times and work onwards from there.</p>
252
253 <p>To start a new project, you initialize a repository, add your files and commit whenever you finished a part of your work.</p>
254
255 <p>Also you check your history from time to time, so see how you progressed.</p>
256
257 <h3>Workflow</h3>
258
259 <h5>Basics from log keeping</h5>
260
261 <p>Init your project, add files, see changes and commit them.</p>
262
263 <pre>$ hg init project
264 $ cd project
265 $ (add files)
266 $ hg add # tell Mercurial to track all files
267 $ (do some changes)
268 $ hg diff # see changes
269 $ hg commit # save changes
270 $ hg cp # copy files or folders
271 $ hg mv # move files or folders
272 $ hg log # see history
273
274 </pre>
275
276 <h5>Seeing an earlier revision</h5>
277
278 <p>Different from the log keeping workflow, you'll want to go back in history at times and do some changes directly there, for example because an earlier change introduced a bug and you want to fix it where it occurred.</p>
279
280 <p>To look at a previous version of your code, you can use update. Let's assume that you want to see revision 1.</p>
281
282 <pre>$ hg update 1
283
284 </pre>
285
286 <p>Now your code is back at revision 1, the second commit (Mercurial starts counting at 0).
287 To check if you're really at that revision, you can use <em>identify -n</em>.</p>
288
289 <pre>$ hg identify -n
290
291 </pre>
292
293 <div class="output">output of hg identify -n:
294 1
295 </div>
296
297 <div class="output">output of ls:
298 hello.py
299 </div>
300
301
302 <div class="note">
303 <p class="note-title">Note:</p>
304 <em>identify</em> without options gives you the short form of a unique revision ID. That ID is what Mercurial uses internally. If you tell someone about the version you updated to, you should use that ID, since the numbers can be different for other people. If you want to know the reasons behind that, please read up Mercurials [<a href="http://mercurial.selenic.com/wiki/UnderstandingMercurial">basic concepts</a>]. When you're at the most recent revision, <em>hg identify -n</em> will return "-1".
305 </div>
306
307 <p>To update to the most recent revision, you can use "tip" as revision name.</p>
308
309 <pre>$ hg update tip
310
311 </pre>
312
313 <div class="note">
314 <p class="note-title">Note:</p>
315 If at any place any command complains, your best bet is to read what it tells you and follow that advice.
316 </div>
317
318 <div class="note">
319 <p class="note-title">Note:</p>
320 Instead of <em>hg update</em> you can also use the shorthand <em>hg up</em>. Similarly you can abbreviate <em>hg commit</em> to <em>hg ci</em>.
321 </div>
322
323 <div class="note">
324 <p class="note-title">Note:</p>
325 To get a revision devoid of files, just <em>update</em> to "null" via <em>hg update null</em>. That's the revision before any files were added.
326 </div>
327
328 <div class="note">
329 <p class="note-title">Note:</p>
330 If the output of <em>hg identify</em> ends in a “+”, your repository has uncommitted changes.
331 </div>
332
333
334 <h5>Fixing errors in earlier revisions</h5>
335
336 <p>When you find a bug in some earlier revision you have two options: either you can fix it in the current code, or you can go back in history and fix the code exactly where you did it, which creates a cleaner history.</p>
337
338 <p>To do it the cleaner way, you first update to the old revision, fix the bug and commit it. Afterwards you merge this revision and commit the merge. Don't worry, though: Merging in mercurial is fast and painless, as you'll see in an instant.</p>
339
340 <p>Let's assume the bug was introduced in revision 1.</p>
341
342 <pre>$ hg update 1
343 $ echo 'print("Hello Mercurial")' > hello.py
344 $ hg commit
345
346 </pre>
347
348 <div class="output">output of hg commit (after entering the message):
349 created new head
350 </div>
351
352 <div class="note">
353 <p class="note-title">Note:</p>
354 “created new head” means that there is now one more revision, which does not have children. Heads are current states of your project living side by side. It is good style to merge them together before propagating them.
355 </div>
356
357
358 <p>Now the fix is already stored in history. We just need to merge it with the current version of your code.</p>
359
360 <pre>$ hg merge
361
362 </pre>
363
364 <div class="output">output of hg merge (here):
365 merging hello.py and copy to copy
366 merging hello.py and target to target
367 </div>
368
369 <p>If there are conflicts use <em>hg resolve</em> - that's also what merge tells you to do in case of conflicts.</p>
370
371 <p>First list the files with conflicts</p>
372
373 <pre>$ hg resolve --list
374
375 </pre>
376
377 <p>Then resolve them one by one. <em>resolve</em> attempts the merge again</p>
378
379 <pre>$ hg resolve conflicting_file
380 (fix it by hand, if necessary)
381
382 </pre>
383
384 <div class="note">
385 <p class="note-title">Note:</p>
386 For more details on resolving conflicts, see the wiki-page <a title="Tutorial - Merging conflicting Changes" href="http://mercurial.selenic.com/wiki/TutorialConflict">TutorialConflict</a>.
387 </div>
388
389
390 <p>Mark the fixed file as resolved</p>
391
392 <pre>$ hg resolve --mark conflicting_file
393
394 </pre>
395
396 <p>Commit the merge, as soon as you resolved all conflicts. This step is also necessary when there were no conflicts!</p>
397
398 <pre>$ hg commit
399
400 </pre>
401
402 <p>At this point, your fix is merged with all your other work, and you can just go on coding. Additionally the history shows clearly where you fixed the bug, so you'll always be able to check where the bug was.</p>
403
404 <div class="output">output of hg log (after the final commit):
405 changeset: 4:3b06bba7c1a9
406 tag: tip
407 parent: 3:7ff5cd572d80
408 parent: 2:70eb0ca9d264
409 user: Mr. Johnson <johnson@smith.com>
410 date: Sun Nov 20 20:11:00 2011 +0100
411 summary: merge greeting and copy+move.
412
413 changeset: 3:7ff5cd572d80
414 parent: 1:487d7a20ccbc
415 user: Mr. Johnson <johnson@smith.com>
416 date: Sun Nov 20 20:00:00 2011 +0100
417 summary: Greet Mercurial
418
419 changeset: 2:70eb0ca9d264
420 user: Mr. Johnson <johnson@smith.com>
421 date: Sun Nov 20 11:20:00 2011 +0100
422 summary: Copy and move.
423
424 changeset: 1:487d7a20ccbc
425 user: Mr. Johnson <johnson@smith.com>
426 date: Sun Nov 20 11:11:00 2011 +0100
427 summary: Say Hello World, not just Hello.
428
429 changeset: 0:a5ecbf5799c8
430 user: Mr. Johnson <johnson@smith.com>
431 date: Sun Nov 20 11:00:00 2011 +0100
432 summary: Initial commit.
433 </div>
434
435
436 <div class="note">
437 <p class="note-title">Note:</p>
438 Most merges will just work. You only need <em>resolve</em>, when <em>merge</em> complains.
439 </div>
440
441 <p>So now you can initialize repositories, save changes, update to previous changes and develop in a nonlinear history by committing in earlier changesets and merging the changes into the current code.</p>
442
443 <div class="note">
444 <p class="note-title">Note:</p>
445 If you fix a bug in an earlier revision, and some later revision copied or moved that file, the fix will be propagated to the target file(s) when you merge. This is the main reason why you should always use <em>hg cp</em> and <em>hg mv</em>.
446 </div>
447
448 <h2 id="separate_features">Separate features</h2>
449
450 <h3>Use Case</h3>
451
452 <p>At times you'll be <strong>working on several features in parallel</strong>. If you want to avoid mixing incomplete code versions, you can create clones of your local repository and work on each feature in its own code directory.</p>
453
454 <p>After finishing your feature you then <em>pull</em> it back into your main directory and <em>merge</em> the changes.</p>
455
456 <h3>Workflow</h3>
457
458 <h5>Work in different clones</h5>
459
460 <p>First create the feature clone and do some changes</p>
461
462 <pre>$ hg clone project feature1
463 $ cd feature1
464 $ hg update 3
465 $ echo 'print("Hello feature1")' > hello.py
466 $ hg commit -m "Greet feature1"
467
468 </pre>
469
470 <p>Now check what will come in when you <em>pull</em> from feature1, just like you can use <em>diff</em> before committing. The respective command for pulling is <em>incoming</em></p>
471
472 <pre>$ cd ../project
473 $ hg incoming ../feature1
474
475 </pre>
476
477 <div class="output">output of hg incoming ../feature1:
478 comparing with ../feature1
479 searching for changes
480 changeset: 5:3eb7b39fcf57
481 tag: tip
482 parent: 3:7ff5cd572d80
483 user: Arne Babenhauserheide <bab@draketo.de>
484 date: Sun Nov 20 20:11:11 2011 +0100
485 summary: Greet feature1
486 </div>
487
488
489 <div class="note">
490 <p class="note-title">Note:</p>
491 If you want to see the diffs, you can use <em>hg incoming --patch</em> just as you can do with <em>hg log --patch</em> for the changes in the repository.
492 </div>
493
494 <p>If you like the changes, you pull them into the project</p>
495
496 <pre>$ hg pull ../feature1
497
498 </pre>
499
500 <p>Now you have the history of feature1 inside your project, but the changes aren't yet visible. Instead they are only stored inside a ".hg" directory of the project (<a title="Behind the Scenes" href="http://hgbook.red-bean.com/read/behind-the-scenes.html">more information on the store</a>).</p>
501
502 <div class="output">output of hg pull:
503 pulling from ../feature1
504 searching for changes
505 adding changesets
506 adding manifests
507 adding file changes
508 added 1 changesets with 1 changes to 1 files (+1 heads)
509 (run 'hg heads' to see heads, 'hg merge' to merge)
510 </div>
511
512 <div class="note">
513 <p class="note-title">Note:</p>
514 From now on we'll use the name "repository" for a directory which has a .hg directory with Mercurial history.
515 </div>
516
517 <p>If you didn't do any changes in the project, while you were working on feature1, you can just update to tip (<em>hg update tip</em>), but it is more likely that you'll have done some other changes in between changes. In that case, it's time for merging.</p>
518
519 <p>Merge feature1 into the project code</p>
520
521 <pre>$ hg merge
522
523 </pre>
524
525 <p>If there are conflicts use <em>hg resolve</em> - that's also what merge tells you to do in case of conflicts. After you <em>merge</em>, you have to <em>commit</em> explicitly to make your <em>merge</em> final</p>
526
527 <pre>$ hg commit
528 (enter commit message, for example "merged feature1")
529
530 </pre>
531
532 <div class="output">output of hg log -r -1:-3 (the last 3 changesets):
533 changeset: 6:e8a33691171a
534 tag: tip
535 parent: 4:3b06bba7c1a9
536 parent: 5:3eb7b39fcf57
537 user: Mr. Johnson <johnson@smith.com>
538 date: Sun Nov 20 20:20:00 2011 +0100
539 summary: merged feature1
540
541 changeset: 5:3eb7b39fcf57
542 parent: 3:7ff5cd572d80
543 user: Arne Babenhauserheide <bab@draketo.de>
544 date: Sun Nov 20 20:11:11 2011 +0100
545 summary: Greet feature1
546
547 changeset: 4:3b06bba7c1a9
548 parent: 3:7ff5cd572d80
549 parent: 2:70eb0ca9d264
550 user: Mr. Johnson <johnson@smith.com>
551 date: Sun Nov 20 20:11:00 2011 +0100
552 summary: merge greeting and copy+move.
553 </div>
554
555
556 <div class="note">
557 <p class="note-title">Note:</p>
558 Mercurial offers powerful ways specify revisions. Too see them all, use <em>hg help revsets</em>.
559 </div>
560
561
562 <p>You can create an arbitrary number of clones and also carry them around on USB sticks. Also you can use them to synchronize your files at home and at work, or between your desktop and your laptop.</p>
563
564 <div class="note">
565 <p class="note-title">Note:</p>
566 You also have to commit after a merge when there are no conflicts, because merging creates new history and you might want to attach a specific message to the merge (like "merge feature1").
567 </div>
568
569 <h5>Rollback mistakes</h5>
570
571 <p>Now you can work on different features in parallel, but from time to time a bad commit might sneak in. Naturally you could then just go back one revision and merge the stray error, keeping all mistakes out of the merged revision. However, there's an easier way, if you realize your error before you do another <em>commit</em> or <em>pull</em>: <em>rollback</em>.</p>
572
573 <p>Rolling back means undoing the last operation which added something to your history.</p>
574
575 <p>Imagine you just realized that you did a bad commit - for example you didn't see a spelling error in a commit message. To fix it you would use</p>
576
577 <pre>$ hg rollback
578
579 </pre>
580
581 <div class="output">output of hg rollback:
582 repository tip rolled back to revision 5 (undo commit)
583 working directory now based on revisions 4 and 5
584 </div>
585
586 <p>And then redo the commit</p>
587
588 <pre>$ hg commit -m "Merged Feature 1"
589
590 </pre>
591
592 <div class="output">output of hg log -r -1:-2 (after rollback and commit):
593 changeset: 6:3f549b33c7ef
594 tag: tip
595 parent: 4:3b06bba7c1a9
596 parent: 5:3eb7b39fcf57
597 user: Mr. Johnson <johnson@smith.com>
598 date: Sun Nov 20 20:20:11 2011 +1100
599 summary: Merged Feature 1
600
601 changeset: 5:3eb7b39fcf57
602 parent: 3:7ff5cd572d80
603 user: Arne Babenhauserheide <bab@draketo.de>
604 date: Sun Nov 20 20:11:11 2011 +0100
605 summary: Greet feature1
606 </div>
607
608
609
610 <p>If you can use the command history of your shell and you added the previous message via <em>commit -m "message"</em>, that following commit just means two clicks on the arrow-key "up" and one click on "enter".</p>
611
612 <p>Though it changes your history, rolling back doesn't change your files. It only undoes the last addition to your history.</p>
613
614 <p>But beware, that a rollback itself can't be undone. If you <em>rollback</em> and then forget to commit, you can't just say "give me my old commit back". You have to create a new commit.</p>
615
616 <div class="note">
617 <p class="note-title">Note:</p>
618 Rollback is possible, because Mercurial uses transactions when recording changes, and you can use the transaction record to undo the last transaction. This means that you can also use <em>rollback</em> to undo your last <em>pull</em>, if you didn't yet commit anything new.
619 </div>
620
621 <h2 id="sharing_changes">Sharing changes</h2>
622
623 <h3>Use Case</h3>
624
625 <p>Now we go one step further: You are no longer alone, and you want to <strong>share your changes with others and include their changes</strong>.</p>
626
627 <p>The basic requirement for that is that you have to be able to see the changes of others.</p>
628
629 <p>Mercurial allows you to do that very easily by including a simple webserver from which you can <em>pull</em> changes just as you can pull changes from local clones.</p>
630
631 <div class="note">
632 <p class="note-title">Note:</p>
633 There are a few other ways to share changes, though. Instead of using the builtin webserver, you can also send the changes by email or setup a shared repository, to where you <em>push</em> changes instead of pulling them. We'll cover one of those later.
634 </div>
635
636 <h3>Workflow</h3>
637
638 <h5>Using the builtin webserver</h5>
639
640 <p>This is the easiest way to quickly share changes.</p>
641
642 <p>First the one who wants to share his changes creates the webserver</p>
643
644 <pre>$ hg serve
645
646 </pre>
647
648 <p>Now all others can point their browsers to his IP address (for example 192.168.178.100) at port 8000. They will then see all his history there and can decide if they want to pull his changes.</p>
649
650 <pre>$ firefox http://192.168.178.100:8000
651
652 </pre>
653
654 <p>If they decide to include the changes, they just pull from the same URL</p>
655
656 <pre>$ hg pull http://192.168.178.100:8000
657
658 </pre>
659
660 <p>At this point you all can work as if you had pulled from a local repository. All the data is now in your individual repositories and you can merge the changes and work with them without needing any connection to the served repository.</p>
661
662 <h5>Sending changes by email</h5>
663
664 <p>Often you won't have direct access to the repository of someone else, be it because he's behind a restrictive firewall, or because you live in different timezones. You might also want to keep your changes confidential and prefer internal email (if you want additional protection, you can also encrypt the emails, for example with <a href="http://gnupg.org" title="GnuPG">GnuPG</a>).</p>
665
666 <p>In that case, you can easily export your changes as patches and send them by email.</p>
667
668 <p>Another reason to send them by email can be that your policy requires manual review of the changes when the other developers are used to reading diffs in emails. I'm sure you can think of more reasons.</p>
669
670 <p>Sending the changes via email is pretty straightforward with Mercurial. You just <em>export</em> your changes and attach (or copy paste) it in your email. Your colleagues can then just <em>import</em> them.</p>
671
672 <p>First check which changes you want to export</p>
673
674 <pre>$ cd project
675 $ hg log
676
677 </pre>
678
679 <p>We assume that you want to export changeset 3 and 4</p>
680
681 <pre>$ hg export 3 > change3.diff
682 $ hg export 4 > change4.diff
683
684 </pre>
685
686 <p>Now attach them to an email and your colleagues can just run <em>import</em> on both diffs to get your full changes, including your user information.</p>
687
688 <p>To be careful, they first <em>clone</em> their repository to have an integration directory as sandbox</p>
689
690 <pre>$ hg clone project integration
691 $ cd integration
692 $ hg import change3.diff
693 $ hg import change4.diff
694
695 </pre>
696
697 <p>That's it. They can now test your changes in feature clones. If they accept them, they <em>pull</em> the changes into the main repository</p>
698
699 <pre>$ cd ../project
700 $ hg pull ../integration
701
702 </pre>
703
704 <div class="note">
705 <p class="note-title">Note:</p>
706 The <em>patchbomb</em> extension automates the email-sending, but you don't need it for this workflow.
707 </div>
708
709 <div class="note">
710 <p class="note-title">Note:</p>
711
712
713 You can also send around bundles, which are snippets of your actual history. Just create them via
714
715 <pre>$ hg bundle --base FIRST_REVISION_TO_BUNDLE changes.bundle
716
717 </pre>
718
719 Others can then get your changes by simply pulling them, as if your bundle were an actual repository
720 <pre>$ hg pull path/to/changes.bundle
721
722 </pre>
723
724 </div>
725
726 <h5>Using a shared repository</h5>
727
728 <p>Sending changes by email might be the easiest way to reach people when you aren't yet part of the regular development team, but it creates additional workload: You have to <em>bundle</em> the changes, send mails and then <em>import</em> the bundles manually. Luckily there's an easier way which works quite well: The shared push repository.</p>
729
730 <p>Till now we transferred all changes either via email or via <em>pull</em>. Now we use another option: pushing. As the name suggests it's just the opposite of pulling: You <em>push</em> your changes into another repository.</p>
731
732 <p>But to make use of it, we first need something we can push to.</p>
733
734 <p>By default <em>hg serve</em> doesn't allow pushing, since that would be a major security hole. You can allow pushing in the server, but that's no solution when you live in different timezones, so we'll go with another approach here: Using a shared repository, either on an existing shared server or on a service like <a title="BitBucket" href="http://bitbucket.org">BitBucket</a>. Doing so has a bit higher starting cost and takes a bit longer to explain, but it's well worth the effort spent.</p>
735
736 <p>If you want to use an existing shared server, you can use <em>serve</em> there and <a title="How to allow pushing for hg serve" href="http://www.selenic.com/mercurial/wiki/HgWebDirStepByStep#head-746ca383e3a62df34279ec2fca888113497da022">allow pushing</a>. Also there are some other nice ways to <a title="Multiple Committers" href="http://www.selenic.com/mercurial/wiki/MultipleCommitters">allow pushing to a Mercurial repository</a>, including simple <a title="Setting up a shared Mercurial repository using SSH" href="http://www.selenic.com/mercurial/wiki/SharedSSH">access via SSH</a>.</p>
737
738 <p>Otherwise you first need to setup a BitBucket Account. Just signup at <a title="BitBucket" href="http://bitbucket.org">BitBucket</a>. After signing up (and login) hover your mouse over "Repositories". There click the item at the bottom of the opening dialog which say "Create new".</p>
739
740 <p>Give it a name and a description. If you want to keep it hidden from the public, select "private"</p>
741
742 <pre>$ firefox http://bitbucket.org
743
744 </pre>
745
746 <p>Now your repository is created and you see instructions for <em>push</em>ing to it. For that you'll use a command similar to the following (just with a different URL):</p>
747
748 <pre>$ hg push https://bitbucket.org/ArneBab/hello/
749
750 </pre>
751
752 <p>(Replace the URL with the URL of your created repository. If your username is "Foo" and your repository is named "bar", the URL will be https://bitbucket.org/Foo/bar/)</p>
753
754 <p>Mercurial will ask for your BitBucket name and password, then <em>push</em> your code.</p>
755
756 <p>Voilà, your code is online.</p>
757
758 <p>To see what you would get if you would push, you can use outgoing. It works with local repositories in the same way as with shared ones, so you can test it with a local one:</p>
759
760 <pre>$ hg outgoing ../feature1
761
762 </pre>
763
764 <div class="output">output of hg outgoing ../feature1 (our feature seperation repo):
765 comparing with ../feature1
766 searching for changes
767 changeset: 6:3f549b33c7ef
768 tag: tip
769 parent: 4:3b06bba7c1a9
770 parent: 5:3eb7b39fcf57
771 user: Mr. Johnson <johnson@smith.com>
772 date: Sun Nov 20 20:20:11 2011 +1100
773 summary: Merged Feature 1
774 </div>
775
776
777 <div class="note">
778 <p class="note-title">Note:</p>
779 You can also <a title="use SSH for pushing to BitBucket" href="http://bitbucket.org/help/UsingSSH">use SSH for pushing to BitBucket</a>.
780 </div>
781
782 <p>Now it's time to tell all your colleagues to sign up at BitBucket, too.</p>
783
784 <p>After that you can click the "Admin" tab of your created repository and add the usernames of your colleagues on the right side under "Permission: Writers". Now they are allowed to <em>push</em> code to the repository.</p>
785
786 <p>(If you chose to make the repository private, you'll need to add them to "Permission: Readers", too)</p>
787
788 <p>If one of you now wants to publish changes, he'll simply <em>push</em> them to the repository, and all others get them by <em>pull</em>ing.</p>
789
790 <p>Publish your changes</p>
791
792 <pre>$ hg push https://bitbucket.org/ArneBab/hello/
793
794 </pre>
795
796 <p>Pull others changes into your local repository</p>
797
798 <pre>$ hg pull https://bitbucket.org/ArneBab/hello/
799
800 </pre>
801
802 <p>People who join you in development can also just <em>clone</em> this repository, as if one of you were using <em>hg serve</em></p>
803
804 <pre>$ hg clone https://bitbucket.org/ArneBab/hello/ hello
805
806 </pre>
807
808 <p>That local repository will automatically be configured to pull/push from/to the online repository, so new contributors can just use <em>hg push</em> and <em>hg pull</em> without an URL.</p>
809
810 <div class="note">
811 <p class="note-title">Note:</p>
812 To make this workflow more scalable, each one of you can have his own BitBucket repository and you can simply <em>pull</em> from the others repositories. That way you can easily establish workflows in which certain people act as integrators and finally <em>push</em> checked code to a shared pull repository from which all others pull.
813 </div>
814
815 <div class="note">
816 <p class="note-title">Note:</p>
817 You can also use this workflow with a shared server instead of BitBucket, either via SSH or via a shared directory. An example for an SSH URL with Mercurial is be ssh://user@example.com/path/to/repo. When using a shared directory you just push as if the repository in the shared directory were on your local drive.
818 </div>
819
820 <h2 id="basic_summary">Summary</h2>
821
822 <p>Now let's take a step back and look where we are.</p>
823
824 <p>With the commands you already know, a bit reading of <em>hg help &lt;command&gt;</em> and some evil script-fu you can already do almost everything you'll ever need to do when working with source code history. So from now on almost everything is convenience, and that's a good thing.</p>
825
826 <p>First this is good, because it means, that you can now use most of the concepts which are utilized in more complex workflows.</p>
827
828 <p>Second it aids you, because convenience lets you focus on your task instead of focusing on your tool. It helps you concentrate on the coding itself. Still you can always go back to the basics, if you want to.</p>
829
830 <p>A short summary of what you can do which can also act as a short check, if you still remember the meaning of the commands.</p>
831
832 <h3>create a project</h3>
833
834 <pre>$ hg init project
835 $ cd project
836 $ (add some files)
837 $ hg add
838 $ hg commit
839 (enter the commit message)
840
841 </pre>
842
843 <h3>do nonlinear development</h3>
844
845 <pre>$ (do some changes)
846 $ hg commit
847 (enter the commit message)
848 $ hg update 0
849 $ (do some changes)
850 $ hg commit
851 (enter the commit message)
852 $ hg merge
853 $ (optionally hg resolve)
854 $ hg commit
855 (enter the commit message)
856
857 </pre>
858
859 <h3>use feature clones</h3>
860
861 <pre>$ cd ..
862 $ hg clone project feature1
863 $ cd feature1
864 $ (do some changes)
865 $ hg commit
866 (enter the commit message)
867 $ cd ../project
868 $ hg pull ../feature1
869
870 </pre>
871
872 <h3>share your repository via the integrated webserver</h3>
873
874 <pre>$ hg serve &amp;
875 $ cd ..
876 $ hg clone http://127.0.0.1:8000 project-clone
877
878 </pre>
879
880 <h3>export changes to files</h3>
881
882 <pre>$ cd project-clone
883 $ (do some changes)
884 $ hg commit
885 (enter the commit message)
886 $ hg export tip > ../changes.diff
887
888 </pre>
889
890 <h3>import changes from files</h3>
891
892 <pre>$ cd ../project
893 $ hg import ../changes.diff
894
895 </pre>
896
897 <h3>pull changes from a served repository (hg serve still runs on project)</h3>
898
899 <pre>$ cd ../feature1
900 $ hg pull http://127.0.0.1:8000
901
902 </pre>
903
904 <h3>Use shared repositories on BitBucket</h3>
905
906 <pre>$ (setup bitbucket repo)
907 $ hg push https://bitbucket.org/USER/REPO
908 (enter name and password in the prompt)
909 $ hg pull https://bitbucket.org/USER/REPO
910
911 </pre>
912
913
914 <p>Let's move on towards useful features and a bit more advanced workflows.</p>
915
916 <h1 id="advanced_workflows">Advanced workflows</h1>
917
918 <h2 id="backing_out">Backing out bad revisions</h2>
919
920 <h3>Use Case</h3>
921
922 <p>When you routinely pull code from others, it can happen that you overlook some bad change. As soon as others pull that change from you, you have little chance to get completely rid of it.</p>
923
924 <p>To resolve that fundamental problem in any really distributed system, Mercurial offers you the <em>backout</em> command. Backing out a change means, that you tell Mercurial to <strong>create a commit which reverses the bad change</strong>. That way you don't get rid of the bad code in history, but you can remove it from new revisions.</p>
925
926 <div class="note">
927 <p class="note-title">Note:</p>
928 The basic commands don't rewrite history. If you want to do that, you need to activate some of the extensions which are shipped with mercurial. We'll come to that <a title="Removing History" href="#removing_history">later on</a>.
929 </div>
930
931 <h3>Workflow</h3>
932
933 <p>Let's assume the bad change was revision 3, and you already have one more revision in your
934 repository. To remove the bad code, you can just <em>backout</em> of it. This creates a new
935 change which reverses the bad change. After backing out, you can then merge that new change
936 into the current code.</p>
937
938 <pre>$ hg backout 3
939 $ hg merge
940 (potentially resolve conflicts)
941 $ hg commit
942 (enter commit message. For example: "merged backout")
943
944 </pre>
945
946 <p>That's it. You reversed the bad change. It's still recorded that it was once there (following the principle "don't rewrite history, if it's not really necessary"), but it doesn't affect future code anymore.</p>
947
948 <h2 id="collaborative_development">Collaborative feature development</h2>
949
950 <p>Now that you can share changes and reverse them if necessary, you can go one step further: Using Mercurial to help in <strong>coordinating the coding</strong>.</p>
951
952 <p>The first part is an easy way to develop features together, without requiring every developer to keep track of several feature clones.</p>
953
954 <h3>Use Case</h3>
955
956 <p>When you want to split your development into several features, you need to keep track of who works on which feature and where to get which changes.</p>
957
958 <p>Mercurial makes this easy for you by providing named branches. They are a part of the main repository, so they are available to everyone involved. At the same time, changes committed on a certain branch don't get mixed with the changes in the default branch, so features are kept separate, until they get merged into the default branch.</p>
959
960 <div class="note">
961 <p class="note-title">Note:</p>
962 Cloning a repository always puts you onto the default branch at first.
963 </div>
964
965 <h3>Workflow</h3>
966
967 <p>When someone in your group wants to start coding on a feature without disturbing the others, he can create a named branch and commit there. When someone else wants to join in, he just updates to the branch and commits away. As soon as the feature is finished, someone merges the named branch into the default branch.</p>
968
969 <div class="note">
970 <p class="note-title">Note:</p>
971 Named branch information is <strong>permanently stored in history</strong>, so you can always see for which feature some change was added. If you only want temporary branches as short-lived markers on history, you can use Bookmarks instead. Just replace <em>hg branch</em> with <em>hg bookmark</em> and add <em>-B &lt;bookmark-name&gt;</em> to <em>hg push</em> and <em>hg pull</em>.
972 </div>
973
974 <h5>Working in a named branch</h5>
975
976 <p>Create the branch</p>
977
978 <pre>$ hg branch feature1
979 (do some changes)
980 $ hg commit
981 (write commit message)
982
983 </pre>
984
985 <p>Update into the branch and work in it</p>
986
987 <pre>$ hg update feature1
988 (do some changes)
989 $ hg commit
990 (write commit message)
991
992 </pre>
993
994 <p>Now you can <em>commit</em>, <em>pull</em>, <em>push</em> and <em>merge</em> (and anything else) as if you were working in a separate repository. If the history of the named branch is linear and you call "hg merge", Mercurial asks you to specify an explicit revision, since the branch in which you work doesn't have anything to merge.</p>
995
996 <h5>Merge the named branch</h5>
997
998 <p>When you finished the feature, you <em>merge</em> the branch back into the default branch.</p>
999
1000 <pre>$ hg update default
1001 $ hg merge feature1
1002 $ hg commit
1003 (write commit message)
1004
1005 </pre>
1006
1007 <p>To see the active branches with <em>hg branches</em>. To mark a branch as inactive, for example because you finished implementing the feature, you can close it. Closed branches are hidden in <em>hg branches</em> as well as in <em>hg heads</em>.</pre>
1008
1009 <pre>$ hg update feature1
1010 $ hg commit --close-branch -m "finished feature1"
1011
1012 </pre>
1013
1014 <p>And that's it. Now you can easily keep features separate without unnecessary bookkeeping.</p>
1015
1016 <h2 id="tagging">Tagging revisions</h2>
1017
1018 <h3>Use Case</h3>
1019
1020 <p>Since you can now code separate features more easily, you might want to mark certain revisions as fit for consumption (or similar). For example you might want to <strong>mark releases, or just mark off revisions as reviewed</strong>.</p>
1021
1022 <p>For this Mercurial offers tags. Tags add a name to a revision and are part of the history. You can tag a change years after it was committed. The tag includes the information when it was added, and tags can be pulled, pushed and merged just like any other committed change.</p>
1023
1024 <div class="note">
1025 <p class="note-title">Note:</p>
1026 A tag must not contain the char ":", since that char is used for specifying multiple revisions - see "hg help revisions".
1027 </div>
1028
1029 <div class="note">
1030 <p class="note-title">Note:</p>
1031 To securely mark a revision, you can use the <a title="Using GnuPG to securely sign revisions in Mercurial" href="http://www.selenic.com/mercurial/wiki/GpgExtension">gpg extension</a> to sign the tag.
1032 </div>
1033
1034 <h3>Workflow</h3>
1035
1036 <p>Let's assume you want to give revision 3 the name "v0.1".</p>
1037
1038 <p>Add the tag</p>
1039
1040 <pre>$ hg tag -r 3 v0.1
1041
1042 </pre>
1043
1044 <p>See all tags</p>
1045
1046 <pre>$ hg tags
1047
1048 </pre>
1049
1050 <p>When you look at the log you'll now see a line in changeset 3 which marks the Tag. If someone wants to <em>update</em> to the tagged revision, he can just use the name of your tag</p>
1051
1052 <pre>$ hg update v0.1
1053
1054 </pre>
1055
1056 <p>Now he'll be at the tagged revision and can work from there.</p>
1057
1058
1059 <h2 id="removing_history">Removing history</h2>
1060
1061 <h3>Use Case</h3>
1062
1063 <p>At times you will have changes in your repository, which you really don't want in it.</p>
1064
1065 <p>There are many advanced options for removing these, and most of them use great extensions (<a title="Mercurial Queues Extension" href="http://www.selenic.com/mercurial/wiki/MqExtension">Mercurial Queues</a> is the most often used one), but in this basic guide, we'll solve the problem with just the commands we already learned. But we'll use an option to clone which we didn't yet need.</p>
1066
1067 <p>This workflow becomes inconvenient when you need to remove changes, which are buried below many new changes. If you spot the them early enough, you can <strong>get rid of bad changes</strong> without too much effort, though.</p>
1068
1069 <h3>Workflow</h3>
1070
1071 <p>Let's assume you want to get rid of revision 2 and the highest revision is 3.</p>
1072
1073 <p>The first step is to use the "--rev" option to <em>clone</em>: Create a clone which only contains the changes up to the specified revision. Since you want to keep revision 1, you only clone up to that</p>
1074
1075 <pre>$ hg clone -r 1 project stripped
1076
1077 </pre>
1078
1079 <p>Now you can <em>export</em> the change 3 from the original repository (project) and <em>import</em> it into the stripped one</p>
1080
1081 <pre>$ cd project
1082 $ hg export 3 > ../changes.diff
1083 $ cd ../stripped
1084 $ hg import ../changes.diff
1085
1086 </pre>
1087
1088 <p>If a part of the changes couldn't be applied, you'll see that part in *.rej files. If you have *.rej files, you'll have to include or discard changes by hand</p>
1089
1090 <pre>$ cat *.rej
1091 (apply changes by hand)
1092 $ hg commit
1093 (write commit message)
1094
1095 </pre>
1096
1097 <p>That's it. <em>hg export</em> also includes the commit message, date, committer and similar metadata, so you are already done.</p>
1098
1099 <div class="note">
1100 <p class="note-title">Note:</p>
1101 Removing history will change the revision IDs of revisions after the removed one, and if you pull from someone else who still has the revision you removed, you will pull the removed parts again. That's why rewriting history should most times only be done for changes which you didn't yet publicise.
1102 </div>
1103
1104 <h2 id="advanced_summary">Summary</h2>
1105
1106 <p>So now you can work with Mercurial in private, and also share your changes in a multitude of ways.</p>
1107
1108 <p>Additionally you can remove bad changes, either by creating a change in the repository which reverses the original change, or by really rewriting history, so it looks like the change never occurred.</p>
1109
1110 <p>And you can separate the work on features in a single repository by using named branches and add tags to revisions which are visible markers for others and can be used to update to the tagged revisions.</p>
1111
1112 <p>With this we can conclude our practical guide.</p>
1113
1114 <h1 id="complex_workflows">More Complex Workflows</h1>
1115
1116 <p>If you now want to check some more complex workflows, please have a look at the general <a title="Mercurial Workflows" href="http://selenic.com/mercurial/wiki/Workflows">workflows wikipage</a> and the list of <a title="Mercurial extensions" href="http://mercurial.selenic.com/wiki/UsingExtensions">extensions</a>.</p>
1117
1118 <p>To deepen your understanding, you can also check the <a title="Overview of the basic concepts of Mercurial" href="http://mercurial.selenic.com/wiki/UnderstandingMercurial">basic concept overview</a>.</p>
1119
1120 <p>Have fun with Mercurial!</p>
1121
1122
1123
1124 {% endblock %}
1125 {% block sidebar %}
1126 <h1>Index</h1>
1127 <ul class="sidebar-toc">
1128 <li><a href="#basic_workflow">Basic workflows</a></li>
1129 <ul>
1130 <li><a href="#log_keeping">Log keeping</a></li>
1131 <li><a href="#lone_developer">Lone developer with nonlinear history</a></li>
1132 <li><a href="#separate_features">Separate features</a></li>
1133 <li><a href="#sharing_changes">Sharing changes</a></li>
1134 <li><a href="#basic_summary">Summary</a></li>
1135 </ul>
1136 <li><a href="#advanced_workflows">Advanced workflows</a></li>
1137 <ul>
1138 <li><a href="#backing_out">Backing out bad revisions</a></li>
1139 <li><a href="#collaborative_development">Collaborative feature development</a></li>
1140 <li><a href="#tagging">Tagging revisions</a></li>
1141 <li><a href="#removing_history">Removing history</a></li>
1142 <li><a href="#advanced_summary">Summary</a></li>
1143 </ul>
1144 <li><a href="#complex_workflows">More complex workflows</a></li>
1145 </ul>
1146
1147 </div>
1148 </div>
1149
1150 {% endblock %}