How do I clone a Git repository directly into my current directory instead of creating a new subdirectory?
The command git clone git@github.com:whatever creates a directory named whatever containing a Git repository:
./
whatever/
.git
I want the contents of the Git repository cloned into my current directory ./ instead:
./
.git
What Git command or option should I use to achieve this directory structure?
To clone a Git repository directly into your current directory instead of creating a new subdirectory, use the dot (.) at the end of the git clone command: git clone git@github.com:user/repo.git .. This tells Git to place the repository contents directly into your current working directory rather than creating a new subdirectory.
Contents
- Using the Dot Notation
- Alternative Methods
- Cloning Specific Subdirectories
- Practical Examples
- Troubleshooting Common Issues
Using the Dot Notation
The simplest and most common method is to use a dot (.) as the second argument in the git clone command:
git clone git@github.com:user/repo.git .
This notation explicitly tells Git to clone the repository into the current directory. As Future Studio explains, “put a dot after the git repository URL to clone into the current directory.”
The dot represents the current working directory, preventing Git from creating the default subdirectory structure. Instead of:
./
whatever/
.git/
You’ll get:
./
.git/
[repository files and directories]
Note: If your current directory is not empty, Git will warn you about overwriting existing files. Make sure you want to clone into the current directory before proceeding.
Alternative Methods
Using the -C Parameter
You can use the -C parameter to change directory and then clone:
git -C /path/to/current/directory clone git@github.com:user/repo.git .
This approach is useful when you want to clone into a directory without first changing into it manually.
Cloning to Temporary Directory and Moving
If you’re concerned about potential conflicts with existing files, you can clone to a temporary directory and then move the contents:
git clone git@github.com:user/repo.git tmp
mv tmp/* .
mv tmp/.* . 2>/dev/null || true
rmdir tmp
The -f (force) option can be added to the mv command if you don’t care about overwriting existing files:
git clone git@github.com:user/repo.git tmp && mv -f tmp/* .
As shown in the Stack Overflow answer, this method is more verbose but provides more control over the cloning process.
Using Git Sparse Checkout
For more advanced scenarios, you can use Git’s sparse-checkout feature to only clone specific directories:
git clone --no-checkout --depth=1 git@github.com:user/repo.git .
git sparse-checkout set path/to/subdirectory
git checkout
This method is particularly useful when you only need specific subdirectories from a large repository, as mentioned in the Baeldung article.
Cloning Specific Subdirectories
If you need to clone only specific subdirectories rather than the entire repository, you can combine the dot notation with sparse checkout:
git clone --no-checkout --depth=1 git@github.com:user/repo.git .
git sparse-checkout set path/to/subdirectory
git checkout
Or using the one-liner approach:
git clone --depth 1 --no-checkout git@github.com:user/repo.git && \
cd repo && \
git sparse-checkout set path/to/subdirectory && \
git checkout && \
cd .. && \
mv repo/* . && \
mv repo/.* . 2>/dev/null || true && \
rmdir repo
This approach is especially useful for large repositories where you only need specific parts of the codebase.
Practical Examples
Example 1: Basic Clone to Current Directory
# Navigate to your desired directory
cd /path/to/your/project
# Clone directly into current directory
git clone https://github.com:user/repo.git .
# Verify the structure
ls -la
Example 2: Cloning with Branch Specification
# Clone specific branch directly into current directory
git clone --branch main https://github.com:user/repo.git .
Example 3: Cloning with Depth Limit
# Clone only the latest commit directly into current directory
git clone --depth 1 https://github.com:user/repo.git .
Example 4: Cloning with Sparse Checkout
# Clone only specific subdirectory
git clone --no-checkout --depth 1 https://github.com:user/repo.git .
git sparse-checkout set src/components
git checkout
Troubleshooting Common Issues
Directory Not Empty Error
If your current directory is not empty, Git will warn you about potential file conflicts:
error: destination path '.' already exists and is not an empty directory.
To resolve this, either:
- Clean the current directory before cloning
- Use the temporary directory method mentioned above
- Initialize a new Git repository in the current directory instead
Permission Issues
On some systems, you might encounter permission issues when using mv with hidden files. The 2>/dev/null || true part in the temporary directory method handles this by ignoring errors when moving hidden files.
Git Version Compatibility
The sparse-checkout feature requires Git version 2.25.0 or later. Check your Git version with:
git --version
If you have an older version, consider upgrading or using the temporary directory method instead.
Conclusion
Cloning a Git repository directly into your current directory is straightforward once you know the right techniques. Here are the key takeaways:
- Use
git clone repo.git .with a dot to clone directly into the current directory - The dot notation is the simplest and most common method
- For complex scenarios, consider using sparse-checkout or the temporary directory approach
- Always be cautious when cloning into non-empty directories to avoid overwriting files
- Different methods serve different use cases - choose the one that best fits your workflow
Whether you’re working on a personal project or collaborating with a team, understanding how to control Git’s cloning behavior can save you time and keep your project structure clean and organized.
Sources
- Future Studio - git — Clone Into Current Directory
- Stack Overflow - How to get Git to clone into current directory
- KodeKloud - How to Clone a Git Repository to a Specific Folder
- Baeldung - Clone Only a Subdirectory of a Git Repository
- Git Documentation - git-clone
- Gitscripts - Git Clone Into Current Directory: A Quick Guide