How do I clone a Git repository into a specific folder?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To clone a Git repository into a specific folder, pass the target directory as the second argument to git clone:
Git will create that folder, initialize a .git directory inside it, fetch the remote history, and check out the default branch. If you omit the second argument, Git derives the folder name from the repository URL. The rest of this article covers the variations, edge cases, and related workflows you will encounter when controlling where your cloned repositories land.
Basic Clone Into a Named Folder
This creates a directory called my-project in your current working directory. Inside it, you get the full working tree plus the .git metadata directory.
The target folder can also be an absolute path:
Or a relative path with subdirectories (Git creates intermediate directories as needed):
What git clone Actually Sets Up
A clone does more than copy files. Understanding what it creates helps explain why it is the right tool for getting a new local copy:
| What Gets Created | Purpose |
| Working tree (your files) | The checked-out snapshot of the default branch |
.git/ directory | Full repository history, objects, refs, hooks |
origin remote | Points back to the source URL for fetch/push |
| Remote-tracking branches | origin/main, origin/develop, etc. |
| Checked-out branch | Local branch tracking the remote default |
A clone is a complete, independent copy of the repository. You can work offline, switch branches, and view full history without any network access after the initial clone.
Combining With Other Clone Options
The target folder argument works alongside every other git clone flag.
Clone a Specific Branch
This checks out the develop branch instead of the remote's default. The other branches are still fetched; only the initial checkout changes.
Shallow Clone (Latest Snapshot Only)
A shallow clone fetches only the most recent commit. This is significantly faster for large repositories when you only need the current state, not the full history. It is commonly used in CI/CD pipelines.
Shallow Clone of a Specific Branch
Clone Without Checking Out a Working Tree
This fetches the repository but does not populate the working tree. Useful when you want to set up sparse checkout rules before materializing files.
Sparse Checkout (Specific Subdirectories Only)
For monorepos where you only need certain paths:
This downloads metadata for the full repo but only materializes the files under packages/frontend and docs/.
Clone via SSH
SSH cloning is the standard for repositories you have push access to. The target folder argument works identically with SSH and HTTPS URLs.
Cloning Into the Current Directory
If you want to clone into the current (empty) directory without creating a subdirectory:
The . target tells Git to use the current directory. This only works if the directory is empty. If it contains any files (even hidden ones like .DS_Store), the clone will fail.
What If the Folder Already Exists?
git clone refuses to clone into a non-empty directory. If the target folder exists and contains files, you will see an error like:
For this situation, you need the manual setup path:
This initializes a new repository in the existing folder, connects it to the remote, fetches the history, and checks out the main branch. It is not equivalent to a fresh clone because existing files may conflict with tracked files.
Another option is to clone into a temporary folder and then move the contents:
Multiple Working Copies
Specifying different target folders lets you maintain multiple working copies of the same repository for different purposes:
Each clone is fully independent. Changes in one do not affect the others.
For a lighter-weight alternative, consider git worktree:
Worktrees share the same .git object store, so they use less disk space and fetch history only once. However, each worktree must be on a different branch.
Quick Reference
| Task | Command |
| Clone into a named folder | git clone URL my-folder |
| Clone into current (empty) dir | git clone URL . |
| Clone specific branch | git clone -b branch URL my-folder |
| Shallow clone | git clone --depth 1 URL my-folder |
| Clone via SSH | git clone git@host:repo.git my-folder |
| Sparse checkout | git clone --sparse URL my-folder |
| No working tree | git clone --no-checkout URL my-folder |
Common Pitfalls
Trying to clone into a non-empty directory and expecting Git to merge the repository contents with existing files is the most common mistake. Git intentionally refuses this to prevent data loss. Use the git init and git remote add workflow for existing directories.
Forgetting that the target folder is just the second positional argument leads people to search for a --directory or --target flag that does not exist. The syntax is simply git clone URL folder.
Assuming the default branch is always main can cause errors in the manual setup path. Some repositories use master, develop, or a custom default. Check with git remote show origin to see the remote HEAD.
Using --depth 1 for a repository you plan to contribute to creates problems later. Shallow clones lack the history needed for rebasing, blame, and log commands. Only use shallow clones for read-only or CI contexts.
Cloning large repositories over HTTPS when SSH keys are available wastes time on repeated credential prompts and misses out on connection multiplexing benefits.
Mixing up git clone URL . with git clone URL ./ does not cause issues (they are equivalent), but confusing either with git clone URL (no second argument) means Git will create a subdirectory named after the repo instead of using the current directory.
Summary
- Pass the destination folder as the second argument to
git clone:git clone URL my-folder. - The target can be a relative path, absolute path, or
.for the current directory. - Combine with
-b,--depth,--sparse, or--no-checkoutfor specialized clone workflows. git clonerequires the target to be empty or non-existent. For existing directories, usegit initplusgit remote add.- Consider
git worktreeinstead of multiple full clones when you need parallel working directories for the same repository.
Related reading
- How do I clone all remote branches?
- How do I discard unstaged changes in Git?
- How do I find and restore a deleted file in a Git repository?
- How do I get the current branch name in Git?
- How do I make git use the editor of my choice for editing commit messages?
- How do I modify a specific commit?
- How do I push a new local branch to a remote Git repository and track it too?
- How do I remove local (untracked) files from the current Git working tree?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.