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 [-f] [NAME]

PARAMETERS

-f, --force
    Set branch name even if it already exists on another head

-i, --id
    Print integer branch ID instead of name

-q, --quiet
    Do not print the new branch name

--help
    Display help and exit

DESCRIPTION

The hg branch command in Mercurial (hg) is used to create new named branches or switch the current branch. Named branches are permanent markers in the repository history, unlike bookmarks which are local pointers.

Without arguments, it displays the current branch name. Providing a NAME sets the working directory to that branch; the name must be unique unless using -f. This affects future commits on the current head.

Branches are useful for long-lived development lines, like 'stable' or 'feature-x'. They persist across clones and are visible via hg branches. To close a branch, commit with --close-branch.

This command does not create a new changeset; use hg commit afterward. It's lightweight and doesn't require pushing to activate remotely.

CAVEATS

Branch names must be unique across repository history unless -f is used; cannot rename existing branches directly; use hg branches to list active ones.

EXAMPLES

hg branch
Prints current branch.

hg branch stable
Sets current branch to 'stable'.

hg branch -f existing
Forces branch to existing name.

BRANCH VISIBILITY

Branches are global and replicated on push/pull; closed branches shown with '*' in hg branches output.

HISTORY

Introduced in Mercurial 0.9b (2006) to support named branches, evolving from anonymous heads. Enhanced in later versions with -f (0.9.3) and --id (1.0). Remains core despite rise of bookmarks in 1.3 (2009).

SEE ALSO

hg branches(1), hg commit(1), hg heads(1), git branch(1)

Copied to clipboard