Course Tonight

Course Tonight

Did You Know?

Advanced visual search system powered by Ajax

Chapter 25: Cloning Repositories

Estimated reading: 3 minutes 29 views

Section 25.1: Shallow Clone Cloning a huge repository (like a project with multiple years of history) might take a long time or fail because of the amount of data to be transferred. In cases where you don’t need to have the full history available, you can do a shallow clone:

git clone [repo_url] --depth 1

The above command will fetch just the last commit from the remote repository.

Be aware that you may not be able to resolve merges in a shallow repository. It’s often a good idea to take at least as many commits as you are going to need to backtrack to resolve merges. For example, to instead get the last 50 commits:

git clone [repo_url] --depth 50

Later, if required, you can fetch the rest of the repository:

Version ≥ 1.8.3:

git fetch --unshallow # equivalent of git fetch --depth=2147483647
# fetches the rest of the repository

Version < 1.8.3:

git fetch --depth=1000 # fetch the last 1000 commits

Section 25.2: Regular Clone

To download the entire repository including the full history and all branches, type:

git clone <url>

The example above will place it in a directory with the same name as the repository name.

To download the repository and save it in a specific directory, type:

git clone <url> [directory]

For more details, visit Clone a repository.

Section 25.3: Clone a specific branch To clone a specific branch of a repository, type --branch <branch name> before the repository URL:

git clone --branch <branch name> <url> [directory]

To use the shorthand option for --branch, type -b. This command downloads the entire repository and checks out <branch name>.

To save disk space, you can clone history leading only to a single branch with:

git clone --branch <branch_name> --single-branch <url> [directory]

If --single-branch is not added to the command, the history of all branches will be cloned into [directory]. This can be an issue with big repositories.

To later undo the --single-branch flag and fetch the rest of the repository, use the following commands:

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin

Section 25.4: Clone recursively Version ≥ 1.6.5

git clone <url> --recursive

Clones the repository and also clones all submodules. If the submodules themselves contain additional submodules, Git will also clone those.

Section 25.5: Clone using a proxy If you need to download files with git under a proxy, setting the proxy server system-wide may not be enough. You can try the following:

git config --global http.proxy http://<proxy-server>:<port>/

Leave a Comment

Share this Doc

Chapter 25: Cloning Repositories

Or copy link

CONTENTS