Difference between git remote add and git clone

56,423

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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

Share:
56,423

Related videos on Youtube

nacho4d
Author by

nacho4d

C/C++/Objective-C/C#/Javascript/shell-script/Japanese/Go/Spanish/English I love this site. So Helpful! https://twitter.com/nacho4d http://nacho4d-nacho4d.blogspot.com/

Updated on July 05, 2022

Comments

  • nacho4d
    nacho4d almost 2 years

    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?

  • nacho4d
    nacho4d about 13 years
    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
    nacho4d about 13 years
    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?
  • Cascabel
    Cascabel about 13 years
    @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.
  • Wes Hardaker
    Wes Hardaker about 13 years
    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" :-)
  • bjnord
    bjnord about 12 years
    @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.
  • levsa
    levsa almost 10 years
    --set-upstream seems to be deprecated, this works better: git branch --set-upstream-to=origin/master master
  • Wes Hardaker
    Wes Hardaker over 9 years
    I hate it when developers take the easy options away from you. IMHO, both pull and push should have the easier --set-upstream flag.
  • Chad
    Chad almost 5 years
    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.
  • Pharap
    Pharap over 4 years
    I believe the latter should have a cd .. afterwards?
  • Wes Hardaker
    Wes Hardaker over 4 years
    @Pharap fair enough. Added.