2981

The command git clone git@github.com: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
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
David Smith
  • 38,044
  • 11
  • 44
  • 61
  • 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 **.*** –  Feb 03 '12 at 20:50

20 Answers20

4202

Option A:

git clone git@github.com:whatever folder-name

Ergo, for right here use:

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

leonprou
  • 4,638
  • 3
  • 21
  • 27
Can Berk Güder
  • 109,922
  • 25
  • 130
  • 137
  • 16
    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? – David Smith Mar 17 '09 at 14:02
  • 9
    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. – Can Berk Güder Mar 17 '09 at 14:15
  • I'm trying the symbolic link method, but I'm not sure if this will work with Django? – Sam Stoelinga Oct 21 '10 at 23:48
  • 14
    Plus you can switch between releases quickly with the "Better yet" option. This is especially useful and used for servers. – Halil Özgür Nov 17 '11 at 09:10
  • 2
    Hey guys, I have the same exact question. I am not really familiar with symlinks, but have an idea of how they work. How would I set one up to solve the OP problem? – Yev Feb 19 '12 at 03:56
  • 2
    @Can Berk Gueder: Can you please provide more details, or adding a link to your Better yet option. Thanks. – MEM Mar 25 '12 at 16:39
  • 1
    Make sure to delete your hidden files as well. Like old `.git` and `.gitignore` stuff. – Keith Smiley Nov 26 '12 at 01:26
  • 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 '13 at 01:29
  • 34
    Can anyone explain the benefits of this?? I am being a bit dense today. How does using a symlink benefit servers? – triple Jun 12 '13 at 20:08
  • 2
    None of this works - neither this one, nor the one below from J Wynia. I have spent hours on this trying to get Google's angular-seed directory into a named directory in Apache's htdocs directory. No tutorials exist for this operation. Someone should write a tutorial aimed at new users of git and AngularJS. – Mike_Laird Mar 18 '14 at 20:55
  • If you don't supply folder-name, the project own folder name would be used to (the one with name.git) – m3nda May 29 '15 at 21:11
  • 2
    Should really illustrate what the `whatever` argument is supposed to do, it's incomprehensible without an explanation. – 10000RubyPools Apr 11 '16 at 20:56
  • this works for sub folders as well git clone git@xxx.git. some/sub/folder/ – astroanu Sep 11 '16 at 10:57
  • It's worth noting that in order to point to a folder, (rather than a file,) the link must be symbolic. – Jack Jan 15 '17 at 21:59
  • 3
    I am having following error: `fatal: destination path 'folderName' already exists and is not an empty directory.`can't it just force to overwrite files. @Can Berk Güder – alper Apr 02 '18 at 12:55
  • References [2.1 Git Basics - Getting a Git Repository](https://git-scm.com/book/en/v2/Git-Basics-Getting-a-Git-Repository) – Big Shield Aug 02 '18 at 01:56
  • 1
    @triple I figure with "better yet option", you can handily switch between different versions on the server side without worrying messing up the batch job starting the service. – CCNA Dec 10 '18 at 20:41
  • Downvote for not explaining your **Better yet** option – Green Jun 20 '19 at 07:45
  • User double quotes to create a folder with spaces if necessary. "Folder Name". – David Rupe Jan 24 '20 at 22:39
  • 2
    I get "fatal: destination path 'repo_name' already exists and is not an empty directory". I don't think this is possible for an already existing directory. – Pe Dro Feb 22 '20 at 17:33
643

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

The "." at the end specifies the current folder as the checkout folder.

J Wynia
  • 10,464
  • 4
  • 40
  • 38
262

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

git clone git@github.com:whatever .

else

git init
git remote add origin PATH/TO/REPO
git fetch
git checkout -t origin/master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
csomakk
  • 5,369
  • 1
  • 29
  • 34
  • 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 '14 at 12:11
  • 16
    Only answer that specifies how to checkout into a non-empty folder, thank you. – Preston Badeer Mar 21 '15 at 16:19
  • 3
    This is really helpful as the `switch -t` on isn't clearly shown on documentation of [git checkout](https://git-scm.com/book/en/v2/Git-Commands-Branching-and-Merging#git-checkout). – eQ19 May 09 '16 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? – Momin Zahid Apr 18 '17 at 09: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 '17 at 11:04
  • for @MominZahid you have something that is in your local, and not in your repo. i recommend you adding it to a commit then merging it. but since this is git, there is 20x other good way to go too ;) – csomakk Apr 19 '17 at 11:05
  • 2
    This is the best answer. – DaveS Nov 02 '17 at 01:11
  • 1
    This should be higher up. – somethingsomewhere Oct 12 '20 at 20:12
118

Basic Git Repository Cloning

You clone a repository with

git clone [url]

For example, if you want to clone the Stanford University Drupal Open Framework Git library called open_framework, you can do so like this:

$ git clone git://github.com/SU-SWS/open_framework.git

That creates a directory named open_framework (at your current local file system location), initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the newly created open_framework directory, you’ll see the project files in there, ready to be worked on or used.

Cloning a Repository Into a Specific Local Folder

If you want to clone the repository into a directory named something other than open_framework, you can specify that as the next command-line option:

$ git clone git:github.com/SU-SWS/open_framework.git mynewtheme

That command does the same thing as the previous one, but the target directory is called mynewtheme.

Git has a number of different transfer protocols you can use. The previous example uses the git:// protocol, but you may also see http(s):// or user@server:/path.git, which uses the SSH transfer protocol.

Craig Gjerdingen
  • 1,844
  • 1
  • 21
  • 21
  • this will automatically create a folder in your current folder and put the files there. Thus, you don't need to worry about overwriting any other folder. – arn-arn Mar 27 '17 at 14:06
87

You can use following git command to clone with custom directory name

git clone <git_repo_url> <your_custom_directory_name>

Note: You don't need to create your custom directory because it will create automatically

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
Manoj Rana
  • 3,068
  • 1
  • 24
  • 34
70

To clone git repository into a specific folder, you can use -C <path> parameter, e.g.

git -C /httpdocs clone git@github.com:whatever

Although it'll still create a whatever folder on top of it, so to clone the content of the repository into current directory, use the following syntax:

cd /httpdocs
git clone git@github.com:whatever .

Note that cloning into an existing directory is only allowed when the directory is empty.

Since you're cloning into folder that is accessible for public, consider separating your Git repository from your working tree by using --separate-git-dir=<git dir> or exclude .git folder in your web server configuration (e.g. in .htaccess file).

kenorb
  • 155,785
  • 88
  • 678
  • 743
44

To clone to Present Working Directory:

git clone https://github.com/link.git

To clone to Another Directory:

git clone https://github.com/link.git ./Folder1/Folder2
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Sarat Chandra
  • 5,636
  • 34
  • 30
  • 1
    this worked for me too. not the `./` at the beginning of the path to the folder – Alon Gouldman Sep 08 '19 at 06:00
  • Cloning via this method still creates the repo's own folder name instead of using the present working directory's name. e.g. /present-directory/link/ instead of /present-directory/ – Joel Karunungan Nov 22 '21 at 02:44
20

If you want to clone into the current folder, you should try this:

git clone https://github.com/example/example.git ./
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luis
  • 789
  • 9
  • 9
19

Usage

git clone <repository>

Clone the repository located at the <repository> onto the local machine. The original repository can be located on the local filesystem or on a remote machine accessible via HTTP or SSH.

git clone <repo> <directory>

Clone the repository located at <repository> into the folder called <directory> on the local machine.

Source: Setting up a repository

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
19

When you move the files to where you want them, are you also moving the .git directory? Depending on your OS and configuration, this directory may be hidden.

It contains the repo and the supporting files, while the project files that are in your /public directory are only the versions in the currently check-out commit (master branch by default).

Paul
  • 16,255
  • 3
  • 33
  • 25
16

Clone:

git clone git@jittre.unfuddle.com:jittre/name.git

Clone the "specific branch":

git clone -b [branch-name] git@jittre.unfuddle.com:jittre/name.git
Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
Selvamani
  • 7,434
  • 4
  • 32
  • 43
13

Make sure you remove the .git repository if you are trying to check thing out into the current directory.

rm -rf .git then git clone https://github.com/symfony/symfony-sandbox.git

JJD
  • 50,076
  • 60
  • 203
  • 339
alex
  • 139
  • 1
  • 2
8

From some reason this syntax is not standing out:

git clone repo-url [folder]

Here folder is an optional path to the local folder (which will be a local repository).

Git clone will also pull code from remote repository into the local repository. In fact it is true:

git clone repo-url  =  git init + git remote add origin repo-url + git pull
Community
  • 1
  • 1
prosti
  • 42,291
  • 14
  • 186
  • 151
6

Here's how I would do it, but I have made an alias to do it for me.

$ cd ~Downloads/git; git clone https:git.foo/poo.git

There is probably a more elegant way of doing this, however I found this to be easiest for myself.

Here's the alias I created to speed things along. I made it for zsh, but it should work just fine for bash or any other shell like fish, xyzsh, fizsh, and so on.

Edit ~/.zshrc, /.bashrc, etc. with your favorite editor (mine is Leafpad, so I would write $ leafpad ~/.zshrc).

My personal preference, however, is to make a zsh plugin to keep track of all my aliases. You can create a personal plugin for oh-my-zsh by running these commands:

$ cd ~/.oh-my-zsh/
$ cd plugins/
$ mkdir your-aliases-folder-name; cd your-aliases-folder-name
     # In my case '~/.oh-my-zsh/plugins/ev-aliases/ev-aliases'
$ leafpad your-zsh-aliases.plugin.zsh
     # Again, in my case 'ev-aliases.plugin.zsh'

Afterwards, add these lines to your newly created blank alises.plugin file:

# Git aliases
alias gc="cd ~/Downloads/git; git clone "

(From here, replace your name with mine.)

Then, in order to get the aliases to work, they (along with zsh) have to be sourced-in (or whatever it's called). To do so, inside your custom plugin document add this:

## Ev's Aliases

#### Remember to re-source zsh after making any changes with these commands:

#### These commands should also work, assuming ev-aliases have already been sourced before:

allsource="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh; clear"
sourceall="source $ZSH/oh-my-zsh.sh ; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh"
#### 

####################################

# git aliases

alias gc="cd ~/Downloads/git; git clone "
# alias gc="git clone "
# alias gc="cd /your/git/folder/or/whatever; git clone "

####################################

Save your oh-my-zsh plugin, and run allsource. If that does not seem to work, simply run source $ZSH/oh-my-zsh.sh; source /home/ev/.oh-my-zsh/plugins/ev-aliases/ev-aliases.plugin.zsh. That will load the plugin source which will allow you to use allsource from now on.


I'm in the process of making a Git repository with all of my aliases. Please feel free to check them out here: Ev's dot-files. Please feel free to fork and improve upon them to suit your needs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ev-
  • 199
  • 1
  • 6
6

If you are in the directory you want the contents of the git repository dumped to, run:

git clone git@github.com:origin .

The "." at the end specifies the current folder as the checkout folder.

Riyas TK
  • 1,231
  • 6
  • 18
  • 32
4

If you are using ssh for git cloning you can use the following command.

git -C path clone git@github.com:path_to_repo.git

eg: git -C /home/ubuntu/ clone git@github.com:kennethreitz/requests.git would pull the git repository for requests to your /home/ubuntu/ path.

Ankit Singh
  • 594
  • 4
  • 16
1
  • go to the directory where you want to clone the repo.
    (don't run git init command inside that directory)

  • simply run the command,
    git clone <git repo url> .

Example: git clone https://github.com/Rashmi-Wijesekara/portfolio.git .

-1

Although all of the answers above are good, I would like to propose a new method instead of using the symbolic link method in public html directory as proposed BEST in the accepted answer. You need to have access to your server virtual host configurations.

It is about configuring virtual host of your web server directly pointing to the repository directory. In Apache you can do it like:

DocumentRoot /var/www/html/website/your-git-repo

Here is an example of a virtual host file:

<VirtualHost *:443>
    ServerName example.com

    DocumentRoot /path/to/your-git-repo
    ...
    ...
    ...
    ...
</VirtualHost>
Tarik
  • 4,270
  • 38
  • 35
-2
For Windows user 

1> Open command prompt.
2> Change the directory to destination folder (Where you want to store your project in local machine.)
3> Now go to project setting online(From where you want to clone)
4> Click on clone, and copy the clone command.
5> Now enter the same on cmd .

It will start cloning saving on the selected folder you given .
Tarit Ray
  • 944
  • 12
  • 24
-5

Regarding this line from the original post:

"I know how to move the files after I've cloned the repo, but this seems to break git"

I am able to do that and I don't see any issues so far with my add, commit, push, pull operations.

This approach is stated above, but just not broken down into steps. Here's the steps that work for me:

  1. clone the repo into any fresh temporary folder
  2. cd into that root folder you just cloned locally
  3. copy the entire contents of the folder, including the /.git directory - into any existing folder you like; (say an eclipse project that you want to merge with your repo)

The existing folder you just copied the files into , is now ready to interact with git.

Gene Bo
  • 11,284
  • 8
  • 90
  • 137