Mercurial > hgbook
comparison en/ch04-daily.xml @ 749:7e7c47481e4f
Oops, this is the real merge for my hg's oddity
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Fri, 20 Mar 2009 16:43:35 +0800 |
parents | en/ch05-daily.xml@cfdb601a3c8b |
children | 1c13ed2130a7 |
comparison
equal
deleted
inserted
replaced
748:d13c7c706a58 | 749:7e7c47481e4f |
---|---|
1 <!-- vim: set filetype=docbkxml shiftwidth=2 autoindent expandtab tw=77 : --> | |
2 | |
3 <chapter id="chap.daily"> | |
4 <?dbhtml filename="mercurial-in-daily-use.html"?> | |
5 <title>Mercurial in daily use</title> | |
6 | |
7 <sect1> | |
8 <title>Telling Mercurial which files to track</title> | |
9 | |
10 <para id="x_1a3">Mercurial does not work with files in your repository unless | |
11 you tell it to manage them. The <command role="hg-cmd">hg | |
12 status</command> command will tell you which files Mercurial | |
13 doesn't know about; it uses a | |
14 <quote><literal>?</literal></quote> to display such | |
15 files.</para> | |
16 | |
17 <para id="x_1a4">To tell Mercurial to track a file, use the <command | |
18 role="hg-cmd">hg add</command> command. Once you have added a | |
19 file, the entry in the output of <command role="hg-cmd">hg | |
20 status</command> for that file changes from | |
21 <quote><literal>?</literal></quote> to | |
22 <quote><literal>A</literal></quote>.</para> | |
23 | |
24 &interaction.daily.files.add; | |
25 | |
26 <para id="x_1a5">After you run a <command role="hg-cmd">hg commit</command>, | |
27 the files that you added before the commit will no longer be | |
28 listed in the output of <command role="hg-cmd">hg | |
29 status</command>. The reason for this is that <command | |
30 role="hg-cmd">hg status</command> only tells you about | |
31 <quote>interesting</quote> files&emdash;those that you have | |
32 modified or told Mercurial to do something with&emdash;by | |
33 default. If you have a repository that contains thousands of | |
34 files, you will rarely want to know about files that Mercurial | |
35 is tracking, but that have not changed. (You can still get this | |
36 information; we'll return to this later.)</para> | |
37 | |
38 <para id="x_1a6">Once you add a file, Mercurial doesn't do anything with it | |
39 immediately. Instead, it will take a snapshot of the file's | |
40 state the next time you perform a commit. It will then continue | |
41 to track the changes you make to the file every time you commit, | |
42 until you remove the file.</para> | |
43 | |
44 <sect2> | |
45 <title>Explicit versus implicit file naming</title> | |
46 | |
47 <para id="x_1a7">A useful behaviour that Mercurial has is that if you pass | |
48 the name of a directory to a command, every Mercurial command | |
49 will treat this as <quote>I want to operate on every file in | |
50 this directory and its subdirectories</quote>.</para> | |
51 | |
52 &interaction.daily.files.add-dir; | |
53 | |
54 <para id="x_1a8">Notice in this example that Mercurial printed the names of | |
55 the files it added, whereas it didn't do so when we added the | |
56 file named <filename>a</filename> in the earlier | |
57 example.</para> | |
58 | |
59 <para id="x_1a9">What's going on is that in the former case, we explicitly | |
60 named the file to add on the command line, so the assumption | |
61 that Mercurial makes in such cases is that you know what you | |
62 were doing, and it doesn't print any output.</para> | |
63 | |
64 <para id="x_1aa">However, when we <emphasis>imply</emphasis> the names of | |
65 files by giving the name of a directory, Mercurial takes the | |
66 extra step of printing the name of each file that it does | |
67 something with. This makes it more clear what is happening, | |
68 and reduces the likelihood of a silent and nasty surprise. | |
69 This behaviour is common to most Mercurial commands.</para> | |
70 | |
71 </sect2> | |
72 <sect2> | |
73 <title>Aside: Mercurial tracks files, not directories</title> | |
74 | |
75 <para id="x_1ab">Mercurial does not track directory information. Instead, | |
76 it tracks the path to a file. Before creating a file, it | |
77 first creates any missing directory components of the path. | |
78 After it deletes a file, it then deletes any empty directories | |
79 that were in the deleted file's path. This sounds like a | |
80 trivial distinction, but it has one minor practical | |
81 consequence: it is not possible to represent a completely | |
82 empty directory in Mercurial.</para> | |
83 | |
84 <para id="x_1ac">Empty directories are rarely useful, and there are | |
85 unintrusive workarounds that you can use to achieve an | |
86 appropriate effect. The developers of Mercurial thus felt | |
87 that the complexity that would be required to manage empty | |
88 directories was not worth the limited benefit this feature | |
89 would bring.</para> | |
90 | |
91 <para id="x_1ad">If you need an empty directory in your repository, there | |
92 are a few ways to achieve this. One is to create a directory, | |
93 then <command role="hg-cmd">hg add</command> a | |
94 <quote>hidden</quote> file to that directory. On Unix-like | |
95 systems, any file name that begins with a period | |
96 (<quote><literal>.</literal></quote>) is treated as hidden by | |
97 most commands and GUI tools. This approach is illustrated | |
98 below.</para> | |
99 | |
100 &interaction.daily.files.hidden; | |
101 | |
102 <para id="x_1ae">Another way to tackle a need for an empty directory is to | |
103 simply create one in your automated build scripts before they | |
104 will need it.</para> | |
105 | |
106 </sect2> | |
107 </sect1> | |
108 <sect1> | |
109 <title>How to stop tracking a file</title> | |
110 | |
111 <para id="x_1af">Once you decide that a file no longer belongs in your | |
112 repository, use the <command role="hg-cmd">hg remove</command> | |
113 command; this deletes the file, and tells Mercurial to stop | |
114 tracking it. A removed file is represented in the output of | |
115 <command role="hg-cmd">hg status</command> with a | |
116 <quote><literal>R</literal></quote>.</para> | |
117 | |
118 &interaction.daily.files.remove; | |
119 | |
120 <para id="x_1b0">After you <command role="hg-cmd">hg remove</command> a file, | |
121 Mercurial will no longer track changes to that file, even if you | |
122 recreate a file with the same name in your working directory. | |
123 If you do recreate a file with the same name and want Mercurial | |
124 to track the new file, simply <command role="hg-cmd">hg | |
125 add</command> it. Mercurial will know that the newly added | |
126 file is not related to the old file of the same name.</para> | |
127 | |
128 <sect2> | |
129 <title>Removing a file does not affect its history</title> | |
130 | |
131 <para id="x_1b1">It is important to understand that removing a file has | |
132 only two effects.</para> | |
133 <itemizedlist> | |
134 <listitem><para id="x_1b2">It removes the current version of the file | |
135 from the working directory.</para> | |
136 </listitem> | |
137 <listitem><para id="x_1b3">It stops Mercurial from tracking changes to | |
138 the file, from the time of the next commit.</para> | |
139 </listitem></itemizedlist> | |
140 <para id="x_1b4">Removing a file <emphasis>does not</emphasis> in any way | |
141 alter the <emphasis>history</emphasis> of the file.</para> | |
142 | |
143 <para id="x_1b5">If you update the working directory to a changeset in | |
144 which a file that you have removed was still tracked, it will | |
145 reappear in the working directory, with the contents it had | |
146 when you committed that changeset. If you then update the | |
147 working directory to a later changeset, in which the file had | |
148 been removed, Mercurial will once again remove the file from | |
149 the working directory.</para> | |
150 | |
151 </sect2> | |
152 <sect2> | |
153 <title>Missing files</title> | |
154 | |
155 <para id="x_1b6">Mercurial considers a file that you have deleted, but not | |
156 used <command role="hg-cmd">hg remove</command> to delete, to | |
157 be <emphasis>missing</emphasis>. A missing file is | |
158 represented with <quote><literal>!</literal></quote> in the | |
159 output of <command role="hg-cmd">hg status</command>. | |
160 Mercurial commands will not generally do anything with missing | |
161 files.</para> | |
162 | |
163 &interaction.daily.files.missing; | |
164 | |
165 <para id="x_1b7">If your repository contains a file that <command | |
166 role="hg-cmd">hg status</command> reports as missing, and | |
167 you want the file to stay gone, you can run <command | |
168 role="hg-cmd">hg remove <option | |
169 role="hg-opt-remove">--after</option></command> at any | |
170 time later on, to tell Mercurial that you really did mean to | |
171 remove the file.</para> | |
172 | |
173 &interaction.daily.files.remove-after; | |
174 | |
175 <para id="x_1b8">On the other hand, if you deleted the missing file by | |
176 accident, give <command role="hg-cmd">hg revert</command> the | |
177 name of the file to recover. It will reappear, in unmodified | |
178 form.</para> | |
179 | |
180 &interaction.daily.files.recover-missing; | |
181 | |
182 </sect2> | |
183 <sect2> | |
184 <title>Aside: why tell Mercurial explicitly to remove a | |
185 file?</title> | |
186 | |
187 <para id="x_1b9">You might wonder why Mercurial requires you to explicitly | |
188 tell it that you are deleting a file. Early during the | |
189 development of Mercurial, it let you delete a file however you | |
190 pleased; Mercurial would notice the absence of the file | |
191 automatically when you next ran a <command role="hg-cmd">hg | |
192 commit</command>, and stop tracking the file. In practice, | |
193 this made it too easy to accidentally remove a file without | |
194 noticing.</para> | |
195 | |
196 </sect2> | |
197 <sect2> | |
198 <title>Useful shorthand&emdash;adding and removing files in one | |
199 step</title> | |
200 | |
201 <para id="x_1ba">Mercurial offers a combination command, <command | |
202 role="hg-cmd">hg addremove</command>, that adds untracked | |
203 files and marks missing files as removed.</para> | |
204 | |
205 &interaction.daily.files.addremove; | |
206 | |
207 <para id="x_1bb">The <command role="hg-cmd">hg commit</command> command | |
208 also provides a <option role="hg-opt-commit">-A</option> | |
209 option that performs this same add-and-remove, immediately | |
210 followed by a commit.</para> | |
211 | |
212 &interaction.daily.files.commit-addremove; | |
213 | |
214 </sect2> | |
215 </sect1> | |
216 <sect1> | |
217 <title>Copying files</title> | |
218 | |
219 <para id="x_1bc">Mercurial provides a <command role="hg-cmd">hg | |
220 copy</command> command that lets you make a new copy of a | |
221 file. When you copy a file using this command, Mercurial makes | |
222 a record of the fact that the new file is a copy of the original | |
223 file. It treats these copied files specially when you merge | |
224 your work with someone else's.</para> | |
225 | |
226 <sect2> | |
227 <title>The results of copying during a merge</title> | |
228 | |
229 <para id="x_1bd">What happens during a merge is that changes | |
230 <quote>follow</quote> a copy. To best illustrate what this | |
231 means, let's create an example. We'll start with the usual | |
232 tiny repository that contains a single file.</para> | |
233 | |
234 &interaction.daily.copy.init; | |
235 | |
236 <para id="x_1be">We need to do some work in | |
237 parallel, so that we'll have something to merge. So let's | |
238 clone our repository.</para> | |
239 | |
240 &interaction.daily.copy.clone; | |
241 | |
242 <para id="x_1bf">Back in our initial repository, let's use the <command | |
243 role="hg-cmd">hg copy</command> command to make a copy of | |
244 the first file we created.</para> | |
245 | |
246 &interaction.daily.copy.copy; | |
247 | |
248 <para id="x_1c0">If we look at the output of the <command role="hg-cmd">hg | |
249 status</command> command afterwards, the copied file looks | |
250 just like a normal added file.</para> | |
251 | |
252 &interaction.daily.copy.status; | |
253 | |
254 <para id="x_1c1">But if we pass the <option | |
255 role="hg-opt-status">-C</option> option to <command | |
256 role="hg-cmd">hg status</command>, it prints another line of | |
257 output: this is the file that our newly-added file was copied | |
258 <emphasis>from</emphasis>.</para> | |
259 | |
260 &interaction.daily.copy.status-copy; | |
261 | |
262 <para id="x_1c2">Now, back in the repository we cloned, let's make a change | |
263 in parallel. We'll add a line of content to the original file | |
264 that we created.</para> | |
265 | |
266 &interaction.daily.copy.other; | |
267 | |
268 <para id="x_1c3">Now we have a modified <filename>file</filename> in this | |
269 repository. When we pull the changes from the first | |
270 repository, and merge the two heads, Mercurial will propagate | |
271 the changes that we made locally to <filename>file</filename> | |
272 into its copy, <filename>new-file</filename>.</para> | |
273 | |
274 &interaction.daily.copy.merge; | |
275 | |
276 </sect2> | |
277 <sect2 id="sec.daily.why-copy"> | |
278 <title>Why should changes follow copies?</title> | |
279 | |
280 <para id="x_1c4">This behaviour, of changes to a file propagating out to | |
281 copies of the file, might seem esoteric, but in most cases | |
282 it's highly desirable.</para> | |
283 | |
284 <para id="x_1c5">First of all, remember that this propagation | |
285 <emphasis>only</emphasis> happens when you merge. So if you | |
286 <command role="hg-cmd">hg copy</command> a file, and | |
287 subsequently modify the original file during the normal course | |
288 of your work, nothing will happen.</para> | |
289 | |
290 <para id="x_1c6">The second thing to know is that modifications will only | |
291 propagate across a copy as long as the repository that you're | |
292 pulling changes from <emphasis>doesn't know</emphasis> about | |
293 the copy.</para> | |
294 | |
295 <para id="x_1c7">The reason that Mercurial does this is as follows. Let's | |
296 say I make an important bug fix in a source file, and commit | |
297 my changes. Meanwhile, you've decided to <command | |
298 role="hg-cmd">hg copy</command> the file in your repository, | |
299 without knowing about the bug or having seen the fix, and you | |
300 have started hacking on your copy of the file.</para> | |
301 | |
302 <para id="x_1c8">If you pulled and merged my changes, and Mercurial | |
303 <emphasis>didn't</emphasis> propagate changes across copies, | |
304 your source file would now contain the bug, and unless you | |
305 remembered to propagate the bug fix by hand, the bug would | |
306 <emphasis>remain</emphasis> in your copy of the file.</para> | |
307 | |
308 <para id="x_1c9">By automatically propagating the change that fixed the bug | |
309 from the original file to the copy, Mercurial prevents this | |
310 class of problem. To my knowledge, Mercurial is the | |
311 <emphasis>only</emphasis> revision control system that | |
312 propagates changes across copies like this.</para> | |
313 | |
314 <para id="x_1ca">Once your change history has a record that the copy and | |
315 subsequent merge occurred, there's usually no further need to | |
316 propagate changes from the original file to the copied file, | |
317 and that's why Mercurial only propagates changes across copies | |
318 until this point, and no further.</para> | |
319 | |
320 </sect2> | |
321 <sect2> | |
322 <title>How to make changes <emphasis>not</emphasis> follow a | |
323 copy</title> | |
324 | |
325 <para id="x_1cb">If, for some reason, you decide that this business of | |
326 automatically propagating changes across copies is not for | |
327 you, simply use your system's normal file copy command (on | |
328 Unix-like systems, that's <command>cp</command>) to make a | |
329 copy of a file, then <command role="hg-cmd">hg add</command> | |
330 the new copy by hand. Before you do so, though, please do | |
331 reread section <xref linkend="sec.daily.why-copy"/>, and make | |
332 an informed | |
333 decision that this behaviour is not appropriate to your | |
334 specific case.</para> | |
335 | |
336 </sect2> | |
337 <sect2> | |
338 <title>Behaviour of the <command role="hg-cmd">hg copy</command> | |
339 command</title> | |
340 | |
341 <para id="x_1cc">When you use the <command role="hg-cmd">hg copy</command> | |
342 command, Mercurial makes a copy of each source file as it | |
343 currently stands in the working directory. This means that if | |
344 you make some modifications to a file, then <command | |
345 role="hg-cmd">hg copy</command> it without first having | |
346 committed those changes, the new copy will also contain the | |
347 modifications you have made up until that point. (I find this | |
348 behaviour a little counterintuitive, which is why I mention it | |
349 here.)</para> | |
350 | |
351 <para id="x_1cd">The <command role="hg-cmd">hg copy</command> command acts | |
352 similarly to the Unix <command>cp</command> command (you can | |
353 use the <command role="hg-cmd">hg cp</command> alias if you | |
354 prefer). The last argument is the | |
355 <emphasis>destination</emphasis>, and all prior arguments are | |
356 <emphasis>sources</emphasis>. If you pass it a single file as | |
357 the source, and the destination does not exist, it creates a | |
358 new file with that name.</para> | |
359 | |
360 &interaction.daily.copy.simple; | |
361 | |
362 <para id="x_1ce">If the destination is a directory, Mercurial copies its | |
363 sources into that directory.</para> | |
364 | |
365 &interaction.daily.copy.dir-dest; | |
366 | |
367 <para id="x_1cf">Copying a directory is | |
368 recursive, and preserves the directory structure of the | |
369 source.</para> | |
370 | |
371 &interaction.daily.copy.dir-src; | |
372 | |
373 <para id="x_1d0">If the source and destination are both directories, the | |
374 source tree is recreated in the destination directory.</para> | |
375 | |
376 &interaction.daily.copy.dir-src-dest; | |
377 | |
378 <para id="x_1d1">As with the <command role="hg-cmd">hg rename</command> | |
379 command, if you copy a file manually and then want Mercurial | |
380 to know that you've copied the file, simply use the <option | |
381 role="hg-opt-copy">--after</option> option to <command | |
382 role="hg-cmd">hg copy</command>.</para> | |
383 | |
384 &interaction.daily.copy.after; | |
385 | |
386 </sect2> | |
387 </sect1> | |
388 <sect1> | |
389 <title>Renaming files</title> | |
390 | |
391 <para id="x_1d2">It's rather more common to need to rename a file than to | |
392 make a copy of it. The reason I discussed the <command | |
393 role="hg-cmd">hg copy</command> command before talking about | |
394 renaming files is that Mercurial treats a rename in essentially | |
395 the same way as a copy. Therefore, knowing what Mercurial does | |
396 when you copy a file tells you what to expect when you rename a | |
397 file.</para> | |
398 | |
399 <para id="x_1d3">When you use the <command role="hg-cmd">hg rename</command> | |
400 command, Mercurial makes a copy of each source file, then | |
401 deletes it and marks the file as removed.</para> | |
402 | |
403 &interaction.daily.rename.rename; | |
404 | |
405 <para id="x_1d4">The <command role="hg-cmd">hg status</command> command shows | |
406 the newly copied file as added, and the copied-from file as | |
407 removed.</para> | |
408 | |
409 &interaction.daily.rename.status; | |
410 | |
411 <para id="x_1d5">As with the results of a <command role="hg-cmd">hg | |
412 copy</command>, we must use the <option | |
413 role="hg-opt-status">-C</option> option to <command | |
414 role="hg-cmd">hg status</command> to see that the added file | |
415 is really being tracked by Mercurial as a copy of the original, | |
416 now removed, file.</para> | |
417 | |
418 &interaction.daily.rename.status-copy; | |
419 | |
420 <para id="x_1d6">As with <command role="hg-cmd">hg remove</command> and | |
421 <command role="hg-cmd">hg copy</command>, you can tell Mercurial | |
422 about a rename after the fact using the <option | |
423 role="hg-opt-rename">--after</option> option. In most other | |
424 respects, the behaviour of the <command role="hg-cmd">hg | |
425 rename</command> command, and the options it accepts, are | |
426 similar to the <command role="hg-cmd">hg copy</command> | |
427 command.</para> | |
428 | |
429 <sect2> | |
430 <title>Renaming files and merging changes</title> | |
431 | |
432 <para id="x_1d7">Since Mercurial's rename is implemented as | |
433 copy-and-remove, the same propagation of changes happens when | |
434 you merge after a rename as after a copy.</para> | |
435 | |
436 <para id="x_1d8">If I modify a file, and you rename it to a new name, and | |
437 then we merge our respective changes, my modifications to the | |
438 file under its original name will be propagated into the file | |
439 under its new name. (This is something you might expect to | |
440 <quote>simply work,</quote> but not all revision control | |
441 systems actually do this.)</para> | |
442 | |
443 <para id="x_1d9">Whereas having changes follow a copy is a feature where | |
444 you can perhaps nod and say <quote>yes, that might be | |
445 useful,</quote> it should be clear that having them follow a | |
446 rename is definitely important. Without this facility, it | |
447 would simply be too easy for changes to become orphaned when | |
448 files are renamed.</para> | |
449 | |
450 </sect2> | |
451 <sect2> | |
452 <title>Divergent renames and merging</title> | |
453 | |
454 <para id="x_1da">The case of diverging names occurs when two developers | |
455 start with a file&emdash;let's call it | |
456 <filename>foo</filename>&emdash;in their respective | |
457 repositories.</para> | |
458 | |
459 &interaction.rename.divergent.clone; | |
460 | |
461 <para id="x_1db">Anne renames the file to <filename>bar</filename>.</para> | |
462 | |
463 &interaction.rename.divergent.rename.anne; | |
464 | |
465 <para id="x_1dc">Meanwhile, Bob renames it to | |
466 <filename>quux</filename>.</para> | |
467 | |
468 &interaction.rename.divergent.rename.bob; | |
469 | |
470 <para id="x_1dd">I like to think of this as a conflict because each | |
471 developer has expressed different intentions about what the | |
472 file ought to be named.</para> | |
473 | |
474 <para id="x_1de">What do you think should happen when they merge their | |
475 work? Mercurial's actual behaviour is that it always preserves | |
476 <emphasis>both</emphasis> names when it merges changesets that | |
477 contain divergent renames.</para> | |
478 | |
479 &interaction.rename.divergent.merge; | |
480 | |
481 <para id="x_1df">Notice that Mercurial does warn about the divergent | |
482 renames, but it leaves it up to you to do something about the | |
483 divergence after the merge.</para> | |
484 | |
485 </sect2> | |
486 <sect2> | |
487 <title>Convergent renames and merging</title> | |
488 | |
489 <para id="x_1e0">Another kind of rename conflict occurs when two people | |
490 choose to rename different <emphasis>source</emphasis> files | |
491 to the same <emphasis>destination</emphasis>. In this case, | |
492 Mercurial runs its normal merge machinery, and lets you guide | |
493 it to a suitable resolution.</para> | |
494 | |
495 </sect2> | |
496 <sect2> | |
497 <title>Other name-related corner cases</title> | |
498 | |
499 <para id="x_1e1">Mercurial has a longstanding bug in which it fails to | |
500 handle a merge where one side has a file with a given name, | |
501 while another has a directory with the same name. This is | |
502 documented as <ulink role="hg-bug" | |
503 url="http://www.selenic.com/mercurial/bts/issue29">issue | |
504 29</ulink>.</para> | |
505 | |
506 &interaction.issue29.go; | |
507 | |
508 </sect2> | |
509 </sect1> | |
510 <sect1> | |
511 <title>Recovering from mistakes</title> | |
512 | |
513 <para id="x_1e2">Mercurial has some useful commands that will help you to | |
514 recover from some common mistakes.</para> | |
515 | |
516 <para id="x_1e3">The <command role="hg-cmd">hg revert</command> command lets | |
517 you undo changes that you have made to your working directory. | |
518 For example, if you <command role="hg-cmd">hg add</command> a | |
519 file by accident, just run <command role="hg-cmd">hg | |
520 revert</command> with the name of the file you added, and | |
521 while the file won't be touched in any way, it won't be tracked | |
522 for adding by Mercurial any longer, either. You can also use | |
523 <command role="hg-cmd">hg revert</command> to get rid of | |
524 erroneous changes to a file.</para> | |
525 | |
526 <para id="x_1e4">It's useful to remember that the <command role="hg-cmd">hg | |
527 revert</command> command is useful for changes that you have | |
528 not yet committed. Once you've committed a change, if you | |
529 decide it was a mistake, you can still do something about it, | |
530 though your options may be more limited.</para> | |
531 | |
532 <para id="x_1e5">For more information about the <command role="hg-cmd">hg | |
533 revert</command> command, and details about how to deal with | |
534 changes you have already committed, see chapter <xref | |
535 linkend="chap.undo"/>.</para> | |
536 | |
537 </sect1> | |
538 </chapter> | |
539 | |
540 <!-- | |
541 local variables: | |
542 sgml-parent-document: ("00book.xml" "book" "chapter") | |
543 end: | |
544 --> |