Git How to Clone a Repository into a Specific Folder

To clone a GitHub repo to a specific folder, specify the folder’s name after the git clone command.

git clone https://github.com/user/repo.git folder-name

There’s a great chance you came here because you want to clone a GitHub repository to the current folder you’re in. To do this, run:

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

Notice the “.” at the end of the command. This tells the git clone command to clone to the present working directory! By specifying the dot, Git does not create a new folder but clones the contents into the folder you’re in.

If this answer isn’t enough, keep on reading. This is a complete guide to cloning a repo in a specific folder. For example, you’ll learn how to push an already-created project to a new remote repo. Besides, you will learn how git clone/git init commands work and what’s the .git folder that’s present in your Git projects.

What Does git clone Really Do?

When you call git init, there are a couple of things that take place. Let’s demonstrate this with an example.

Let’s say you have a repository called code_project at https://github.com/user/code_project.git and you clone it with:

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

When you run the above git clone command:

  1. A new folder called code_project is created in the present working directory.
  2. A folder called .git is initialized inside the code_project folder.
  3. All the data is pulled to the code_project folder from the remote repository.
  4. Git checks out a copy of the latest version of the project.
  5. The code_project folder now has the project files and you can start working on them.

What Is the .git Folder?

In Git, the .git folder is what makes a directory a Git repository. Whenever you run git init or git clone, an invisible folder called .git is created in the project folder. The .git folder comes with subfolders objectsrefs/heads, and refs/tags with some template files that enable versioning of the project. (You can see the contents of the .git folder by running ls .git on the command line)

But here’s the important part. The .git folder doesn’t care in which folder it’s in. In other words, you can freely move it around.

If you remove the .git directory from a project, the project is no longer under version control and all the (local) versioning history is lost. This is why there’s no “undo” command for git init because you can just delete the .git folder to get rid of Git in the project.

Also, this is why you can freely rename the project folder. The .git folder lives inside the project folder and doesn’t care about the name of the governing parent folder.

How to Clone a Repo to a Particular Folder?

Let’s have a look at how to clone a GitHub repository to a particular folder. In other words, how to not create a new folder with Git clone but clone the contents instead.

1. Clone to a Folder with a Different Name Directly

To clone a remote GitHub repository to a folder with a different name than the repository name, you can run the command you already saw in the intro:

git clone https://github.com/user/repo.git folder-name

This clones a project called repo to a folder called folder-name.

Example

Let’s see a concrete example.

I have a (private) GitHub repo at https://github.com/artturijalli/exampleProject.git.

Now, let’s run the git clone command to clone the project into a folder called cool-project.

git clone https://github.com/artturijalli/exampleProject.git cool-project

Now the exampleProject is located on my desktop and lives inside a cool-project directory.

2. Move the .git Folder to a New Folder

If you’ve already run the git clone command and want to move the Git versioning to another folder, move the .git folder to a new location. As you learned, the .git folder is an invisible folder that makes a project a GitHub repository.

To move the .git folder, you need to move both the normal files as well as the special dot files. So to move the entire .git folder to another location, you need to run these two commands:

mv path/to/existing/folder/* path/to/new/folder/
mv path/to/existing/folder/.* path/to/new/folder/

Summary

Today you learned how to clone a GitHub project into a specific folder with a different name than the remote repository. Most of the time, you might want to do this to clone a project into the current folder without creating a new subfolder. To do this, specify the path to which you want to clone as the current folder (simply by using a dot).

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

Thanks for reading. Happy coding!

Read Also

Scroll to Top