{"id":460,"date":"2012-10-22T22:45:56","date_gmt":"2012-10-23T05:45:56","guid":{"rendered":"http:\/\/bellm.org\/blog\/?p=460"},"modified":"2012-10-22T22:45:56","modified_gmt":"2012-10-23T05:45:56","slug":"minimal-mercurial","status":"publish","type":"post","link":"http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/","title":{"rendered":"Minimal Mercurial"},"content":{"rendered":"<p>As a scientist, most of my work consists of text files: source code, data, and papers. Keeping track of changes to these files with version control helps me avoid losing work, track down bugs, reuse code, and keep records of my research. Rather than renaming files <code>code.py.oldversion, code.py.aug01, code.py.brokendontuse<\/code>, version control gives me a single, current source file as well as a database of changes that I can use to restore any previous version at will.<\/p>\n<p>Older version control programs (e.g., CVS, SVN) were designed for large teams collaborating on a single code base, so they store changes in a single, centralized repository. For the solitary scientist, newer &#8220;decentralized&#8221; version control systems like <a href=\"http:\/\/git-scm.com\/\">Git<\/a> and <a href=\"http:\/\/mercurial.selenic.com\/\">Mercurial<\/a> work better: they are lightweight and easy to set up, and since the entire change history is carried with the code, it&#8217;s easy to keep your work synced across multiple computers.<\/p>\n<p>Among software engineers, Git is far more popular than Mercurial, thanks largely to the online repository <a href=\"http:\/\/github.com\">github<\/a> [<sup class='footnote'><a href='http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/#fn-460-1' id='fnref-460-1' onclick='return fdfootnote_show(460)'>1<\/a><\/sup>]. Git is unnecessarily complicated for most of the work I do, though, so I prefer Mercurial for day-to-day use.? With either system, though, a single scientist only really needs a few simple commands to get most of the benefits of version control.? The Win-Vector blog published a <a href=\"http:\/\/www.win-vector.com\/blog\/2012\/07\/minimal-version-control-lesson-use-it\/\">guide for git<\/a>; here is mine for Mercurial.<\/p>\n<p>After <a href=\"http:\/\/mercurial.selenic.com\/downloads\/\">installing Mercurial<\/a> [<sup class='footnote'><a href='http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/#fn-460-2' id='fnref-460-2' onclick='return fdfootnote_show(460)'>2<\/a><\/sup>], typing <code>hg<\/code> at the command prompt should print a list of basic commands [<sup class='footnote'><a href='http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/#fn-460-3' id='fnref-460-3' onclick='return fdfootnote_show(460)'>3<\/a><\/sup>].? Of these, three are the most used:<\/p>\n<p><code>hg init<br \/>\nhg add <em>&lt;filename or pattern&gt;<\/em><br \/>\nhg commit<\/code><\/p>\n<p><code>hg init<\/code> creates a new respository in the current directory.? You only need to run it once, when you first start tracking files for a project.? It creates a hidden directory <code>.hg<\/code> in the current directory to store changes in.<\/p>\n<p><code>hg add <em>&lt;filename or pattern&gt;<\/em><\/code> tells Mercurial to track changes for the specified files.? Again, you only need to run it once for a given file [<sup class='footnote'><a href='http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/#fn-460-4' id='fnref-460-4' onclick='return fdfootnote_show(460)'>4<\/a><\/sup>].<\/p>\n<p><code>hg commit<\/code> records any changes to all tracked files and allows you to make notes on the changes you&#8217;ve made.?? You should run this frequently, particularly after any significant change in the state of the code.? I often specify the message at the command line with the <code>-m<\/code> flag:? <code>hg commit -m \"This is my commit message.\"<\/code><\/p>\n<p>That&#8217;s it!? So, a typical session might look like this:<\/p>\n<p><code>mkdir myproject<br \/>\ncd myproject<br \/>\nhg init<br \/>\nvim code.py<br \/>\n<em>make changes to code.py<\/em><br \/>\nhg add code.py<br \/>\nhg commit -m \"Initial commit of code.py\"<br \/>\nvim README.txt<br \/>\n<em>make changes<\/em><br \/>\nhg add README.txt<br \/>\nvim code.py<br \/>\n<em>more changes<\/em><br \/>\nhg commit -m \"added README.txt; added functions to code.py\"<\/code><\/p>\n<p>These commands are enough to ensure you&#8217;ve got all your changes backed up, and for some projects that&#8217;s all you&#8217;ll ever need [<sup class='footnote'><a href='http:\/\/bellm.org\/blog\/2012\/10\/22\/minimal-mercurial\/#fn-460-5' id='fnref-460-5' onclick='return fdfootnote_show(460)'>5<\/a><\/sup>].? Eventually, you&#8217;ll probably want to list the revision history of a file (<code>hg log<\/code>), check which files have uncommitted changes (<code>hg status<\/code>), compare versions of the code (<code>hg diff<\/code> or <code>hg vimdiff<\/code>), and sync with another machine (<code>hg clone\/push\/pull\/update\/merge<\/code>).? To learn how, you can use the command-line help system (<code>hg help<\/code>), the official <a href=\"http:\/\/mercurial.selenic.com\/guide\/\">tutorials<\/a> and <a href=\"http:\/\/hgbook.red-bean.com\/read\/\">Definitive Guide<\/a>, and Joel Spolskly&#8217;s <a href=\"http:\/\/hginit.com\/\">tutorial<\/a>.<\/p>\n<p>Echoing the Win-Vector Blog, though, <a href=\"http:\/\/www.win-vector.com\/blog\/2012\/07\/minimal-version-control-lesson-use-it\/\">you can learn all that when you need it<\/a>.? Starting today, you can get the benefits of version control with just three simple commands: <code>hg init, hg add, hg commit<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a scientist, most of my work consists of text files: source code, data, and papers. Keeping track of changes to these files with version control helps me avoid losing work, track down bugs, reuse code, and keep records of my research. Rather than renaming files code.py.oldversion, code.py.aug01, code.py.brokendontuse, version control gives me a single, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[14,8],"tags":[],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-science","category-tools"],"_links":{"self":[{"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/comments?post=460"}],"version-history":[{"count":29,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":500,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions\/500"}],"wp:attachment":[{"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bellm.org\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}