As your Git repositories grow, it can become difficult to clone them due to the large size of the repository history. One useful solution to this problem is using a shallow clone.
A shallow clone in Git is a clone of a repository with a truncated commit history. Unlike a regular clone, which downloads the entire history of the repository, a shallow clone limits the number of commits, thereby saving bandwidth and disk space.
When you perform a shallow clone, you specify a depth (--depth
) that indicates how many recent commits of each branch you want to retrieve. Like this:
git clone --depth <depth> <repository-url>
The --depth
flag instructs Git to fetch only the last n
commits from the remote history, where n
is the number you specify. For example, using --depth 1
tells Git to fetch only the latest commit:
git clone --depth 1 https://github.com/example/repo.git
This command creates a shallow clone where the .git
directory only contains the single most recent commit from the branch you are cloning instead of the entire commit history.