293

I'd like to git clone the contents of a repository I have on GitHub. When I git clone (git@github:me/name.git...) I get a folder called name/ and inside name I have my contents... How do I get JUST the contents?

2

7 Answers 7

399

If the current directory is empty, you can do that with:

git clone [email protected]:me/name.git .

(Note the . at the end to specify the current directory.) Of course, this also creates the .git directory in your current folder, not just the source code from your project.

This optional [directory] parameter is documented in the git clone manual page, which points out that cloning into an existing directory is only allowed if that directory is empty.

7
  • 4
    Unfortunately, this doesnt work if there are other, non related directories already in the same dir. Looking for a solution. The error message is: "fatal: destination path '.' already exists..." May 23, 2013 at 10:58
  • 18
    The directory git clones into must be empty
    – jolyonruss
    Jul 30, 2013 at 13:06
  • 1
    @HumaunRashid Add a . as discussed in the answer: git clone https://github.com/humaun21/Test . . And yes, [email protected]/name.git is a placeholder for whatever your actual git repo address is. May 23, 2016 at 16:26
  • 2
    @JohnLittle Had the same problem, turns out there is a hidden .DS_Store file, that hides there. Simply rm .DS_Store and you're good to go.
    – Selrond
    Mar 22, 2017 at 5:29
  • 1
    If you are working on linux OS, you need to be sure the directory is empty including checking for hidden files and subdirectories. You can do that with ls -a. You should have only . and .. as output. This wont work otherwise Jun 2, 2018 at 20:31
181

Unfortunately, this doesn't work if there are other, non-related directories already in the same dir. Looking for a solution. The error message is: "fatal: destination path '.' already exists...".

The solution in this case is:

git init
git remote add origin [email protected]:me/name.git
git pull origin master

This recipe works even if there are other directories in the one you want to checkout in.

6
  • Hey in this I am getting an error like Permission denied <public key>. The remote end hung up unexpectedly.
    – Stan
    Feb 20, 2014 at 7:24
  • Shouldn't it be github.com there after git remote ..., and not just github? May 21, 2015 at 21:18
  • 6
    This answer should have been accepted one. works perfectly.
    – vikramvi
    Oct 17, 2016 at 13:35
  • I read elsewhere here that you need to run git fetch --all before the git pull origin master, because if there are other branches on the repo, git pull won't get those unless you use fetch first. Is this correct?
    – Alex G
    Nov 22, 2018 at 11:22
  • But then the other non-related directories and all their files will show up as untracked changes. Mar 29, 2022 at 22:57
48

If the folder is not empty, a slightly modified version of @JohnLittle's answer worked for me:

git init
git remote add origin https://github.com/me/name.git
git pull origin master

As @peter-cordes pointed out, the only difference is using https protocol instead of git, for which you need to have SSH keys configured.

3
  • What version of git are you running? This didn't work for me. It still won't let me set it up because the dir isn't empty
    – guribe94
    Oct 29, 2014 at 4:49
  • @guribe94 git version 2.5.0 Mar 15, 2016 at 8:53
  • 4
    This is the same answer, just using anonymous https instead of git-protocol over ssh (requiring a github account with an ssh keypair set up). Jun 14, 2017 at 23:22
24

You can specify the destination directory as second parameter of the git clone command, so you can do:

git clone <remote> .

This will clone the repository directly in the current local directory.

1
  • This works only if current directory isn't empty.
    – Ion T
    Jul 7, 2022 at 13:07
3

to clone git repo into the current and empty folder (no git init) and if you do not use ssh:

git clone https://github.com/accountName/repoName.git .
1

this worker for me

git clone <repository> .
1

I know this question is old but please notice that git pull will abort if there is local files that matches the remote one, a solution for that would be to make a reset after pulling like this:

git init
git remote add origin REMOTE_REPO_URL.git
git pull origin master
git reset --hard FETCH_HEAD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.