LinuxCommandLibrary

hg-add

Add files to Mercurial repository

TLDR

Add files or directories to the staging area

$ hg add [path/to/file]
copy

Add all unstaged files matching a specified pattern
$ hg add [[-I|--include]] [pattern]
copy

Add all unstaged files, excluding those that match a specified pattern
$ hg add [[-X|--exclude]] [pattern]
copy

Recursively add sub-repositories
$ hg add [[-S|--subrepos]]
copy

Perform a test-run without performing any actions
$ hg add [[-n|--dry-run]]
copy

SYNOPSIS

hg add [-n] [--dry-run] [-I PATTERN] [-X PATTERN] [-S] [-t TYPE] [-d] [FILE]...

PARAMETERS

-R, --repository REPO
    search for repository root or overlay bundle (deprecated)

-I, --include PATTERN [+]
    include only files matching specified glob patterns

-X, --exclude PATTERN [+]
    exclude files matching specified glob patterns

-S, --subrepos
    recursively operate across subrepositories

-t, --type TYPE
    add as specific type: l (link), x (executable), u (symlink)

-d, --delete
    mark untracked files as deleted (inverse of add)

-n, --dry-run
    show what would be added without performing action

DESCRIPTION

The hg add command in Mercurial, a distributed version control system, stages files for the next commit by marking them as tracked in the repository.

It recursively adds all untracked files in the specified paths (or current directory if none given), excluding those matching .hgignore patterns. Modified tracked files are not added by default; use hg add -u via global update flag for that.

This prepares changes for hg commit, similar to git add. Dry-run mode (-n) previews actions without changes. Include/exclude patterns fine-tune selection, and -S handles subrepositories.

Ideal for new projects: run hg init, add files, then commit. Supports special types like symlinks (-t u). Ensures clean working directory status via hg status.

CAVEATS

Does not add directories (empty dirs ignored); respects .hgignore by default. Use -u global option to update modified files. Not for removing files (use hg remove).

EXAMPLES

hg add file.txt
hg add dir/ (adds all eligible files recursively)
hg add -n '*py' (dry-run Python files)

STATUS INTEGRATION

Added files show as A (added) in hg status; untracked remain '?' until added.

HISTORY

Introduced in Mercurial 0.9b (2005) by Matt Mackall. Stable across versions; subrepo support added in 1.9 (2012). Aligns with Git-like workflows in modern releases.

SEE ALSO

hg status(1), hg commit(1), hg forget(1), hg remove(1), git add(1)

Copied to clipboard