The git clone command is used to create a copy of a specific repository or branch within a repository
.
Git is a distributed version control system. Maximize the benefits of a complete repository on your own machine by cloning.
What does git clone do?
When you clone a repository, you do not get a file, as you can in other centralized version control systems. When you clone with Git, you get the entire repository: all files, all branches, and all commits.
Cloning a repository is typically only done once, at the beginning of the interaction with a project. Once a repository already exists on a remote, such as on GitHub, I would clone that repository so that I could interact with it locally. Once you’ve cloned a repository, you don’t need to clone it again for regular development.
The ability to work with the entire repository means that all developers can work more freely. Without being limited by the files you can work on, you can work in a feature branch to make changes securely. Then, you can:
- Later, use git push to share your branch with the remote repository
- Open a pull request to compare changes with your collaborators
- Test and deploy as needed from
- Merge branch in the parent branch.
the
How to use git clone
Common uses and options for
git clone git clone [url]: Clone
- (download) a repository that already exists on GitHub, including all files, branches, and commits
- git clone -mirror: Clone a repository but without the ability to edit any of the files. This includes references or branches. You might want to use this if you are trying to create a secondary copy of a repository on a separate remote and want to match all branches. This can occur during setup with a new remote for your Git hosting or when using Git during automated testing.
- single-branch: Clone only one branch git clone –
- sparse: Instead of populating the working directory with all the files in the current commit recursively, populate only the files present in the root directory. This could help with performance when cloning large repositories with many directories and subdirectories.
- ‘git clone -recurse-submodules[=<pathspec]: After creating the clone, initialize and clone submodules within the function of the provided pathspec. This can be a good option if you are cloning a repository that you know has submodules, and you will work with those submodules as dependencies in your local development.
.
git clone –
You can see all the options with git clone in the git-scm’s documentation
. Git clone
git
clone examples
[url]
The most common use of cloning is simply to clone a repository. This is only done once, when you start working on a project, and it would follow the syntax of git clone [url].
git clone A Branch
git clone –
single-branch: By default, git clone will create remote tracking branches for all branches currently present on the remote control being cloned. The only local branch that is created is the default branch.
But, maybe for some reason you would like to get only one remote tracking branch for a specific branch, or clone a branch that is not the default branch. Both of these things happen when you use -single-branch with git clone.
This will create a clone that only has commits included in the current history line. This means that no other branches will be cloned. You can specify a particular branch to clone, but the default branch, usually primary, will be selected by default.
To clone
a specific branch, use:
git clone [url] -branch [branch] -single-branch
Cloning a single branch does not add any benefits unless the repository is very large and contains binaries that slow down the performance of the repository. The recommended solution is to optimize repository performance before relying on single-branch cloning strategies.
clone git with
SSH
Depending on how you authenticate with the remote server, you can choose
to clone using SSH.
If you choose to clone with SSH, you would use a repository-specific SSH path instead of a URL. Typically, developers authenticate with SSH from the machine level. This means that you would probably clone with HTTPS or SSH, not a mixture of both for your repositories.
Related terms
- git branch: Displays existing branches in your local repository. You can also use git branch [banch-name] to create a branch from your current location, or git branch -all to view all branches, both local ones on your machine and remote tracking branches stored since the last git pull or git fetch from the remote.
- git pull: Update your current local work branch with all new commits from the corresponding remote branch on GitHub. Git Pull is a combination of Git Fetch and Git Merge.
- Git Push: Loads all local branch confirmations to the remote control.
- git remote -v: Displays the associated remote repositories and their stored name, as a source.
Contribute to this article on GitHub.