LinuxCommandLibrary

hg-remove

Remove files from Mercurial's tracking (staging)

TLDR

Remove files or directories from the staging area

$ hg remove [path/to/file]
copy

Remove all staged files matching a specified pattern
$ hg remove [[-I|--include]] [pattern]
copy

Remove all staged files, excluding those that match a specified pattern
$ hg remove [[-X|--exclude]] [pattern]
copy

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

Remove files from the repository that have been physically removed
$ hg remove [[-A|--after]]
copy

SYNOPSIS

hg remove [OPTION]... FILE...
hg rm [OPTION]... FILE...

PARAMETERS

-f, --force
    remove file even if modified or added

-n, --dry-run
    do not perform actions, just show what would be done

-I, --include PATTERN
    include only files matching the given patterns

-X, --exclude PATTERN
    exclude files matching the given patterns

-a, --after
    record each removed file as a deletion to be committed later

--rev REV[+]
    stop at the specified revision (removes up to REV)

-e, --encoding ENCODING
    specify the encoding to use

-g, --git
    parse argfile using git extended diff format

-R, --repository REPO
    specify repository root path

DESCRIPTION

The hg remove (or hg rm) command in Mercurial version control system marks specified files as removed for the next commit. It removes them from both the working directory and the repository's tracking data.

By default, it deletes the files from the filesystem after staging the removal. Use options to control behavior, such as dry-run previews, forcing removal of modified files, or recording removals without deleting files.

This is essential for cleaning up obsolete files while preserving commit history. After running hg remove, commit the changes with hg commit to finalize. Files ignored by .hgignore are skipped unless forced.

It supports pattern matching for bulk operations and works with specific revisions for advanced use cases like backouts.

CAVEATS

Files are permanently deleted from the working directory unless --after is used. Always use --dry-run for previews. Ignores .hgignore patterns unless forced. Not for undoing adds; use hg forget.

EXAMPLES

hg rm file.txt – remove and delete single file.
hg rm -n '*.tmp' – preview removal of all .tmp files.
hg rm --after olddir/ – stage directory removal without deleting.

EXIT STATUS

0: success.
1: uncommitted changes present.
255: other errors.

HISTORY

Introduced in early Mercurial versions (pre-1.0, around 2006). Evolved with options like --after (1.3, 2008) and pattern matching. Remains stable in modern Mercurial (5.x+), aligning with Git-like workflows.

SEE ALSO

hg forget(1), hg status(1), git rm(1), rm(1)

Copied to clipboard