How to Clone into a Non-Empty Directory

Cloning a Git Repository to a non-empty directory is useful if you have a remote repository that contains some files, and you want to merge them with the files in the local directory. In this tutorial, you will get the answers to your problem.

You should execute the following command line:

git init
git remote add origin PATH/TO/REPO
git fetch

The following step is required when the versioned files existed in the path before git init:

git reset origin/master

Then, run the following:

git checkout -t origin/master
The -t option sets the upstream branch.

So, your remote repository will be successfully cloned to the non-empty directory.

Working with Remotes

For collaborating on a Git project, you should know how to manage remote repositories. Remote repositories are versions of your project hosted on the Internet. You can have several of them that can be either read-only or read/write for you. Working with other members of the team involves the process of pushing and pulling data to and from them. You should master the techniques of adding or removing remote repositories. You can call the git remote command to see which remote servers you have configured. It is used for creating, viewing, and removing connections to other repositories. When the repository is cloned, you should see “origin”, which is the default name that Git gives to the server you cloned from. Local copy of the central directory is an easy method of pulling upstream modifications or publishing local commits.


The git fetch command is used for downloading commits, files, and references from a remote repository into the local repository. Both git fetch and git pull commands are designed for downloading the content from the remote repository. The git fetch command does not force to merge the changes into the repository; it just shows the progression of the central history. The fetched content does not affect the local work. It should be checked out with the git checkout command, making it safe to review commits before merging them into the local repository.

Do you find this helpful?

Related articles