Git

How to Clone Into a Non-Empty Git Directory

Git is a freely available decentralized version control system that is utilized for tracking the files or projects when multiple people are working on the same project. On this platform, you can work locally and then commit the changes to the remote directory. It also permits you to clone a whole project or milestone. However, for beginners, sometimes it becomes challenging to clone into a non-empty Git repository.

This article will teach the procedure to clone a remote repository to a non-empty directory.

How to Clone Into a Non-Empty Git Directory?

Cloning the remote repository into the non-empty folder becomes crucial when you want to merge the files with the newly cloned remote repository.

Let’s look at the instructions to understand the procedure of cloning into a non-empty Git directory.

Step 1: Open Git Bash
Search for “Git Bash” on your system using the “Startup” menu and launch it:

Step 2: Move to Directory
Move to the directory where you want to clone the remote repository:

$ cd "C:\Users\nazma\Git\Linux_1"

In our case, we want to clone the remote repository in “Linux_1” folder:

Step 3: Initialize Empty Repository
Execute the below-provided command to set up an empty repository in the current folder:

$ git init

Step 4: Copy Remote Repository URL
Next, open the Git remote repository which you want to clone and copy its URL:

Note: If you execute the “$ git clone” command with copied URL, a fatal warning message will display on the command line, which indicates that our destination Git directory is not empty:

$ git clone https://github.com/GitUser0422/Linux-repo

You can handle the above-given error by adding a remote repository first and then moving toward cloning.

Step 4: Add Remote Repository
Now, add the remote repository into your specified non-empty Git directory with the “git remote add” command and specify the remote repository URL:

$ git remote add https://github.com/GitUser0422/Linux-repo.git

Step 5: Pull and Merge
Lastly, pull and merge the existing data Git directory with the remote repository:

$ git pull origin master --allow-unrelated-histories

As a result, the remote repository will be successfully cloned into the specified non-empty directory:

We have provided the method of cloning the remote repository into the non-empty Git directory.

Conclusion

To clone the remote repo into a non-empty Git directory, first, move to the specific directory in which you want to clone the repo, and set it up by executing the “$ git init” command. Then, go to the remote Git repo and copy its URL. Execute the “$ git remote add origin” command with copied URL. After that, run the “$ git pull origin master –allow-unrelated-histories” command to merge the local files with remote repository files. In this blog, we have illustrated the procedure of cloning the remote repository into the non-empty Git directory.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.