81

What does the clone command do? Is there any equivalent to it in svn?

What is the difference between

git remote add test git://github.com/user/test.git

And

git clone git://github.com/user/test.git

Does the name of the created repo matter?

code_dredd
  • 5,915
  • 1
  • 25
  • 53
nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • 1
    possible duplicate of [What is the difference between clone and mkdir->cd->init->remote-add->pull?](http://stackoverflow.com/questions/4108778/what-is-the-difference-between-clone-and-mkdir-cd-init-remote-add-pull) – Cascabel Jan 31 '11 at 23:19
  • The duplicate I chose is kind of a superset of your question: it asks "what's the difference betweeen `git remote add; ...other commands...` and `git clone`. – Cascabel Jan 31 '11 at 23:20

5 Answers5

69

git remote add just creates an entry in your git config that specifies a name for a particular URL. You must have an existing git repo to use this.

git clone creates a new git repository by copying an existing one located at the URI you specify.

Jason LeBrun
  • 13,037
  • 3
  • 46
  • 42
  • You mean that, for example, in my example, test folder will be empty and I will have to do '$git fetch test' from test folder? – nacho4d Jan 31 '11 at 20:27
  • @nacho4d: You'd have to do `git init` first, then `git fetch` and `git merge` (or `git pull`) afterwards, and then you'd still be a little bit off... see the duplicate I linked. – Cascabel Jan 31 '11 at 23:21
60

These are functionally similar (try it!):

 # git clone REMOTEURL foo

and:

 # mkdir foo
 # cd foo
 # git init
 # git remote add origin REMOTEURL
 # git pull origin master
 # cd ..

Now there are minor differences, but fundamentally you probably won't notice them. As an exercise left to the reader, compare the .git/config's from each directory.

Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
  • 2
    I have tried it. With using `clone`, the .git/config has a [branch "master"] part. Does this mean that when using `clone` (In the future) I can merge that branch and with `remote add` I cannot? – nacho4d Jan 31 '11 at 22:06
  • 2
    Well, first... they're still the same. You can actually merge from the upstream either way. With the extra "branch" statement in there it means you don't need to specify the upstream branch. IE, in a clone with the above "branch" in it you can do a "git pull" where as in the other you most likely need to do a "git pull origin master". Now, you can actually use the "git remote add" with the -t flag too to add this branch statement. Try a "git remote add" with and without "-t master" :-) – Wes Hardaker Feb 01 '11 at 00:17
  • @nacho4d: You can get that same [branch "master"] section in .git/config, with this command: `git branch --set-upstream master origin/master`. That's the 3rd command I use after the `git remote add` and `git pull` you have listed, and those 3 commands seem to give me an identical configuration to what `git clone` would have done. – bjnord Mar 10 '12 at 05:09
  • `--set-upstream` seems to be deprecated, this works better: `git branch --set-upstream-to=origin/master master` – levsa Jul 18 '14 at 20:39
  • I hate it when developers take the easy options away from you. IMHO, both pull and push should have the easier --set-upstream flag. – Wes Hardaker Sep 12 '14 at 14:44
  • Actually, if the remote repo does NOT have a master branch, `git clone` works correctly, but `git remote add` runs into a problem because `git init` creates a master branch by default which doesn't correspond to anything in the remote. – Chad Jun 05 '19 at 16:44
  • I believe the latter should have a `cd ..` afterwards? – Pharap Aug 10 '19 at 19:40
  • @Pharap fair enough. Added. – Wes Hardaker Dec 20 '19 at 17:19
11

The clone command creates a local copy of the repo you specified. remote add adds a remote repo that you can either push to or pull from.

The svn equivalent of clone is checkout.

Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
11

git clone:

Will physically download the files into your computer. It will take space from your computer. If the repo is 200Mb, then it will download that all and place it in the directory you cloned.

git remote add:

Won't take space! It's more like a pointer! It doesn't increase your disk consumption. It just gets a snapshot of what branches are available and their git commit history I believe. It doesn't contain the actual file/folders of your project.

If you do:

git remote add TechLeadRepo  git://github.com/user/test.git

then you haven't added anything to your computer. After you've added it in your remote branches then you're able to get a list of all branches on that remote by doing:

git fetch --all

upon fetching (or pulling), you download the files And then if you wanted to do get your colleague's feature22 branch into your local, you'd just do

git checkout -b myLocalFeature22 TechLeadRepo/feature22

Had you cloned his repo then you would have to go into that local repository's directory and simply just checkout to your desired branch

mfaani
  • 33,269
  • 19
  • 164
  • 293
0

git clone URL -

Will physically download the files into your computer. It will take space from your computer. If the repo is 200 MB, then it will download that all and place it in the directory you cloned.

git remote add origin -

Won't take space! It's more like a pointer! It doesn't increase your disk consumption. It just gets a snapshot of what branches are available and their git commit history I believe. It doesn't contain the actual files/folders of your project.