Git Guides
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 advantages of a full repository on your own machine by cloning.
What Does git clone Do?
When you clone a repository, you don’t get one file, like you may in other centralized version control systems. By cloning 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 your interaction with a project. Once a repository already exists on a remote, like on GitHub, then you would clone that repository so you could interact with it locally. Once you have cloned a repository, you won’t need to clone it again to do regular development.
The ability to work with the entire repository means that all developers can work more freely. Without being limited by which files you can work on, you can work on a feature branch to make changes safely. Then, you can:
- later use git push to 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 master branch.
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 commits.
- git clone —mirror : Clone a repository but without the ability to edit any of the files. This includes the refs, or branches. You may want to use this if you are trying to create a secondary copy of a repository on a separate remote and you want to match all of the branches. This may occur during configuration using a new remote for your Git hosting, or when using Git during automated testing.
- git clone —single-branch : Clone only a single branch
- git clone —sparse : Instead of populating the working directory with all of the files in the current commit recursively, only populate the files present in the root directory. This could help with performance when cloning large repositories with many directories and sub-directories.
- `git clone —recurse-submodules[=
You can see all of the many options with git clone in git-scm’s documentation.
Examples of git clone
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 master , will be selected by default.
To clone one specific branch, use:
git clone [url] —branch [branch] —single-branch
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. Typically, developers are authenticated with SSH from the machine level. This means that you would probably clone with HTTPS or with SSH — not a mix of both for your repositories.
- git branch : This shows the 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 see all branches, both the local ones on your machine, and the remote tracking branches stored from the last git pull or git fetch from the remote.
- git pull : Updates your current local working 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 : Uploads all local branch commits to the remote.
- git remote -v : Show the associated remote repositories and their stored name, like origin .
Get started with git and GitHub
Review code, manage projects, and build software alongside 40 million developers.
Источник
About remote repositories
GitHub’s collaborative approach to development depends on publishing commits from your local repository to GitHub for other people to view, fetch, and update.
About remote repositories
A remote URL is Git’s fancy way of saying «the place where your code is stored.» That URL could be your repository on GitHub, or another user’s fork, or even on a completely different server.
You can only push to two types of URL addresses:
- An HTTPS URL like https://github.com/user/repo.git
- An SSH URL, like git@github.com:user/repo.git
Git associates a remote URL with a name, and your default remote is usually called origin .
Creating remote repositories
You can use the git remote add command to match a remote URL with a name. For example, you’d type the following in the command line:
This associates the name origin with the REMOTE_URL .
You can use the command git remote set-url to change a remote’s URL.
Choosing a URL for your remote repository
There are several ways to clone repositories available on GitHub.
When you view a repository while signed in to your account, the URLs you can use to clone the project onto your computer are available below the repository details.
For information on setting or changing your remote URL, see «Managing remote repositories.»
Cloning with HTTPS URLs
The https:// clone URLs are available on all repositories, regardless of visibility. https:// clone URLs work even if you are behind a firewall or proxy.
When you git clone , git fetch , git pull , or git push to a remote repository using HTTPS URLs on the command line, Git will ask for your GitHub username and password. When Git prompts you for your password, enter your personal access token (PAT) instead. Password-based authentication for Git has been removed, and using a PAT is more secure. For more information, see «Creating a personal access token.»
If you are accessing an organization that uses SAML SSO, you must also authorize your personal access token to access the organization before you authenticate. For more information, see «About authentication with SAML single sign-on» and «Authorizing a personal access token for use with SAML single sign-on.»
Tips:
- You can use a credential helper so Git will remember your GitHub credentials every time it talks to GitHub. For more information, see «Caching your GitHub credentials in Git.»
- To clone a repository without authenticating to GitHub on the command line, you can use GitHub Desktop to clone instead. For more information, see «Cloning a repository from GitHub to GitHub Desktop.»
If you’d rather use SSH but cannot connect over port 22, you might be able to use SSH over the HTTPS port. For more information, see «Using SSH over the HTTPS port.»
Cloning with SSH URLs
SSH URLs provide access to a Git repository via SSH, a secure protocol. To use these URLs, you must generate an SSH keypair on your computer and add the public key to your GitHub account. For more information, see «Connecting to GitHub with SSH.»
When you git clone , git fetch , git pull , or git push to a remote repository using SSH URLs, you’ll be prompted for a password and must provide your SSH key passphrase. For more information, see «Working with SSH key passphrases.»
If you are accessing an organization that uses SAML single sign-on (SSO), you must authorize your SSH key to access the organization before you authenticate. For more information, see «About authentication with SAML single sign-on» and «Authorizing an SSH key for use with SAML single sign-on.»
Tip: You can use an SSH URL to clone a repository to your computer, or as a secure way of deploying your code to production servers. You can also use SSH agent forwarding with your deploy script to avoid managing keys on the server. For more information, see «Using SSH Agent Forwarding.»
Cloning with GitHub CLI
You can also install GitHub CLI to use GitHub workflows in your terminal. For more information, see «About GitHub CLI.»
Cloning with Subversion
You can also use a Subversion client to access any repository on GitHub. Subversion offers a different feature set than Git. For more information, see «What are the differences between Subversion and Git?»
You can also access repositories on GitHub from Subversion clients. For more information, see «Support for Subversion clients.»
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
Источник
Cloning and forking repositories from GitHub Desktop
You can use GitHub Desktop to clone and fork repositories that exist on GitHub.
About local repositories
Repositories on GitHub are remote repositories. You can clone or fork a repository with GitHub Desktop to create a local repository on your computer.
You can create a local copy of any repository on GitHub that you have access to by cloning the repository. If you own a repository or have write permissions, you can sync between the local and remote locations. For more information, see «Syncing your branch.»
When you clone a repository, any changes you push to GitHub will affect the original repository. To make changes without affecting the original project, you can create a separate copy by forking the repository. You can create a pull request to propose that maintainers incorporate the changes in your fork into the original upstream repository. For more information, see «About forks.»
When you try to use GitHub Desktop to clone a repository that you do not have write access to, GitHub Desktop will prompt you to create a fork automatically. You can choose to use your fork to contribute to the original upstream repository or to work independently on your own project. Any existing forks default to contributing changes to their upstream repositories. You can modify this choice at any time. For more information, see «Managing fork behavior».
You can also clone a repository directly from GitHub or GitHub Enterprise. For more information, see «Cloning a repository from GitHub to GitHub Desktop».
Cloning a repository
In the File menu, click Clone Repository.
Click the tab that corresponds to the location of the repository you want to clone. You can also click URL to manually enter the repository location.
Choose the repository you want to clone from the list.
Click Choose. and navigate to a local path where you want to clone the repository.
Click Clone.
Forking a repository
If you clone a repository that you do not have write access to, GitHub Desktop will create a fork. After creating or cloning a fork, GitHub Desktop will ask how you are planning to use the fork.
In the File menu, click Clone Repository.
Click the tab that corresponds to the location of the repository you want to clone. You can also click URL to manually enter the repository location.
Choose the repository you want to clone from the list.
Click Choose. and navigate to a local path where you want to clone the repository.
Click Clone.
If you plan to use this fork for contributing to the original upstream repository, click To contribute to the parent project.
If you plan to use this fork for a project not connected to the upstream, click For my own purposes.
Click Continue.
Managing fork behavior
You can change how a fork behaves with the upstream repository in GitHub Desktop.
Open the Repository menu, then click Repository settings. .
Click Fork behavior, then select how you want to use the fork.
Click Save.
Creating an alias for a local repository
You can create an alias for a local repository to help differentiate between repositories of the same name in GitHub Desktop. Creating an alias does not affect the repository’s name on GitHub. In the repositories list, aliases appear in italics.
- In the upper-left corner of GitHub Desktop, to the right of the current repository name, click
.
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
Источник