139

I have given access to server, and want to clone git repo into my root folder. But when I do git clone it will make me folder with project name, and my project folder is my root. I dont have access to my parent folder my root is

/var/www/sites/mysite/

and when I do cloning folder structure will be

/var/www/sites/mysite/mysite
Braiam
  • 1
  • 11
  • 47
  • 78
dzona
  • 3,323
  • 3
  • 31
  • 47
  • 2
    be sure to not deliver .git in apache/webserver of choice. – mnagel Jul 11 '13 at 06:36
  • 1
    Does this answer your question? [Clone contents of a GitHub repository (without the folder itself)](https://stackoverflow.com/questions/6224626/clone-contents-of-a-github-repository-without-the-folder-itself) – Vishrant Mar 25 '20 at 01:09

7 Answers7

317

git clone accepts a last argument that is the destination directory, it is by default the name of the project but you can change it. In your case you probably want simply .:

$ git clone origin-url .

But note that, from man git-clone:

Cloning into an existing directory is only allowed if the directory is empty.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 7
    `git clone @rodrigo ./heaven` – amdev Jun 03 '19 at 07:24
  • And how about cloning two levels deep? I have two folders on top of the pom.xml? – Bionix1441 Aug 26 '20 at 14:32
  • 1
    @Bionix1441: I don't know about your directory structure, but if you are two levels down by just writing `git clone aaa/bbb`. If you wanted to do two levels up you could try `git clone ..` but that will not work be because your parent directoy is never empty. – rodrigo Aug 27 '20 at 11:23
  • The structure is the following `folder1\folder2\pom.xml`, I only need the `pom.xml` and the folders on the same level as the `pom.xml` – Bionix1441 Aug 27 '20 at 14:10
54

You can also just setup a new repo and then the tracking remote and branch, fetch all the objects on the origin repository and change to the master branch:

git init .
git remote add origin git@github.com:user/repo.git
git fetch origin
git checkout master
Braiam
  • 1
  • 11
  • 47
  • 78
richo
  • 8,717
  • 3
  • 29
  • 47
  • This still wouldn't work if the repo has a parent folder – Hittz May 05 '15 at 10:19
  • Had to also set the upstream branch to match: `git branch --set-upstream-to=origin/master master` Then had to push local changes to the remote branch: `git push --force` This replaced the old remote branch with my local up to date copy. – Daniel ZA Apr 25 '20 at 12:57
  • working still in 2023 – y.y Apr 05 '23 at 15:47
14

This is working well on Windows also.

git init
git remote add origin git@github.com:user/repo.git
git pull origin master
hobbydev
  • 1,513
  • 19
  • 31
7

Remember that a git repository is simply the directory structure where you store it. This means that when you clone a repository into the wrong directory, you can simply move the directory contents anywhere else that you wish and the repository data is still intact. So for example, you can run the following commands from the command line:

$ mv /var/www/sites/mysite/mysite/* /var/www/sites/mysite`
$ mv /var/www/sites/mysite/mysite/.* /var/www/sits/mysite`
$ rmdir /var/www/sites/mysite/mysite
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Hi can I ask, what does the period signify in the second line? – fred Oct 09 '21 at 16:01
  • @fred The period is a literal character that is part of a file or directory name. In Unix-like systems a leading period in a file name indicates a "hidden" file. These file names are not matched with the `*`. The second line will make sure the `.git` folder and `.gitignore` files are correctly moved among others. – Code-Apprentice Oct 11 '21 at 20:31
4

You can clone your project into subfolder and them move all files including the .git folder into the parent folder (your root).

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
3
git clone git@github.com:user/repo.git .

Notice the . at the end of that command.

Or Another way is

git init

git remote add origin git@github.com:user/repo.git

git pull origin master
2

A simple way I found to achieve this is by initializing a Git repo in the directory you want to clone into and pull from remote:

git init
git pull <remote-url>
unrealapex
  • 578
  • 9
  • 23