LinuxCommandLibrary

hg-init

Create a new Mercurial repository

TLDR

Initialize a new repository in the current directory

$ hg init
copy

Initialize a new repository in the specified directory
$ hg init [path/to/directory]
copy

SYNOPSIS

hg init [OPTION]... [DESTINATION]

PARAMETERS

DESTINATION
    The path to the directory where the new Mercurial repository should be initialized. If omitted, the current working directory will be used. If the directory does not exist, hg init will attempt to create it.

--cwd DIR
    Run as if current working directory was DIR. This is a global Mercurial option that affects where the command is executed.

--config SECTION.NAME=VALUE
    Set a configuration option for this command execution. This can be used to set initial repository configurations (e.g., hooks) immediately upon creation, though often not necessary for init.

--debug
    Enable debug output. Provides more detailed information on the command's execution, useful for troubleshooting.

--verbose, -v
    Enable verbose output. Displays more information about what the command is doing.

DESCRIPTION

The hg init command is used to create a new, empty Mercurial repository. When executed, it sets up the necessary directory structure for Mercurial to track changes within a project. This involves creating a hidden directory named .hg at the specified DESTINATION (or in the current working directory if no DESTINATION is provided). The .hg directory is the core of the repository, containing all revision history, metadata, configuration files, and other internal data that Mercurial uses to manage the project's versions.

Running hg init is typically the very first step when starting a new project that you intend to manage with Mercurial. It does not add any existing files from the working directory to the repository; subsequent commands like hg add and hg commit are required for that.

CAVEATS

Running hg init in a directory that is already a Mercurial repository (i.e., it already contains a .hg directory) will typically result in a message indicating the repository already exists and will not re-initialize or corrupt the existing repository data.
The command only creates the .hg directory; it does not automatically add any existing files from the working directory into version control. Files must be explicitly added using hg add and then committed with hg commit.
hg init creates an empty repository. No branches, tags, or changesets exist until the first commit is made.

FIRST STEPS AFTER INITIALIZATION

After running hg init, your project directory is now a Mercurial repository. To start tracking files, you would typically use:
hg add . (to add all current files to be tracked)
hg commit -m "Initial commit" (to save the first version of your files)

CONTENTS OF THE <I>.HG</I> DIRECTORY

The .hg directory is crucial for Mercurial's operation. It contains:
store/: The changelog, manifests, and file data (deltas).
dirstate: Information about the working directory's state.
hgrc: The local repository configuration file.
hooks/: Directory for repository hooks.
And other internal files used by Mercurial.

HISTORY

Mercurial was developed by Matt Mackall starting in April 2005, primarily as a response to the licensing changes of BitKeeper, which had been used for Linux kernel development. The hg init command has been a foundational part of Mercurial since its early development. Its core functionality—creating a new, empty repository—has remained remarkably consistent throughout Mercurial's history, reflecting its role as the essential starting point for any new project under Mercurial version control.

SEE ALSO

hg(1), hg add(1), hg commit(1), hg clone(1), hg status(1)

Copied to clipboard