LinuxCommandLibrary

git-cp

Copy files within a Git repository

TLDR

Copy an existing file in a Git repo, staying in the same directory

$ git cp [file] [new_file]
copy

Copy an existing file in a Git repo and place it elsewhere
$ git cp [path/to/file] [path/to/new_file]
copy

SYNOPSIS

git cp [-r|--recursive] [-v|--verbose] [-m|--message <msg>] [-C|--cached] <source>... <dest>

PARAMETERS

-r, --recursive
    Copy directories recursively.

-v, --verbose
    Enable verbose output during copy.

-m, --message <msg>
    Specify commit message; auto-commits after staging.

-C, --cached
    Copy from Git index instead of working tree.

DESCRIPTION

The git cp command, part of the third-party git-extras toolkit, copies files or directories within a Git repository and automatically stages the destination file(s) with git add. Unlike the standard Unix cp, it integrates with Git's index, making it convenient for tracked copies without manual staging.

It treats the copy as a new blob unless specified otherwise, preserving Git history context. Source files remain unchanged, distinguishing it from git mv. Ideal for duplicating configs, backups, or branches during development.

Requires installation of git-extras (e.g., via package managers like apt, brew). Not a core Git command; core Git lacks a porcelain cp equivalent. Usage simplifies workflows: copy and stage in one step, optionally committing immediately.

Verbose mode shows progress; recursive for dirs. Commit message option enables one-liner copy-commit.

CAVEATS

Third-party tool; install git-extras first. Does not preserve copy metadata in Git history by default (new blob). Fails if dest exists unless overwritten implicitly.

EXAMPLE

git cp README.md docs/README.md — Copy and stage.
git cp -r src/ backup/src/ — Recursive dir copy.
git cp -m 'Duplicate config' config.prod config.dev — Copy, stage, commit.

INSTALLATION

Linux: sudo apt install git-extras or brew install git-extras on macOS.

HISTORY

Developed as part of git-extras by Mark Otto and contributors, first released around 2013. Evolved to handle recursive copies and auto-commit in later versions; widely used in modern Git workflows.

SEE ALSO

git-mv(1), cp(1), git-add(1), git-extras(1)

Copied to clipboard