Kiln » HgInit » HgInit
Clone URL:  
00.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>HgInit: Subversion Re-education</title> <script src="c/jquery-1.4.1.js"></script> <script src="c/basic.js"></script> <link href="c/styles.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> try { var pageTracker = _gat._getTracker("UA-225715-20"); pageTracker._setDomainName("none"); pageTracker._setAllowLinker(true); pageTracker._trackPageview(); } catch(err) {}</script> <!-- Google Website Optimizer Control Script --> <script> function utmx_section(){}function utmx(){} (function(){var k='2687020024',d=document,l=d.location,c=d.cookie;function f(n){ if(c){var i=c.indexOf(n+'=');if(i>-1){var j=c.indexOf(';',i);return c.substring(i+n. length+1,j<0?c.length:j)}}}var x=f('__utmx'),xx=f('__utmxx'),h=l.hash; d.write('<sc'+'ript src="'+ 'http'+(l.protocol=='https:'?'s://ssl':'://www')+'.google-analytics.com' +'/siteopt.js?v=1&utmxkey='+k+'&utmx='+(x?x:'')+'&utmxx='+(xx?xx:'')+'&utmxtime=' +new Date().valueOf()+(h?'&utmxhash='+escape(h.substr(1)):'')+ '" type="text/javascript" charset="utf-8"></sc'+'ript>')})(); </script><script>utmx("url",'A/B');</script> <!-- End of Google Website Optimizer Control Script --> </head> <body> <div id="kilnFlyOut"> <div class="flyouttext"> <strong>Kiln gives you:</strong><br /> &#8226; A complete version control system based on <a href="http://mercurial.selenic.com/" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/flyout/mercurial.selenic.com');">Mercurial</a><br /> &#8226; <a href="http://fogcreek.com/kiln/learnmore.html#hist_BranchAndMerge" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/flyout/branchandmerge.kilnhg.com');">Branching and merging</a> that really works </div> <div class="flyouttext"> &#8226; <a href="http://fogcreek.com/kiln/learnmore.html#hist_Tools" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/flyout/tools.kilnhg.com');">Straightforward setup</a> on your server, or simple secure hosting on ours<br /> &#8226; Seamlessly integrated <a href="http://fogcreek.com/kiln/learnmore.html#hist_StartReviewsEffortlessly" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/flyout/startreviews.kilnhg.com');">code review</a> </div> <div class="flyouttext"> <a href="convert.html" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/flyout/freetrial.shop.fogcreek.com');"><img src="i/getstarted.gif" width="182" height="24" border="0" /></a> </div> <div class="flyoutteaser"><nobr><strong>Try Kiln free!</strong> Mercurial hosting and more.</nobr></div> </div> <div class="toptop" align="left"> <ul> <li align="center"><a href="http://www.joelonsoftware.com/" onClick="javascript: pageTracker._trackPageview('/outbound/toptop/joelonsoftware.com');">Joel on Software</a></li> <li align="center"><a href="http://mercurial.selenic.com/" onClick="javascript: pageTracker._trackPageview('/outbound/toptop/mercurial.selenic.com');">Mercurial</a></li> <li align="center"><a href="index.html">Home</a></li> </ul> </div> <div id="dropshadow"></div> <div id="topnav"> <div class="tInterior"> <a href="index.html"> <img src="i/logo.png" border="0" alt="HgInit Home" /> </a> If you&#8217;ve been using Subversion, Mercurial is going to be confusing. This tutorial covers the biggest differences in how Mercurial works. If you&#8217;ve never used Subversion, just <a href="01.html">skip ahead</a>. </div> </div> <div class="main"> <h1> Subversion Re-education </h1> <div class="content"> <p> When the programmers at my company decided to switch from Subversion to Mercurial, <em>boy</em> was I confused. </p> <p>First, I came up with all kinds of stupid reasons why we shouldn&#8217;t switch. &#8220;We have to keep the repository on a central server, so it will be safe,&#8221; I said. You know what? I was wrong. In Mercurial every developer has a copy of the entire repository on their hard drive. It&#8217;s actually <em>safer</em>. And anyway, almost every Mercurial team uses a central repository, too, which you can back up compulsively, and you can build a three-ringed security zone complete with layers of Cylons, Stormtroopers, and adorable labradoodles (or whatever your IT department requires).</p> <p>&#8220;The trouble with distributed version control is that it makes it too easy to branch,&#8221; I said, &#8220;and branching always causes problems.&#8221; Turns out this was wrong, too. I was on a streak. Branching causes problems <em>in Subversion</em> because <em>Subversion</em> doesn&#8217;t store enough information to make <em>merging</em> work. In <em>Mercurial,</em> merging is painless and easy, and so branching is commonplace and harmless.</p> <p>Then I said, &#8220;Fine, I&#8217;ll use it, but don&#8217;t expect me to be able to figure it out.&#8221; And I asked Jacob to make me a cheat sheet listing all the things that I normally did in Subversion, and the equivalent way to do them in Mercurial.</p> <p>Now, I could show you this cheat sheet, but I won&#8217;t, because it messed up my brain for months.</p> <p>It turns out that if you&#8217;ve been using Subversion, your brain is a little bit, um, how can I say this politely? You&#8217;re brain damaged. No, that&#8217;s not polite. You need a little re-education. I walked around brain damaged for six months thinking that Mercurial was <em>more</em> complicated than Subversion, but that was only because I didn&#8217;t understand how it really worked, and once I did, it turns out&#8212;hey presto!&#8212;it&#8217;s really kind of simple.</p> <p>So I wrote this tutorial for you, in which I have been very careful <em>not</em> to explain things in terms of Subversion, because there is just no reason to cause any more brain damage. The world is brain damaged enough. Instead, for those of you who are coming from Subversion, I&#8217;ve got this one chapter at the beginning that will try to reverse as much damage as possible so that you can learn Mercurial from a clean slate.</p> <p><strong>If you&#8217;ve never used Subversion, you can skip ahead to the next chapter (<a href="01.html">Ground Up Mercurial</a>) without missing anything.</strong></p> <p>Ready? OK, let&#8217;s start with a short quiz.</p> <p style="margin-left:2em;"><strong>Question 1.</strong> Do you write perfect code the first time?</p> <p>If you answered &#8220;Yes&#8221; to question 1, you&#8217;re a liar and a cheat. You fail. Take the test again.</p> <p>New code is buggy. It takes a while to get it working respectably. In the meantime, it can cause trauma for the other developers on the team.</p> <p>Now, here&#8217;s how Subversion works:</p> <ul> <li>When you check new code in, everybody else gets it.</li> </ul> <p>Since all new code that you write has bugs, you have a choice.</p> <ul> <li>You can check in buggy code and drive everyone else crazy, or</li> <li>You can avoid checking it in until it&#8217;s fully debugged.</li> </ul> <p> Subversion always gives you this horrible dilemma. Either the repository is full of bugs because it includes new code that was just written, <em>or</em> new code that was just written is not in the repository.</p> <p>As Subversion users, we are so used to this dilemma that it&#8217;s hard to imagine it not existing.</p> <p>Subversion team members often go days or weeks without checking anything in. In Subversion teams, newbies are terrified of checking any code in, for fear of breaking the build, or pissing off Mike, the senior developer, or whatever. Mike once got so angry about a checkin that broke the build that he stormed into an intern&#8217;s cubicle and swept off all the contents of his desk and shouted, &#8220;This is your last day!&#8221; (It wasn&#8217;t, but the poor intern practically wet his pants.)</p> <p>All this fear about checkins means people write code for weeks and weeks <em>without the benefit of version control</em> and then go find someone senior to help them check it in. Why have version control if you can&#8217;t use it?</p> <p>Here&#8217;s a simple illustration of life under Subversion:</p> <div class="i"> <img src="i/00-svn.png" /> </div> <p>In Mercurial, <em>every developer has their own repository, living on their desktop:</em></p> <div class="i"> <img src="i/00-hg.png" /> </div> <p>So you can commit your code to your private repository, and get all the benefit of version control, whenever you like. Every time you reach a logical point where your code is a little bit better, you can commit it.</p> <p>Once it&#8217;s solid, and you&#8217;re willing to let other people use your new code, you <em>push</em> your changes from your repository to a central repository that everyone else pulls from, and they finally see your code. When it&#8217;s ready.</p> <p><em>Mercurial separates the act of committing new code from the act of inflicting it on everybody else.</em></p> <p>And that means that you can commit (<strong>hg com</strong>) without anyone else getting your changes. When you&#8217;ve got a bunch of changes that you like that are stable and all is well, you push them (<strong>hg push</strong>) to the main repository.</p> <h2>One more big conceptual difference</h2> <p>You know how every single street has a name?</p> <p><img src="i/00-tokyo.png" style="float:right; margin-left: 1em; margin-bottom: 1em; border: 1px solid black;" />Well, yeah, turns out that in Japan, not so much. They usually just number the <a href="http://sivers.org/jadr">blocks</a> in between the streets, and only very, very important streets get names.</p> <p>There&#8217;s a similar difference between Subversion and Mercurial.</p> <p>Subversion likes to think about <em>revisions</em>. A revision is what the entire file system looked like at some particular point in time. </p> <p>In Mercurial, you think about <em>changesets.</em> A changeset is a concise list of the changes between one revision and the next revision.</p> <p>Six of one, half dozen of the other: what&#8217;s the difference?</p> <p>Here&#8217;s the difference. Imagine that you and I are working on some code, and we branch that code, and we each go off into our separate workspaces and make lots and lots of changes to that code separately, so they have diverged quite a bit.</p> <p>When we have to merge, Subversion tries to look at both revisions&#8212;my modified code, and your modified code&#8212;and it tries to guess how to smash them together in one big unholy mess. It usually fails, producing pages and pages of &#8220;merge conflicts&#8221; that aren&#8217;t really conflicts, simply places where Subversion failed to figure out what we did.</p> <p>By contrast, while we were working separately in Mercurial, Mercurial was busy keeping a <em>series of changesets</em>. And so, when we want to merge our code together, Mercurial actually has a whole lot more information: it knows <em>what each of us changed</em> and can <em>reapply those changes</em>, rather than just looking at the final product and trying to guess how to put it together.</p> <p>For example, if I change a function a little bit, and then move it somewhere else, Subversion doesn&#8217;t really remember those steps, so when it comes time to merge, it might think that a new function just showed up out of the blue. Whereas Mercurial will remember those things separately: function changed, function moved, which means that if you <em>also</em> changed that function a little bit, it is much more likely that Mercurial will successfully merge our changes.</p> <p>Since Mercurial thinks of everything in terms of changesets, you get to do interesting things with those changesets. You can push them to a friend on the team to try them out, instead of pushing them to the central repository and inflicting them on everybody.</p> <p>If this all seems a bit confusing, don&#8217;t worry&#8212;by the time you get through this tutorial, it&#8217;ll all make perfect sense. For now the most important thing to know is that because Mercurial thinks in terms of &#8220;changesets&#8221; instead of &#8220;revisions&#8221; <em>it can merge code much better than Subversion</em>.</p> <p>And that means that <em>you can feel free to branch</em> because the merge isn&#8217;t going to be a nightmare.</p> <p>Want to know something funny? Almost every Subversion team I&#8217;ve spoken to has told me some variation on the very same story. This story is so common I should just name it &#8220;Subversion Story #1.&#8221; The story is this: at some point, they tried to branch their code, usually so that the shipping version which they gave their customers can be branched off separately from the version that the developers are playing with. And every team has told me that when they tried this, it worked fine, <em>until they had to merge</em>, and then it was a nightmare. What should have been a five minute process ended up with six programmers around a single computer working for two weeks trying to manually reapply every single bug fix from the stable build back into the development build.</p> <p>And almost every Subversion team told me that they vowed &#8220;never again,&#8221; and they swore off branches. And now what they do is this: each new feature is in a big #ifdef block. So they can work in one single trunk, while customers never see the new code until it&#8217;s debugged, and frankly, that&#8217;s ridiculous.</p> <p>Keeping stable and dev code separate is <em>precisely what source code control is supposed to let you do</em>.</p> <p>When you switch to Mercurial, you may not even realize it, but branching becomes possible again, and you don&#8217;t have to be afraid.</p> <p>That means you can have team repositories, where a small team of programmers collaborates on a new feature, and when they&#8217;re done, they merge it into the main development repository, and <em>it works!</em></p> <p>That means you can have a QA repository that lets the QA team try out the code. If it works, the QA team can push it up to the central repository, meaning, the central repository always has solid, tested code. And <em>it works!</em></p> <p>That means you can run experiments in separate repositories, and if they work, you can merge them into the main repository, and if they don&#8217;t work, you can abandon them, and <em>it works!</em></p> <h2>One last big conceptual difference</h2> <p>The last major conceptual difference between Subversion and Mercurial is not as big a deal, but it will trip you up if you&#8217;re not aware of it, and here it is:</p> <p>Subversion is basically revision control for <em>files,</em> but in Mercurial, revision control always applies to an entire directory&#8212;including all subdirectories.</p> <p>The main way you notice this is that in Subversion, if you go into a subdirectory and commit your changes, it only commits changes in that subdirectory and all directories below it, which potentially means you&#8217;ve forgotten to check something in that lives in some other subdirectory which also changed. Whereas, in Mercurial, all commands always apply to the entire tree. If your code is in <strong>c:\code</strong>, when you issue the <strong>hg commit</strong> command, you can be in <strong>c:\code</strong> or in <em>any subdirectory</em> and it has the same effect.</p> <p>This is not a big deal, but if you&#8217;re used to having one big gigantic repository for the whole company, where some people only check out and work on subdirectories that they care about, this isn&#8217;t a very good way to work with Mercurial&#8212;you&#8217;re better off having lots of smaller repositories for each project.</p> <h2>And finally&#8230;</h2> <p>Here&#8217;s the part where you&#8217;re just going to have to take my word for it.</p> <p>Mercurial is better than Subversion.</p> <p>It is a better way of working on source code with a team. It is a better way of working on source code by yourself.</p> <p>It is just <em>better.</em></p> <p>And, mark my words, if you understand the way Mercurial works, and you work the way Mercurial works, and you don&#8217;t try to fight it, and you don&#8217;t try to do things the old Subversion way using Mercurial but instead you learn to work the way Mercurial expects you to work, you will be happy, successful, and well-fed, and you&#8217;ll always be able to find the remote control to the TV.</p> <p>And in the early days, you will be tempted, I know you will, to give up on Mercurial and go back to Subversion, because it will be strange, like living in a foreign country, and you&#8217;ll be homesick, and you will come up with all kinds of rationalizations, like, for example, you will claim that Mercurial working directories take up too much disk space, which is bologna, because, actually, they usually take up less space than Subversion directories. (It&#8217;s true!)</p> <p>And then you&#8217;ll go back to Subversion because you tried to branch the Subversion way, and you got confused, and it didn&#8217;t work so well, because you really should have been branching the Mercurial way, by cloning repositories, not by trying to do the things that work in Subversion, but by learning the Mercurial way of doing things, which, trust me, <em>works.</em></p> <p>And then you&#8217;ll get Jacob, or whoever the equivalent of Jacob is at your office, to give you the &#8220;Subversion to Mercurial cheat sheet,&#8221; and you&#8217;ll spend three months thinking that <strong>hg fetch</strong> is just like <strong>svn up</strong>, without really knowing what <strong>hg fetch</strong> does, and one day, things will go wrong and you&#8217;ll blame Mercurial, when you really should be blaming yourself for not understanding how Mercurial works.</p> <p>I know you&#8217;ll do that because that is what I did.</p> <p>Don&#8217;t make the same mistake. Learn Mercurial, trust Mercurial, and figure out how to do things the Mercurial way, and you will move an entire generation ahead in source code control. While your competitors are busy taking a week to resolve all the merge conflicts they got when a vendor updated a library, you&#8217;re going to type <strong>hg merge</strong> and say to yourself, &#8220;Oh gosh, that&#8217;s cool, it just worked.&#8221; And Mike will chill out and share a doobie with the interns, and soon it will be spring and the kids at the nearby college will trade in their heavy parkas for skimpy A&amp;F pre-torn T-shirts, and life will be good.</p> <div class="footer"> <div class="footernav" id="home" align="center"><a href="index.html"><span class="angleQuote">&#171;</span> Home</a></div> <div class="footernav" id="comingup">Coming up: Mercurial<br />from the ground up.</div> <div class="footernav" id="next" align="center"><a href="01.html">Next <span class="angleQuote">&#187;</span></a></div> </div> <div class="pagebottom"> <div class="bottomimg"> <a href="http://www.kilnhg.com" target="_blank" id="logoLink" title="KilnHg.com" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/klinhg.com');">&nbsp;</a> <div class="bottomtext">This tutorial was brought to you by the fine folks at Fog Creek Software, makers of Kiln, a version control system powered by Mercurial</div> <div class="bottomtext"><strong>Kiln gives you:</strong><br /> &#8226; A complete version control system based on <a href="http://mercurial.selenic.com/" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/mercurial.selenic.com');">Mercurial</a><br /> &#8226; <a href="http://fogcreek.com/kiln/learnmore.html#hist_BranchAndMerge" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/branchandmerge.kilnhg.com');">Branching and merging</a> that really works<br /> &#8226; <a href="http://fogcreek.com/kiln/learnmore.html#hist_Tools" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/tools.kilnhg.com');">Straightforward setup</a> on your server, or simple secure hosting on ours<br /> &#8226; Seamlessly integrated <a href="http://fogcreek.com/kiln/learnmore.html#hist_StartReviewsEffortlessly" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/startreviews.kilnhg.com');">code review</a><br /> </div> <div class="bottomtext getStarted"> <a href="convert.html" target="_blank" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/freetrial.shop.fogcreek.com');"><img src="i/getstarted.gif" width="182" height="24" border="0" /></a> </div> </div> <div class="about"> <p> <strong>Any questions?</strong> </p> <p> If you have any questions about the material in this tutorial, no matter how newbie, ask them at <a href="http://kiln.stackexchange.com/" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/kiln.stackexchange.com');">the Kiln Knowledge Exchange.</a> </p> <p> <strong>About the author.</strong> </p> <p> Joel Spolsky is the founder of <a href="http://fogcreek.com" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/fogcreek.com');">Fog Creek Software</a>, a New York company that proves that you can treat programmers well and still be profitable. Programmers get private offices, free lunch, and work 40 hours a week. Customers only pay for software if they&#8217;re delighted. Fog Creek makes <a href="http://www.fogbugz.com" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/fogbugz.com');">FogBugz</a>, <a href="http://www.kilnhg.com/" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/text.kilnhg.com');">Kiln</a>, and <a href="https://www.copilot.com/" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/copilot.com');">Fog Creek Copilot</a>. Joel's blog <a href="http://www.joelonsoftware.com/" onClick="javascript: pageTracker._trackPageview('/outbound/bottom/joelonsoftware.com');">Joel on Software</a> is read by programmers everywhere. </p> </div> <!-- close bottom div --> </div> <!-- close "content" column --> </div> <!-- close "main" div --> </div> <!-- bottom grey bar --> <div class="botbot" align="left"> </div> <!-- Google Website Optimizer Tracking Script --> <script type="text/javascript"> if(typeof(_gat)!='object')document.write('<sc'+'ript src="http'+ (document.location.protocol=='https:'?'s://ssl':'://www')+ '.google-analytics.com/ga.js"></sc'+'ript>')</script> <script type="text/javascript"> try { var gwoTracker=_gat._getTracker("UA-225715-15"); gwoTracker._trackPageview("/2687020024/test"); }catch(err){}</script> <!-- End of Google Website Optimizer Tracking Script --> </body> </html>