165

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
2

7 Answers 7

351

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.

Sign up to request clarification or add additional context in comments.

4 Comments

git clone @rodrigo ./heaven
And how about cloning two levels deep? I have two folders on top of the pom.xml?
@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.
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
59

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 [email protected]:user/repo.git
git fetch origin
git checkout master

3 Comments

This still wouldn't work if the repo has a parent folder
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.
working still in 2023
17

This is working well on Windows also.

git init
git remote add origin [email protected]:user/repo.git
git pull origin master

Comments

8
git clone [email protected]:user/repo.git .

Notice the . at the end of that command.

Or Another way is

git init

git remote add origin [email protected]:user/repo.git

git pull origin master

Comments

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

2 Comments

Hi can I ask, what does the period signify in the second line?
@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.
6

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>

Comments

4

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

3 Comments

why it can't be done more easily, like git clone [email protected]:whatever .\
it can, see rodrigos answer. but i you know that a git repo is just a bunch of files that can be moved around freely, this answer is easy to remember.
git clone git@github:me/name.git .

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.