Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Clone a Repository

git clone https://github.com/user/project.git

Git creates a folder named project (the address without .git), downloads every commit into it, and checks out the default branch. Go into the folder before you run other Git commands:

cd project
git log --oneline -3
git remote -v

If git is not found, install it first; see how to install Git on Linux.

HTTPS or SSH Address

Most hosting services show two kinds of address on the repository page.

HTTPSSSH
Starts withhttps://the user git
LoginAccess tokenSSH key
Public repositoryNo login neededKey still needed
Good forQuick readsServers and pushing

The SSH address shown on the page starts with the user name git, then the host name, a colon, and the repository path. Git also accepts the same address in URL form:

git clone ssh://git@your_git_host/user/project.git

Replace your_git_host with the host name of your service, or with a host alias from your SSH config file. GitHub and many other services no longer accept your account password for Git over HTTPS; they ask for an access token instead.

Choose the Folder

Add a folder name after the address:

git clone https://github.com/user/project.git site
git clone https://github.com/user/project.git .

The first line clones into site. The dot clones into the current folder, which must be empty.

Clone a Specific Branch

git clone -b develop https://github.com/user/project.git

-b checks out develop instead of the default branch. The other branches are still downloaded. -b also accepts a tag, which leaves you on that release without a branch.

Shallow Clone with --depth

git clone --depth 1 https://github.com/user/project.git

Only the latest commit is downloaded, which is faster for a large repository when you only need the files. A shallow clone fetches a single branch unless you add --no-single-branch. To get the full history later, run git fetch --unshallow.

Include Submodules

Some projects pull other repositories in as submodules. Clone them in the same step:

git clone --recurse-submodules https://github.com/user/project.git

If you already cloned without them, the submodule folders are empty. Fill them with:

git submodule update --init --recursive

Clone onto a Server with a Deploy Key

For a private repository on a server, a read-only deploy key is safer than your account password or a personal token. The key setup is in how to use SSH keys with GitHub. Then clone as the user that runs the site, not as root. Create the folder with sudo, then run the last two lines as that user (here deploy):

sudo mkdir -p /var/www/example.com
sudo chown deploy:deploy /var/www/example.com
cd /var/www/example.com
git clone ssh://git@your_git_host/user/project.git .

Avoid storing an HTTPS token with credential.helper store on a shared server: it saves the token unencrypted in ~/.git-credentials, protected only by file permissions.

Common Errors

  • Permission denied (publickey): the server's key is missing or not added to the hosting service. See how to fix permission denied (publickey).
  • Repository not found: a typo in the address, or your account or key has no access to a private repository.
  • Destination path already exists: the target folder exists and is not empty. Pick another name or clean the folder.
  • Host key verification failed: SSH does not know or does not trust the host key of the Git service yet. See how to fix host key verification failed.
Was this answer helpful?
Loading...