How to clone git repo to the current directory

The most common way to clone git repository is to enter in the terminal command that looks like something like this:

git clone https://github.com/bessarabov/my_project.git

This command will create the directory "my_project" in the current directory and it will clone repo to that directory. Here is an example:

$ pwd
/Users/bessarabov
$ git clone https://github.com/bessarabov/my_project.git
Cloning into 'my_project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ ls -1a my_project/
.
..
.git
README.md
$

Command "pwd" prints the directory where you are now. The command "git clone ..." does the clone. And with "ls" command we check that there is a hidden ".git" directory that stores all the history and other meta information and there is a "README.md" file.

Specify directory to clone to

Sometimes you need to place git repository in some other directory. Here is an example:

$ pwd
/Users/bessarabov
$ git clone https://github.com/bessarabov/my_project.git project
Cloning into 'project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ ls -1a project
.
..
.git
README.md
$

As you can see here I used "git clone" command with two parameters:

Clone to the current directory

And sometimes you need to clone the git repo to the current directory. To specify the current directory the symbol dot is used. So to clone repo to the current directory you need to specify two parameters to git clone:

Here is an example:

$ mkdir the_project
$ cd the_project/
$ pwd
/Users/bessarabov/the_project
git clone https://github.com/bessarabov/my_project.git .
Cloning into '.'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ ls -1a
.
..
.git
README.md
$

Here I have created a new directory with the name "the_project", then I've entered it with "cd" command and did the clone with the command "git clone url .". The dot in this command is what makes git clone to the directory where I'm now.

Error "fatal: destination path '.' already exists and is not an empty directory"

Sometimes you can see error message:

$ git clone https://github.com/bessarabov/my_project.git .
fatal: destination path '.' already exists and is not an empty directory.

It means exactly what it it written in this message. You are trying to checkout repo to the directory that has some files in it. With the command "ls" you can check what files are in the current directory. It is also possible that there are some hidden files, so it is better to use "-a" option to make "ls" show all files including hidden:

$ ls -1a
.
..
.git
README.md

The "ls" command shows that git is right. The directory is not empty. There is a directory .git and a file README.md. You can permanent delete that files with the command "rm" (but do it only if you don't need those files, you will not be able to "undelete" them):

$ rm -rf .git README.md

After that the "git clone" will succeed:

$ git clone https://github.com/bessarabov/my_project.git .
Cloning into '.'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$

Recap

Ivan Bessarabov
ivan@bessarabov.ru

5 november 2019