How do I clone a Git repository into a specific folder?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Git lets you choose the destination folder when you clone a repository. The standard way is to pass the target directory as the second argument to git clone, which tells Git where to create the working copy instead of using the repository name from the URL.
Use git clone With a Destination Folder
The basic syntax is:
For example:
Git will create my-folder, fetch the repository into it, set up the origin remote, and check out the default branch there.
If you omit the second argument, Git uses the repository name from the URL instead.
What a Normal Clone Sets Up
A clone does more than copy files. It also:
- creates the
.gitdirectory - records the source repository as the
originremote - downloads remote refs
- checks out a working tree
That is why git clone is the right tool for a new local copy. It handles repository initialization and remote setup in one step.
Combine the Folder Argument With Other Clone Options
You can still choose a target folder while using other useful clone flags.
Clone a specific branch:
Clone only the latest snapshot with shallow history:
These options are common when you want a custom local layout and also want to control what gets fetched.
What If the Folder Already Exists
git clone expects the destination to be absent or empty enough for Git to initialize it safely. If the folder already exists and contains files, clone usually fails rather than merging the repository into that directory.
If you already have a directory and want to connect it to a remote repository, the process is different:
This is not the same as cloning into a fresh folder. It is the manual setup path for an already-existing directory.
If the remote default branch is not main, use the correct branch name instead.
Why a Custom Folder Name Is Useful
Choosing a specific local folder is handy when:
- you want a shorter or clearer directory name
- you keep multiple working copies of the same repository
- your build or deployment scripts expect a certain path
- you want separate clean and experimental worktrees
For example:
That is often easier than constantly resetting one working directory for different tasks.
Common Pitfalls
- Forgetting that the target folder is simply the second argument to
git clone. - Trying to clone into a non-empty existing folder and expecting Git to merge files automatically.
- Assuming the remote default branch is always
main. - Using a shallow clone and later forgetting why older history is missing.
- Mixing up "clone into a new folder" with "initialize an existing folder and attach it to a remote."
Summary
- Pass the destination folder as the second argument to
git clone. - Git will create that folder and set up the repository there.
- You can combine the folder argument with options such as
-bor--depth. - If the folder already exists and contains files, use
git initplusgit remote addinstead of a normal clone. - A custom target folder is useful for organization, parallel working copies, and predictable local paths.

