AICollection Help

Mercurial

Introduction to Mercurial

Mercurial (hg) is a distributed version control system (VCS) used to manage software source code changes. It was created by Matt Mackall in 2005 and has since become a popular choice among developers who need a lightweight and efficient tool for tracking and managing changes in their projects. Mercurial is similar to Git in that it allows each developer to have a local copy of the entire repository, meaning they can work offline and sync changes later.

Mercurial is known for its simplicity, speed, and strong support for branching and merging, making it well-suited for both small and large projects. Unlike centralized VCS systems like SVN, where there is a single server storing the repository, Mercurial allows each developer to have their own complete version of the repository. This makes it ideal for distributed workflows, where multiple developers are working on the same project in parallel.

In this article, we will explore Mercurial’s key features, commands, and provide detailed examples of how to use Mercurial for managing your software development projects.

Key Concepts in Mercurial

Before diving into the specific commands, it is essential to understand the key concepts that underpin Mercurial’s functionality:

  • Repository: The repository is where all the project files and their history are stored. Since Mercurial is a distributed system, each user has a full copy of the repository, including all past revisions.

  • Commit: A commit in Mercurial is a snapshot of changes in your local working directory. Once you’ve made changes to files, you commit them to the repository to record those changes.

  • Branch: A branch in Mercurial is used to create a separate line of development. You can branch off from the main project and work on new features or bug fixes without affecting the main codebase.

  • Revision: Each commit creates a new revision, and each revision is associated with a unique identifier. Mercurial uses a numbered revision history that allows you to track changes and roll back to previous versions.

  • Clone: Since Mercurial is distributed, you don’t work directly with a central repository. Instead, you clone the repository to your local machine, creating a full copy that you can work with offline.

  • Merge: Merging in Mercurial allows you to combine changes from two branches. This is typically done after completing a feature or bug fix and integrating those changes back into the main development branch.

  • Push and Pull: Mercurial supports a distributed workflow, so you can push changes to a remote repository or pull changes from one. This makes collaboration easy, as you can work offline and sync with the main repository later.

Basic Syntax of Mercurial Commands

The syntax for using Mercurial commands is straightforward:

hg [command] [options] [arguments]

Where:

  • command is the action you want to perform, such as commit, clone, update.

  • options are flags that modify the behavior of the command.

  • arguments are files, directories, or other parameters that the command will operate on.

Common Mercurial Commands and Examples

1. hg init: Initialize a New Repository

The hg init command is used to create a new Mercurial repository in the current directory. This command initializes a local repository, allowing you to start version controlling your project.

Example:

hg init my_project

This creates a new Mercurial repository in the my_project directory.

2. hg clone: Clone a Repository

The hg clone command is used to make a copy of an existing Mercurial repository. It is often used to create a working copy of a remote repository.

Example:

hg clone https://example.com/hg/my_project

This command creates a copy of the repository located at https://example.com/hg/my_project, allowing you to work on your local version of the repository.

3. hg status: Check the Status of Your Working Directory

The hg status command shows the current status of the working directory, including which files have been modified, added, or deleted since the last commit.

Example:

hg status

This command will display the changes in your working directory, including files that have been modified, added, or deleted.

4. hg add: Add Files to the Repository

The hg add command is used to add new files or directories to be tracked by Mercurial. Files need to be added before they can be committed.

Example:

hg add newfile.txt

This command adds the file newfile.txt to the repository, preparing it to be committed.

5. hg commit: Commit Changes to the Repository

The hg commit command is used to commit changes from your working directory to the repository. Each commit creates a new revision, recording the changes made.

Example:

hg commit -m "Added login functionality"

This commits the changes in your working directory with the message "Added login functionality", creating a new revision in the repository.

6. hg log: View the Commit History

The hg log command shows the commit history for the current repository or a specific file. It includes information about each revision, such as the revision number, author, date, and commit message.

Example:

hg log

This command lists the history of commits in the repository, allowing you to see what changes have been made over time.

7. hg update: Update Your Working Copy

The hg update command updates your working directory to the latest revision from the repository. This ensures your local working copy is synchronized with the latest changes.

Example:

hg update

This command updates your working directory to the latest changes from the repository, ensuring you’re working with the most recent version of the code.

8. hg merge: Merge Changes from Another Branch

The hg merge command is used to merge changes from one branch into another. This is typically done after finishing work on a feature or bug fix branch and integrating those changes back into the main branch.

Example:

hg merge feature/login

This merges the changes from the feature/login branch into the current branch. If there are any conflicts, you will be prompted to resolve them.

9. hg push: Push Changes to a Remote Repository

The hg push command is used to push your committed changes to a remote repository. This allows other developers to access your changes and work on them.

Example:

hg push https://example.com/hg/my_project

This pushes your changes to the remote repository located at https://example.com/hg/my_project.

10. hg pull: Pull Changes from a Remote Repository

The hg pull command retrieves changes from a remote repository and applies them to your local working copy. This is used to sync your local copy with the latest changes made by others.

Example:

hg pull https://example.com/hg/my_project

This command pulls the latest changes from the specified remote repository and merges them into your local working copy.

11. hg diff: Show Differences Between Versions

The hg diff command shows the differences between your working directory and the last commit or between two revisions. It’s useful for reviewing changes before committing.

Example:

hg diff

This will show the differences between your working directory and the last committed revision. You can also compare two revisions:

hg diff -r 100:101

This compares revisions 100 and 101.

12. hg branch: Create and Switch Branches

The hg branch command is used to create a new branch or to show which branch your working directory is currently on.

Example:

hg branch feature/login

This command creates a new branch called feature/login for working on the login feature. When you’re done, you can merge it back into the main branch.

Mercurial vs Other Version Control Systems

Mercurial is a distributed version control system like Git, meaning that each user has a full copy of the repository and its history. This is in contrast to centralized version control systems like SVN, where there is a single central repository that developers interact with.

  • Mercurial vs Git: Git and Mercurial are both distributed VCS tools, and they share many similarities. Git is more popular and has a larger ecosystem, but Mercurial is known for being easier to learn and faster for many common tasks. Mercurial has a simpler, more streamlined interface and is better suited for projects that don’t need the full complexity of Git.

  • Mercurial vs SVN: Mercurial is distributed, meaning that every developer has a full copy of the repository, which allows for working offline and committing changes locally. SVN, on the other hand, is centralized, which means that all users rely on a central repository. Mercurial is more flexible for distributed teams, while SVN might be preferred in environments where a single central repository is necessary.

  • Mercurial vs Perforce: Perforce is known for its high-performance capabilities, particularly in handling very large repositories. Mercurial is lightweight, easier to use, and more suited for open-source and collaborative workflows, whereas Perforce is often used in enterprise environments with large, complex codebases.

Conclusion

Mercurial is a powerful and flexible version control system that excels in distributed environments. With its simple commands, fast performance, and strong branching and merging capabilities, it provides a robust solution for managing source code and collaborating with other developers. Whether you’re working on a small open-source project or a large enterprise system, Mercurial offers the tools you need to efficiently track and manage changes.

By understanding the key commands and concepts in Mercurial, you can leverage its full potential to streamline your development workflow, keep track of code changes, and collaborate effectively with other developers.

Last modified: 04 February 2025