80

I cloned a git repository but accidentally messed up. So I re-cloned and the message showed up:

destination path already exists and is not an empty directory

I've tried deleting folders in my mac with the name of the destination path but it did not work.

I'm very new to coding so all help would be appreciated.

RBT
  • 24,161
  • 21
  • 159
  • 240
flopga slays
  • 827
  • 1
  • 6
  • 3
  • what error message you got ? –  May 16 '18 at 01:15
  • 1
    fatal: destination path 'XXX' already exists and is not an empty directory – flopga slays May 16 '18 at 01:19
  • 4
    why don't you simply `rm -rf XXX` ? – Gilles Gouaillardet May 16 '18 at 01:38
  • 1
    Possible duplicate of [Fatal: destination path 'opencv\_contrib' already exists and is not an empty directory](https://stackoverflow.com/questions/42686465/fatal-destination-path-opencv-contrib-already-exists-and-is-not-an-empty-dire) – phd May 16 '18 at 11:36
  • Related: [How to get Git to clone into current directory](https://stackoverflow.com/questions/9864728/how-to-get-git-to-clone-into-current-directory) – krubo Oct 04 '19 at 18:21
  • Either delete the current folder or rename it and get a clone – VishalParkash May 28 '20 at 12:01
  • this is a pretty straightforward git question, but has no duplicated flag nor accepted answer after two years – Daemon Painter Aug 06 '20 at 21:31
  • I had this issue because I didn't spot that the directory had a hidden file in (configuration file of my IDE). I moved the file out, did the clone and then put it back. – wkille Mar 05 '21 at 09:46
  • I think the proper answer to this question would be if there is an option on git to **force** clone a repo, even into a non-empty directory. There is no real reason why this shouldn't be possible; at worst you may want to forbid cloning into a directory that already has a `.git` file. However, it doesn't seem to be possible (or no one here knows how). – Marses Jun 24 '21 at 17:15
  • Gilles got the right answer. You need to remove that invisible folder with a suffix .git. To see that folder, use ls -a. To remove it, use rm -rf – Chen Lizi Jul 14 '21 at 16:09
  • If you're using macOs you should remove `.DS_Store` file. – Jeff Pal Oct 29 '22 at 23:05

15 Answers15

33

This error comes up when you try to clone a repository in a folder which still contains .git folder (Hidden folder).

If the earlier answers doesn't work then you can proceed with my answer. Hope it will solve your issue.

Open terminal & change the directory to the destination folder (where you want to clone).

Now type: ls -a

You may see a folder named .git.

You have to remove that folder by the following command: rm -rf .git

Now you are ready to clone your project.

Farid Chowdhury
  • 2,766
  • 1
  • 26
  • 21
  • 11
    It also comes up when there is no `.git` folder however. For example, if you make a project folder with PyCharm, you will have an `.idea` folder. I think what OP needs is a way to "force" git to clone the repo, overwrite or not (although in the above situation overwriting wouldn't even be required). – Marses Jun 24 '21 at 17:14
33

For the root folder (and any other)

For those coming here wishing to

  • clone a repository that already has contents

in an

  • existing folder that already contains files/folders

Follow the following steps (in this case for the root folder):

cd /
git init
git remote add origin <your-repo-url>
git pull
git checkout main -f
git branch --set-upstream-to origin/master

Change "cd /" to point to your desired folder


For remote computers requiring sudo

In a remote computer, for the root folder, when not a root user; you may execute sudo --preserve-env=SSH_AUTH_SOCK command to transfer the ssh agent. E.g.:

sudo --preserve-env=SSH_AUTH_SOCK su

or add a file to /etc/sudoers.d with this content:

Defaults    env_keep+=SSH_AUTH_SOCK
Rafa Alonso
  • 472
  • 4
  • 5
19

Explanation

This is pretty vague but I'll do what I can to help.

First, while it may seem daunting at first, I suggest you learn how to do things from the command line (called terminal on OSX). This is a great way to make sure you're putting things where you really want to.

You should definitely google 'unix commands' to learn more, but here are a few important commands to help in this situation:

ls - list all files and directories (folders) in current directory

cd <input directory here without these brackets> - change directory, or change the folder you're looking in

mkdir <input directory name without brackets> - Makes a new directory (be careful, you will have to cd into the directory after you make it)

rm -r <input directory name without brackets> - Removes a directory and everything inside it

git clone <link to repo without brackets> - Clones the repository into the directory you are currently browsing.

Answer

So, on my computer, I would run the following commands to create a directory (folder) called projects within my documents folder and clone a repo there.

  1. Open terminal
  2. cd documents (Not case sensitive on mac)
  3. mkdir projects
  4. cd projects
  5. git clone https://github.com/seanbecker15/wherecanifindit.git
  6. cd wherecanifindit (if I want to go into the directory)

p.s. wherecanifindit is just the name of my git repository, not a command!

Sean Becker
  • 611
  • 1
  • 6
  • 6
  • 1
    And if I were to receive the error you're getting, I would go into the projects directory and run `rm -rf wherecanifindit`, like the other people said. `rm` removes a file, `rm -r` removes a directory, and `rm -f` stops the command line from asking you questions. Put that all together and you get `rm -rf` – Sean Becker May 16 '18 at 01:46
6

This just means that the git clone copied the files down from github and placed them into a folder. If you try to do it again it will not let you because it can't clone into a folder that has files into it. So if you think the git clone did not complete properly, just delete the folder and do the git clone again. The clone creates a folder the same name as the git repo.

James Knott
  • 1,278
  • 7
  • 6
  • 1
    i deleted the folder but it is still showing up EDIT: ok im an idiot, it didnt delete properly – flopga slays May 16 '18 at 01:18
  • This answer is correct. I created a local repo at root. Then tried to also clone it, at the same place, which doesn't work. But if you move elsewhere in the dir structure, clone works. And he's also right that if it didn't work, delete the folder and clone again. No harm, no foul. – larboyer May 12 '23 at 15:49
6

Steps to get this error ;

  1. Clone a repo (for eg take name as xyz)in a folder
  2. Not try to clone again in the same folder . Even after deleting the folder manually this error will come since deleting folder doesn't delete git info .

Solution : rm -rf "name of repo folder which in out case is xyz" . So

rm -rf xyz
ArpitA
  • 721
  • 7
  • 17
4

What works for me is that, I created a new folder that doesn't contain any other files, and selected that new folder I created and put the clone there.

I hope this helps

Neuron
  • 5,141
  • 5
  • 38
  • 59
belards
  • 71
  • 2
  • Hello, and welcome to SO. Very much like [James Knott's answer](https://stackoverflow.com/a/50361155/2125110) the solution is indeed to clone the repository elsewhere. – Daemon Painter Aug 06 '20 at 21:37
3

Make a new-directory and then use the git clone url

Varun Kumar
  • 153
  • 11
1

If you got Destination path XXX already exists means the name of the project repository which you are trying to clone is already there in that current directory. So please cross-check and delete any existing one and try to clone it again

1

I got same issue while using cygwin to install nvm

In fact, the target directory where empty but the git binary used was the one from windows (and not git from cygwin git package).

After installing cygwin git package, the git clone from nvm install was ok!

boly38
  • 1,806
  • 24
  • 29
  • The issue at hand is trying to clone a repository in a location which is already a git repository. This answer doesn't help with that. – Daemon Painter Aug 06 '20 at 21:36
  • 1
    @DaemonPainter your comment is often true in many cases, but I don't undertstand why you down vote for the rest. A solution to a given issue is not always one an only one answer, because it's depends of your context. Your action helps nobody. – boly38 Aug 07 '20 at 06:39
0

An engineered way to solve this if you already have files you need to push to Github/Server:

  1. In Github/Server where your repo will live:

    • Create empty Git Repo (Save <YourPathAndRepoName>)
    • $git init --bare
  2. Local Computer (Just put in any folder):

    • $touch .gitignore
    • (Add files you want to ignore in text editor to .gitignore)
    • $git clone <YourPathAndRepoName>

    • (This will create an empty folder with your Repo Name from Github/Server)

    • (Legitimately copy and paste all your files from wherever and paste them into this empty Repo)

    • $git add . && git commit -m "First Commit"

    • $git push origin master
kevin_theinfinityfund
  • 1,631
  • 17
  • 18
  • He needs to clone, but is trying to clone into a location that is already a git repository. This answer doesn't help with that. – Daemon Painter Aug 06 '20 at 21:35
  • 1
    @DaemonPainter the question states that they've tried deleting the folders; thus, it's presumed that they are trying to restart after an `rm -f path` but they still have files they want to commit to an initially empty git repo. – kevin_theinfinityfund Aug 07 '20 at 18:15
  • True, but the comment posted under [this answer](https://stackoverflow.com/a/50361155/2125110) by the asker states "I'm an idiot, I didn't delete it properly". To me, it looks like the asker is admitting the proposed solution is to _not_ attempt to clone into an existing repo... – Daemon Painter Aug 07 '20 at 19:04
0

Multiple ways to do this.

Once you log into your server, below are the commands that you can use.

  1. ls ==> To check the files in the current folder.
  2. ls -a ==> To check hidden files in the current folder.
  3. cd "Folder Name" ==> To move into a folder.
  4. cd ==> To move out of a folder.
  5. rm -r ==> Delete a File.
  6. rm -rf ==> Remove a folder.

So for the issue, use ls and check if you are in the right place to delete the folder. Then use rm -rf " Folder Name". This should delete the folder or you can use rm -r "File name" to delete a file.

Regards, Nikhil

Nikhil Rao
  • 36
  • 4
  • 1
    Do note that `-r` is to remove recursively, and `-f` is to move forcefully to avoid CHMOD permissions. – ReyHaynes Sep 24 '22 at 16:34
0

Besides the error appears because it already contains a git directory in that folder, as other people mentioned above. It also occurs if a folder with the same name as your repository already exists.

Ex:

Repository: github.com/username/myRepo.git

If the folder where "clone" is running contains a myRepo folder, this error is thrown: destination path already [...]

0

I had this issue as well,try deleting the actual file in the computer then try clone again.

Zernation
  • 1
  • 2
  • 1
    I'm afraid your answer didn't add any value to what have already been said by others. Also OP said right in his question, that he tried to delete content of the folder – The Dreams Wind Jul 29 '22 at 13:06
-1

If you have happened the same as me and you wanted to clone in the C:src> folder with the flutter folder already saved there, delete it and then you can run the command C:src>git clone https://github.com/flutter/flutter.git -b stable.

Well actually if you are in C:\src> just copy the command git clone https://github.com/flutter/flutter.git -b stable and hit enter.

Dharman
  • 30,962
  • 25
  • 85
  • 135
-3

Use Command: ls -a You will see list of files or folders that's blocking it. Use Command : rm -r Foldername/filename

After deleting all the files and folders. You can clone your project to the directory