Categories
git git-clone repository

How do I clone a Git repository into a specific folder?

2792

The command git clone [email protected]:whatever creates a directory named whatever containing a Git repository:

./
    whatever/
        .git

I want the contents of the Git repository cloned into my current directory ./ instead:

./
    .git

1

  • 7

    On Windows you can move files from folder where you will clone the repo[if there is any] so it is empty when you do git clone. After cloning move your files back[ctrl-z] and whoala! If current folder is empty just use : git clone giturl .

    – user1188429

    Feb 3, 2012 at 20:50

3922

Option A:

git clone [email protected]:whatever folder-name

Ergo, for right here use:

git clone [email protected]:whatever .

Option B:

Move the .git folder, too. Note that the .git folder is hidden in most graphical file explorers, so be sure to show hidden files.

mv /where/it/is/right/now/* /where/I/want/it/
mv /where/it/is/right/now/.* /where/I/want/it/

The first line grabs all normal files, the second line grabs dot-files. It is also possibe to do it in one line by enabling dotglob (i.e. shopt -s dotglob) but that is probably a bad solution if you are asking the question this answer answers.

Better yet:

Keep your working copy somewhere else, and create a symbolic link. Like this:

ln -s /where/it/is/right/now /the/path/I/want/to/use

For your case this would be something like:

ln -sfn /opt/projectA/prod/public /httpdocs/public

Which easily could be changed to test if you wanted it, i.e.:

ln -sfn /opt/projectA/test/public /httpdocs/public

without moving files around. Added -fn in case someone is copying these lines (-f is force, -n avoid some often unwanted interactions with already and non-existing links).

If you just want it to work, use Option A, if someone else is going to look at what you have done, use Option C.

20

  • 13

    I hadn’t thought of your ‘Better yet’ option, and I like it although I’m not sure why. What are the advantages of this?

    Mar 17, 2009 at 14:02

  • 8

    It probably doesn’t provide any advantage right now, but it might save you a lot of trouble if you decide to move stuff around some day.

    Mar 17, 2009 at 14:15

  • 13

    Plus you can switch between releases quickly with the “Better yet” option. This is especially useful and used for servers.

    Nov 17, 2011 at 9:10

  • 5

    @MEM I think he means create a symbolic link to .git i.e. ln -s path/to/.git path/to/working/directory

    – Alexander

    Feb 23, 2013 at 1:29


  • 32

    Can anyone explain the benefits of this?? I am being a bit dense today. How does using a symlink benefit servers?

    – triple

    Jun 12, 2013 at 20:08

631

The example I think a lot of people asking this question are after is this. If you are in the directory you want the contents of the git repository dumped to, run:

git clone [email protected]:whatever .

The “.” at the end specifies the current folder as the checkout folder.

6

  • 36

    true, so you don’t want to run “git init” in this folder before cloning into it. But if you did, then removing the .git folder before cloning will do the trick.

    – Magne

    Aug 27, 2011 at 14:28

  • 5

    fixed for empty directories in my answer

    – csomakk

    Dec 12, 2013 at 8:43

  • 3

    couldn’t get this working in nitrous.io? even after emptying, it says directory isn’t empty

    Aug 2, 2014 at 5:10

  • @TejasManohar Make sure that you have deleted hidden files as well.

    – Britic

    May 27, 2015 at 4:28

  • ls -a will show all files, including hidden ones.

    Apr 14, 2019 at 20:15

252

Go into the folder.. If the folder is empty, then:

git clone [email protected]:whatever .

else

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master

8

  • 10

    Actually this was most useful answer for me when I just needed to push already created project to newly created repository. Kudos!

    – wirher

    Nov 28, 2014 at 12:11

  • 15

    Only answer that specifies how to checkout into a non-empty folder, thank you.

    Mar 21, 2015 at 16:19

  • 3

    This is really helpful as the switch -t on isn’t clearly shown on documentation of git checkout.

    – eQ19

    May 9, 2016 at 17:56

  • 5

    what does the -t switch do? I’m getting an abort message with a list of untracked files which will be overwritten by checkout? What to do in this situation? I don’t want those files to be overwritten?

    Apr 18, 2017 at 9:48

  • 3

    -t documentation from git: -t, –track When creating a new branch, set up “upstream” configuration. See “–track” in git-branch(1) for details.

    – csomakk

    Apr 19, 2017 at 11:04