- you try to add yet another small feature to a complex program and suddenly nothing works anymore,
- you factor two seemingly identical methods, and some test breaks,
- you remove some obviously useless redrawing code and three days later some weird behaviour pops up...
That is exactly what source control systems are for: they are "the granaries of software".
hg --versionThis should display a version number and copyright notice. Now make an empty directory and enter it:
mkdir hg-projectTo initialize a new repository simply type:
hg initThis creates a subdirectory .hg in which Mercurial keeps all information about the repository. Let us add a dummy file dummy.txt with some text:
echo "Hello Mercurial!" > dummy.txtNow, if you type:
hg statusMercurial displays the status of the files present in the repository. Right now, there is only one file dummy.txt which is not tracked. This is indicated by a question mark next to the file name, such as this:
? dummy.txtTo track the file, first type:
hg add dummy.txtThe status (obtained by typing the command hg status) becomes:
A dummy.txtwhich means dummy.txt is marked for addition in the next revision.
To create a new revision, simply type:
hg commit --user "James Hacker" --message "[add] First revision of the repository. Added a dummy file for explanatory purposes."
That is where the real fun begins! Inadvertent deletion of tracked files are not to be feared anymore:
del dummy.txtThe status of the repository indicates that dummy.txt is missing. This is displayed with an exclamation mark next to the file name:
! dummy.txtIn order to recover the lost file, just type:
hg revert --all
Another scenario is when you code for a few day, but end up disappointed with the result and want to go back to your initial state. To simulate this scenario let us first replace the text in dummy.txt:
echo "By doing this, I am taking the wrong path" > dummy.txt
M dummy.txtWe commit this modification to the repository:
hg commit --user "James Hacker" --mesage "[add] Performed some experiment. This may turn out to be a mistake..."
hg historyIt should display a list of revisions, each with its number, author, date of creation and description. We want to go back to the first revision (numbered 0), so we type:
hg update --rev 0And voila! If we check the content of dummy.txt, it is now back to its initial content:
more dummy.txtFrom there, we could add a new file to the repository and perform another commit:
echo "Tracking a second file." > another-dummy.txt
hg add another-dummy.txt
hg commit --user "James Hacker" --message "[add] A second dummy file."
hg update --rev 1
Rome was not built in a day, and so is great software. A source control system such as Mercurial is the granary that protects your code from disasters during development time.
This post presented the basics of Mercurial. In a later post, I will talk about the Mercurial graphical user interface TortoiseHg, explain how default settings are changed and share my personal practices for revision messages.
Some questions for today:
- Do you use a source control system?
- If not, why?
- If so, what is your preferred source control system?