repository
folder
clone
git

How to git clone into a specific folder (or a different folder name)

Codemia Knowledge Hub ·

To clone a Git repository into a specific folder, pass the folder as the second argument to git clone:

bash
git clone <repository-url> <target-folder>

Git creates that folder, initializes a .git directory inside it, fetches the remote history and checks out the default branch. Without the second argument, Git names the folder after the repository URL.

Clone into a folder with a different name

bash
git clone https://github.com/example/project.git my-project

This creates my-project in the current working directory, containing the full working tree plus the .git metadata directory.

The target can be an absolute path:

bash
git clone https://github.com/example/project.git /opt/apps/my-project

Or a relative path with subdirectories (Git creates intermediate directories as needed):

bash
git clone https://github.com/example/project.git projects/backend/my-project

What git clone sets up

A clone does more than copy files:

What Gets CreatedPurpose
Working tree (your files)The checked-out snapshot of the default branch
.git/ directoryFull repository history, objects, refs, hooks
origin remotePoints back to the source URL for fetch/push
Remote-tracking branchesorigin/main, origin/develop, etc.
Checked-out branchLocal branch tracking the remote default

It is a complete, independent copy. After the initial clone you can work offline, switch branches and view full history with no network access.

Combining the folder argument with other clone options

The target folder works with every other git clone flag.

Clone a specific branch

bash
git clone -b develop https://github.com/example/project.git my-project

This checks out develop instead of the remote default. Other branches are still fetched; only the initial checkout changes.

Shallow clone (latest snapshot only)

bash
git clone --depth 1 https://github.com/example/project.git my-project

Only the most recent commit is fetched, which is much faster for large repositories when you only need the current state. It is common in CI/CD pipelines.

Shallow clone of a specific branch

bash
git clone --depth 1 -b v2.0 https://github.com/example/project.git my-project

Clone without checking out a working tree

bash
git clone --no-checkout https://github.com/example/project.git my-project

The repository is fetched but the working tree is left empty, which is useful when you want to set up sparse checkout rules before materializing files.

Sparse checkout (specific subdirectories only)

For monorepos where you need only certain paths:

bash
git clone --filter=blob:none --sparse https://github.com/example/monorepo.git my-project
cd my-project
git sparse-checkout set packages/frontend docs/

This downloads metadata for the whole repo but only materializes files under packages/frontend and docs/.

Clone via SSH

bash
git clone [email protected]:example/project.git my-project

SSH is the standard for repositories you can push to. The target folder argument works the same with SSH and HTTPS URLs.

Cloning into the current directory

To clone into the current (empty) directory without creating a subdirectory:

bash
mkdir my-project && cd my-project
git clone https://github.com/example/project.git .

. tells Git to use the current directory. It only works if the directory is empty; any file (even a hidden one like .DS_Store) makes the clone fail.

When the folder already exists and is not empty

git clone refuses to clone into a non-empty directory:

 
fatal: destination path 'my-project' already exists and is not an empty directory.

Set it up manually instead:

bash
1cd existing-folder
2git init
3git remote add origin https://github.com/example/project.git
4git fetch origin
5git checkout -t origin/main

This initializes a repository in the existing folder, connects the remote, fetches history and checks out the main branch. It is not the same as a fresh clone, because existing files may conflict with tracked files.

Alternatively, clone into a temporary folder and move the contents:

bash
git clone https://github.com/example/project.git /tmp/repo-temp
cp -r /tmp/repo-temp/. existing-folder/
rm -rf /tmp/repo-temp

Multiple working copies of one repository

Different target folders give you independent working copies for different purposes:

bash
git clone https://github.com/example/project.git project-dev
git clone https://github.com/example/project.git project-review
git clone https://github.com/example/project.git project-experiment

Changes in one do not affect the others.

A lighter alternative is git worktree:

bash
git clone https://github.com/example/project.git project-main
cd project-main
git worktree add ../project-feature feature-branch

Worktrees share one .git object store, so they use less disk space and fetch history only once. Each worktree must be on a different branch.

Quick reference

TaskCommand
Clone into a named foldergit clone URL my-folder
Clone into current (empty) dirgit clone URL .
Clone specific branchgit clone -b branch URL my-folder
Shallow clonegit clone --depth 1 URL my-folder
Clone via SSHgit clone git@host:repo.git my-folder
Sparse checkoutgit clone --sparse URL my-folder
No working treegit clone --no-checkout URL my-folder

Mistakes when choosing a clone folder

  • Expecting Git to merge into a non-empty directory. Git refuses on purpose to prevent data loss. Use the git init and git remote add workflow.
  • Looking for a --directory or --target flag. It doesn't exist. The folder is just the second positional argument: git clone URL folder.
  • Assuming the default branch is main in the manual setup. Some repositories use master, develop or a custom default. git remote show origin shows the remote HEAD.
  • Using --depth 1 for a repository you will contribute to. Shallow clones lack the history needed for rebasing, blame and log. Keep them for read-only or CI use.
  • Cloning large repositories over HTTPS when SSH keys are available. You waste time on repeated credential prompts and miss connection multiplexing.
  • Confusing git clone URL . with git clone URL. . and ./ are equivalent, but leaving the argument off creates a subdirectory named after the repo.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions