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.
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
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
--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 fetchcalls do not download tags.
--origin <name>: Rename the remote repository for this clone from "origin" to whatever is specified by
--no-checkout: Don't check out HEAD after cloning
--bare: Places the files from
<directory>/.gitdirectly 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.
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.
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!
A few examples of protocols that are natively supported by Git are shown below:
ftp[s]://example.com[: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: