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 advantages of a full repository on your own machine by cloning.
What Does git clone Do?
- Use
git pushto share your branch with the remote repository - Open a pull request to compare the changes with your collaborators
- Test and deploy as needed from the branch
- Merge into the
mainbranch
How to Use git clone
Common usages and options for git clone
git clone [url]: Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commitsgit clone --mirror: Clone a repository but without the ability to edit any of the files. This includes the refs or branches. Useful for creating a secondary copy of a repository on a separate remotegit clone --single-branch: Clone only a single branchgit clone --sparse: Instead of populating the working directory with all files, only populate the files present in the root directory. This could help with performance when cloning large repositoriesgit clone --recurse-submodules[=<pathspec]: After the clone is created, initialize and clone submodules within based on the provided pathspec
Examples of git clone
git clone [url]
The most common usage of cloning is to simply clone a repository. This is only done once, when you begin working on a project, and 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 of the branches currently present in the remote which is being cloned. The only local branch that is created is the default branch.
But, maybe for some reason, you would like to only get a remote tracking branch for one specific branch, or clone one branch which isn’t 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 line of history. This means no other branches will be cloned. You can specify a certain branch to clone, but the default branch, usually main, will be selected by default.
To clone one specific branch, use:
Cloning only one branch does not add any benefits unless the repository is very large and contains binary files that slow down the performance of the repository. The recommended solution is to optimize the performance of the repository before relying on single branch cloning strategies.
git clone With SSH
Depending on how you authenticate with the remote server, you may choose to clone using SSH.
If you choose to clone with SSH, you would use a specific SSH path for the repository instead of a URL.
Workflow Example
Here’s a typical workflow after cloning a repository:Related Commands
git branch: Shows the existing branches in your local repository. You can also usegit branch [branch-name]to create a branch from your current location, orgit branch --allto see all branchesgit pull: Updates your current local working branch with all new commits from the corresponding remote branch on GitHub.git pullis a combination ofgit fetchandgit mergegit push: Uploads all local branch commits to the remotegit remote -v: Show the associated remote repositories and their stored name, likeorigin
Next Steps
Git Status
Check the status of your cloned repository
Git Add
Stage changes for commit
Git Commit
Create commits in your repository
Git Push
Share your changes with the remote