This post will discuss how to clone a Git repository into a specific folder.

The standard approach to clone is repository is using the git-clone command. But when you simply clone a repository with git clone <repository>, it creates a new directory with repository name at the current path in the file system and clones the repository inside it.

If you want to clone the remote repository into a specific directory, you can specify its path:

$ git clone <repository> <path>

Here, <path> is the path of the directory to clone into. This is demonstrated below, where cloning is done into an existing local directory.

git clone directory

 
If you want to clone the git repository into the current directory, you can do like:

$ git clone <repository> .

Here, the dot (.) represents the current directory.

git clone .

 
Alternatively, you can use the git [-C ] option to specify the root directory. This works as git will assume the <path> as the current working directory.

$ git -C <path> clone <repository>

Note that this creates a new directory with repository name at the specified directory and clone the repository inside it, as shown below:

git -C directory clone

 
However, you can only clone into an existing directory only when it is empty. Otherwise, git will complain that the destination path already exists and is not an empty directory.

git error – not an empty directory

 
If the destination is not empty, you can do like:

# create and initialize an empty repository
$ git init
 
# add a remote named origin for the repository at <repository>
$ git remote add origin <repository>
 
# do a git-fetch
$ git fetch
 
# check out the master branch
$ git checkout master

This approach is demonstrated below:

git fetch

That’s all about cloning a Git repository into a specific folder.