Cloning a Git repository into an existing directory

Greg Foster
Greg Foster
Graphite software engineer

Cloning a Git repository typically involves creating a new directory that contains the repository's files. However, there are scenarios where you might want to clone a repository into an existing directory.

This guide will walk you through the steps and considerations for cloning a Git repository into an existing directory, including workarounds for non-empty directories and how to force Git to overwrite existing files if necessary.

Git does not allow you to directly clone a repository into a non-empty directory using the standard git clone command. This limitation is designed to prevent unintended file overwrites and conflicts. However, there are legitimate cases where you might need to merge a repository with an existing directory's contents or clone into a preconfigured directory structure.

If the existing directory is empty, cloning into it is straightforward. You can specify the directory into which you want to clone the repository using the git clone command:

Terminal
git clone <repository-url> <existing-directory-path>

This command tells Git to clone the repository from <repository-url> into the specified <existing-directory-path>. If the directory is empty, Git will proceed without issues.

To clone a repository into your current working directory, you can use the following command:

Terminal
git clone <repository-url> .

Make sure the current directory is empty, as Git will refuse to clone into a directory that contains any other existing files.

To clone a repository into a non-empty directory, you can use a two-step process. First, clone the repository into a temporary directory, then move the Git metadata into the target directory and check out the files.

  1. Create a temporary directory:
Terminal
mkdir temp-dir
  1. Clone into the temporary directory:
Terminal
git clone <repository-url> temp-dir
  1. Move the .git Folder:
Terminal
mv temp-dir/.git <existing-directory-path>/
  1. Remove the temporary directory:
Terminal
rm -rf temp-dir
  1. Checkout the repository files:

Navigate to the existing directory and checkout the files:

Terminal
cd <existing-directory-path>
git checkout .

This will update the directory to match the current state of the HEAD commit, keeping the pre-existing files intact, while retrieving all of the other tracked remote files.

Git doesn't directly support forcing a clone that overwrites existing files in the target directory.

If you absolutely need to overwrite files, you should manually delete or backup the existing files before cloning. Alternatively, you can fetch the repository contents into an existing Git repository as described above and then manually resolve any file conflicts.

  • Data loss: Overwriting files in an existing directory can lead to data loss. Always ensure you have backups of important files before attempting to clone into or merge a Git repository with an existing directory.
  • Repository conflicts: When merging a repository with an existing directory's contents, be prepared to handle conflicts, especially if there are files with the same names but different contents.
  • Hidden .git directory: Remember that the .git directory contains all of the repository's history and metadata. When moving this directory, you're effectively turning your existing directory into a Git repository.

For further reading on cloning git repositories see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2