Basic Hg Commands

The following sheet summarizes important commands used while working with Mercurial in command line interfaces. You can download higher resolution here

08_HgSheet

Mercurial is distributed version control system that is developed since 2005. Although it is not
commonly used but it has interesting features that it makes more sense to use it as best preference
among other version control tools. The following lines will try to show important features of Mercurial.

* Versions Storage
Storing different versions of tracked files is based on delta difference of each file. i.e any file that has
a new version that needs to be recorded, Mercurial stores only the new changes occurred on the file, not the complete file again. This feature helps in saving repository size as no duplication in sizes of files.
The exception for that when a file has long history of version, at certain thershold Mercurial stores another snapshot from the file then continues with the delta mechanism instead of having long list of accumulated deltas which may take time to reconstruct the file. Eventually, you can configure also your repositories if you need to work with full snapshot only or delta difference.
Although this may sound interesting, however it works only best with text files (source codes for instances). However, for binary files (word document for instance) this can problematic as Mercurial will not be able to detect the difference between different versions, so it stores the complete file again (not the difference only). 

* One Time Staging
To choose which files you target to include in your repository, it is common to call this step as staging. i.e you add it to a stage in order to let your versioning tool to track it. Mercurial mechanism is using staging concept but only one time when you first start tracking your file. Should your file undergo to some modifications, you need then to directly commit it wihtout need to stage it.

* Different Branching Models
The concept of branching in Mercurial can be implemented in several ways. First, you can simply created un-named branch. This is helpful when you want keep working in one development branch but you would like to have kind of parallel quick development beside this branch. To do that you can simply do that by committing your changes while you are in the branch, then checkout back again to previous commit and do another parallel commit. For example lets say your are in main_develop branch on commit ‚A‘. While you are at main_develop branch you made new commit ‚B‘. If you want to make un-named branch, you can checkout to commit ‚A‘ again and make your modifications then commit again, let’s say ‚C‘. In that way you will have ‚B‘ and ‚C‘ are parallel branches with their parent is ‚A‘.
If you would like to work with named branches, still you have two models. You can use what is called ‚Bookmark‘ which is something equivalent to normal branches in git. You can create a bookmark, checkout to it and merge or push it.
Yet Mercurial has third branching model which is using ‚branch‘. And here branch means longstanding branch that cannot be deleted. You can only close it. So ‚branch‘ in Mercurial means something that should stay forever, something like release and main branches. You can perform main operations on branches (checkout, merge, push).

* Really Exchange between Repositories To clarify this point, simply try to create repository in Git. Then clone it from another directory, make changes and commit in the cloned repository, push back your changes. Git will refuse pushing back again because you are trying to write to non-bare repository or the target branch is already active. This process works straightforward in Mercurial as simply it applies the concept of DVCS.