Git: Clone a Repository

One of the many benefits of using version control software like Git is how easily you can copy the entire contents and history of a project with a simple command in your terminal. Once on your local machine, you can then make the changes/additions/deletions that you want, and push it back to the origin repo.

In this short article we'll see how you can clone a Git repository from a remote (or local) source.

Cloning Repositories

In order to clone a remote repository you'll want to use the git clone command, which is typically used in this fashion:

$ git clone <repo-url>

This will clone the repository in to your current working directory using the name of the repo as the destination directory. In order to clone it in to a different directory, you can specify one as the second parameter to clone:

$ git clone <repo-url> <directory>

An example of this command for a public GitHub repo would look like the following:

$ git clone [email protected]:scottwrobinson/twentyjs.git twentyjs-clone
Cloning into 'twentyjs-clone'...
remote: Enumerating objects: 48, done.
remote: Total 48 (delta 0), reused 0 (delta 0), pack-reused 48
Receiving objects: 100% (48/48), 9.35 KiB | 0 bytes/s, done.
Resolving deltas: 100% (22/22), done.

Like all Git commands, there are quite a few flags that can be used here. Below are some of the more commonly used ones for git clone:

  • -l or --local: This tells Git that the repo we're cloning is on our local machine, which bypasses the normal "git aware" transport mechanism. The files under .git/objects/ are also hard-linked to the original repo, which saves disk space. This flag is unnecessary if the repo is specified as a local path, but must be explicitly specified if the local repo is specified as a URL.
  • --no-tags: Do not download any of the repo's tags. This setting is also saved so that subsequent git pull and git fetch calls do not download tags.
  • -o <name> or --origin <name>: Rename the remote repository for this clone from "origin" to whatever is specified by <name>.
  • -n or --no-checkout: Don't check out HEAD after cloning
  • --bare: Places the files from <directory>/.git directly in to the location specified by <directory>. This is good for when you need a copy of the repo, but you don't need to work directly out of it for development purposes.

Remember, I've only mentioned the most commonly used flags specific to the clone command. There are quite a few more, which you can check out using the git help clone command in your terminal.

Cloning Local Repositories

As you've probably guessed from some of the flags mentioned above, you can also clone repositories that are already on your local machine. This is done by specifying the repo's directory path instead of a URL. Here is an example of what that would look like:

$ git clone /Users/scott/sandbox/twentyjs /Users/scott/projects/twentyjs

With this command the twentyjs Git project would be copied in to the new projects/twentyjs directory. You can ommit the destination path, but keep in mind that you either need to be in a different working directory than where it resides or you need to give it a different name to avoid conflicts.

Other Protocols

There are a number of ways to specify which repository you want to clone, wheter you're cloning a local or remote repo. The differences are in the protocol you specify, which also must be supported by the server hosting the repo. When choosing a protocol, be sure to consider security requirements and use the most secure protocols when necessary or able.

A few examples of protocols that are natively supported by Git are shown below:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

  • git://[:port]/path/to/repo/project.git
  • ssh://[user@][:port]/path/to/repo/project.git
  • http[s]://[:port]/path/to/repo/project.git
  • ftp[s]://[:port]/path/to/repo/project.git (not recommended due to deprecation)

Other URL forms are also supported, depending on the protocol being used, like this one for SSH:

As for local repositories, you can use one of the following forms:

  • /path/to/repo/project.git
  • file:///path/to/repo/project.git
Last Updated: September 7th, 2019
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

© 2013-2024 Stack Abuse. All rights reserved.