To clone a Git repository into a specific folder, append the target directory name to the git clone
command. Here’s how:
Basic Syntax
git clone <repository-url> <target-directory>
Example:
git clone https://github.com/user/repo.git ./my-project
- Clones the repository into the folder
my-project
(created if it doesn’t exist). - If
my-project
already exists, it must be empty—otherwise, Git will throw an error.
Key Notes
- Target Directory Must Be Empty or Nonexistent
Git will fail if the target directory exists and is not empty. To force overwrite, delete the directory first:
rm -rf my-project && git clone https://github.com/user/repo.git my-project
- Use Absolute or Relative Paths
git clone https://github.com/user/repo.git /absolute/path/to/dir
git clone https://github.com/user/repo.git ../relative/path
- Spaces in Directory Names
Wrap the path in quotes:
git clone https://github.com/user/repo.git "My Project Folder"
Cloning into the Current Directory
You can clone into the current folder (.
) if it’s empty:
mkdir my-project && cd my-project
git clone https://github.com/user/repo.git .
Example Workflow
# Clone into a specific directory
git clone https://github.com/axios/axios.git ./http-client
# Verify
cd http-client
ls # Shows cloned files (e.g., package.json, README.md)
Troubleshooting
- Error:
fatal: destination path already exists and is not an empty directory
Delete the existing directory or choose a different name. - Permission Issues
Ensure you have write access to the target directory.
By specifying the directory explicitly, you control where Git clones the repository.