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 [--all] : :

PARAMETERS

:
    Specifies the source repository and the Git object (blob, tree, commit, or tag) to be copied. is a path or URL to the source repository, and is the object's SHA-1 hash or a symbolic reference (e.g., branch name or tag name).

:
    Specifies the destination repository and the desired name for the copied object. is a path or URL to the destination repository, and is the name of the new object in the destination repository (e.g., a branch name or a tag name). If already exists, it will be overwritten.

--all
    Copy all reachable objects from source repository, including branches, tags, commits, and blobs to destination repository.

DESCRIPTION

The git-cp command allows you to copy objects (blobs, trees, commits, and tags) between Git repositories, without needing a full checkout or history merge. It's particularly useful for transplanting individual files or directories from one repository to another, or for extracting specific commits or tag references. This command differs from `git cherry-pick` because it moves Git objects directly, avoiding the need to apply patches or resolve merge conflicts. It is primarily meant for developers who need to incorporate isolated changes between unrelated projects or across different branches where merging is not feasible or desired.

git-cp is not a core Git command, but a contributed script that usually comes with Git. Its functionality is similar to `git fetch` and `git cat-file` in that it relies on Git's plumbing commands to move objects directly between repos, without altering the source repository's or target repository's histories. The copied objects will exist only in the destination repository, allowing developers to build up their project utilizing code from various sources.

CAVEATS

git-cp is not a core Git command, which means it is provided as contributed script. Availability depends on the Git installation and the maintainer. Errors might occur if the required perl libraries aren't installed, so ensure that your system meets the prerequisites. Use with care because overwriting existing references in the destination repository can lead to data loss if the overwritten references are not properly backed up.

EXAMPLES

Copy a file from one repository to another: git cp repo1:HEAD:path/to/file.txt repo2:file.txt
Copy a tag from one repository to another: git cp repo1:mytag repo2:mytag

SEE ALSO

git fetch(1), git cat-file(1), git remote(1)

Copied to clipboard