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:
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
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:
Or a relative path with subdirectories (Git creates intermediate directories as needed):
What git clone sets up
A clone does more than copy files:
| 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 |
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
This checks out develop instead of the remote default. Other branches are still fetched; only the initial checkout changes.
Shallow clone (latest snapshot only)
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
Clone without checking out a working tree
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:
This downloads metadata for the whole repo but only materializes files under packages/frontend and docs/.
Clone via SSH
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:
. 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:
Set it up manually instead:
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:
Multiple working copies of one repository
Different target folders give you independent working copies for different purposes:
Changes in one do not affect the others.
A lighter alternative is git worktree:
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
| 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 |
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 initandgit remote addworkflow. - Looking for a
--directoryor--targetflag. It doesn't exist. The folder is just the second positional argument:git clone URL folder. - Assuming the default branch is
mainin the manual setup. Some repositories usemaster,developor a custom default.git remote show originshows the remote HEAD. - Using
--depth 1for 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 .withgit clone URL..and./are equivalent, but leaving the argument off creates a subdirectory named after the repo.
Related reading
- How do I clone all remote branches?
- 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 remove local (untracked) files from the current Git working tree?
- How do I resolve merge conflicts in a Git repository?
- How do I squash my last N commits together?
- How to add images to README.md on GitHub?
- How to determine the URL that a local Git repository was originally cloned from
.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 courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.