Clone a Git repository into a specific folder
Pass a destination directory to git clone, clone into an empty current folder, or connect an existing folder to a remote after preserving local files.
Add the destination after the repository URL: git clone URL my-folder. Git uses that path instead of deriving a directory name from the URL. Use . to clone into the current directory, which must be empty.
# Use a custom directory name.
git clone https://github.com/org/repo.git my-folder
# Run this alternative from an existing empty directory.
git clone https://github.com/org/repo.git .
# An absolute path works too, if its parent is writable.
git clone https://github.com/org/repo.git /opt/apps/api
Replace the example repository with your own. Quote destination paths that contain spaces.
Destination requirements
Git can create a destination that does not yet exist, or use an existing empty directory. A nonempty destination is rejected, including one that contains hidden files. There is no clone option that safely merges arbitrary existing files with the incoming checkout.
If you need to keep existing files, the easiest approach is to clone alongside them and copy selected files after comparing their contents. Another option, in a folder that is not already a repository, is:
git init .
git remote add origin https://github.com/org/repo.git
git fetch origin
git switch --track -c main origin/main
This assumes the remote branch is main and no local branch with that name has been created. Checkout normally refuses to overwrite conflicting non-ignored untracked files, but ignored files can be overwritten during a branch switch. Back up all existing content first, including ignored files, then move or reconcile conflicts. Do not use a force flag to bypass the comparison.
Combine the destination with clone options
# Limit history depth; this normally implies a single-branch clone.
git clone --depth 1 https://github.com/org/repo.git dir
# Check out develop initially.
git clone -b develop https://github.com/org/repo.git dir
# Initialize the repository's submodules as well.
git clone --recurse-submodules https://github.com/org/repo.git dir
These are separate examples. -b develop selects the initial branch; it does not itself limit fetching to that branch.
FAQ
Can I move the clone afterward?
A conventional clone with its own .git directory can usually be moved as a whole, for example with mv my-folder correct-folder. Check external paths, submodules, or repository layouts that use a separate Git directory before treating a move as entirely self-contained.
Can I clone the same repository twice?
Yes. Each clone has its own working tree, configuration, and local branches. If you mainly need another checkout of an existing local repository, git worktree add ../dir2 branch-name shares the object database; the branch must not already be checked out elsewhere.
Why is an apparently empty directory rejected?
Check for hidden files. Move anything you need to retain, or use a different empty destination.
See git-clone, git-init, and git-worktree.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan