LinuxCommandLibrary

hg-branch

Manage Mercurial branches

TLDR

Show the name of the currently active branch

$ hg branch
copy

Create a new branch for the next commit
$ hg branch [branch_name]
copy

SYNOPSIS

hg branch [OPTION...] [NAME]

PARAMETERS

NAME
    The name of the branch to set for the next commit. If provided, the branch name for subsequent commits (until changed again) will be set to NAME. If omitted, the command displays the name of the current active branch.

-f, --force
    Allows setting a branch name even if a branch with the same name already exists. When used with NAME, it bypasses warnings if the name duplicates an existing branch. It is also implied when using --rev to set a branch.

-C, --close-branch
    Marks the current branch as closed. Closed branches are still part of the repository history but are typically filtered out by default when displaying active branches, signifying that no further development is expected on them.

--rev REV
    Operates on a specific revision REV. When used to set a branch, it applies the branch name to the specified revision. This option implies --force if setting a new branch.

DESCRIPTION

The hg branch command is a core utility within the Mercurial (hg) Distributed Version Control System. It allows users to interact with named branches, which are persistent lines of development tracked within a repository. This command serves three primary purposes: showing the current active branch, setting the name of the branch for the next commit, and marking the current branch as closed. Unlike anonymous branches (which are implicit and created by multiple heads), named branches are explicitly declared and visible in the repository history, providing a way to organize different development efforts or releases.

It's crucial to note that this is a Mercurial-specific command and not a native Linux command, though it is commonly used on Linux systems with Mercurial installed.

CAVEATS

The hg branch command is part of the Mercurial (hg) Distributed Version Control System, not a standalone Linux utility. It requires Mercurial to be installed on the system to function. When setting a new branch name, the change only takes effect for subsequent commits; it does not alter the history of previous commits. Closing a branch marks it as inactive but does not delete its history from the repository. Named branches are persistent; they are part of the repository's metadata and are propagated during pushes and pulls.

BRANCHING MODEL

Mercurial's named branches are persistent and global to the repository. Unlike lightweight branches in some other VCS, closing a named branch prevents new commits on it but doesn't remove its history. New work typically starts on a new named branch. This model emphasizes clear, long-lived development lines.

BEST PRACTICES

It's common practice to use a unique named branch for each significant feature or release line. Once a feature is merged or a release is finalized, the branch can be closed to signify its completion, keeping the list of active branches manageable.

HISTORY

Mercurial, created by Matt Mackall, emerged in 2005 as a distributed version control system, primarily written in Python. It was designed as a free, open-source alternative to proprietary systems like BitKeeper. The concept of named branches, which hg branch manages, has been a fundamental feature of Mercurial since its early days, providing a robust way to manage parallel lines of development within a repository. This contrasts with some other VCS where branches might be more lightweight or managed differently (e.g., Git's explicit branch pointers).

SEE ALSO

hg commit(1), hg heads(1), hg branches(1), hg update(1), hg revert(1)

Copied to clipboard