271

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?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
mazlix
  • 5,643
  • 6
  • 31
  • 45
  • possible duplicate of [How do you clone a git repository into a specific folder?](http://stackoverflow.com/questions/651038/how-do-you-clone-a-git-repository-into-a-specific-folder) – bryanbraun May 14 '14 at 05:20
  • To actually skip downloading unneeded objects to save network resources: https://stackoverflow.com/questions/600079/how-do-i-clone-a-subdirectory-only-of-a-git-repository/52269934#52269934 – Ciro Santilli Путлер Капут 六四事 Sep 11 '18 at 07:20

6 Answers6

367

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

git clone git@github.com: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.

ruohola
  • 19,113
  • 6
  • 51
  • 82
Mark Longair
  • 415,589
  • 70
  • 403
  • 320
  • 3
    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..." – John Little May 23 '13 at 10:58
  • 16
    The directory git clones into must be empty – jolyonruss Jul 30 '13 at 13:06
  • what is git@github.me? Is it the path of the repository? It's not working for me. I am trying this and I want only files inside Test folder... git clone https://github.com/humaun21/Test.git I want to get only the files inside Test folder. But It's not working. I creates a Test folder which contains my folder. But I want only files inside Test folder? – Humaun Rashid Nayan Mar 02 '16 at 11:41
  • 1
    @HumaunRashid Add a `.` as discussed in the answer: `git clone https://github.com/humaun21/Test .` . And yes, `git@github.me/name.git` is a placeholder for whatever your actual git repo address is. – Aaron Campbell May 23 '16 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 '17 at 05: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 – Ayo Makanjuola Jun 02 '18 at 20:31
172

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 git@github.com:me/name.git
git pull origin master

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

John Little
  • 9,293
  • 16
  • 73
  • 126
  • Hey in this I am getting an error like Permission denied . The remote end hung up unexpectedly. – Stan Feb 20 '14 at 07:24
  • Shouldn't it be `github.com` there after `git remote ...`, and not just `github`? – amn May 21 '15 at 21:18
  • 6
    This answer should have been accepted one. works perfectly. – vikramvi Oct 17 '16 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 '18 at 11:22
  • But then the other non-related directories and all their files will show up as untracked changes. – jewbix.cube Mar 29 '22 at 22:57
44

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.

21

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.

Laurent Pireyn
  • 6,627
  • 1
  • 28
  • 39
2

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 .
fingerman
  • 81
  • 2
1

this worker for me

git clone <repository> .