328

What is the difference between doing (after mkdir repo and cd repo):

git init
git remote add origin git://github.com/cmcculloh/repo.git
git fetch --all
git pull origin master

and

git clone git://github.com/cmcculloh/repo.git

I mean, obviously one is shorter, but other than that are they basically doing the same thing?

YakovL
  • 7,557
  • 12
  • 62
  • 102
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
  • 2
    `git pull` is the most useless Git command. And everybody uses it, without understanding what it does and why many times it is wrong to use it. – axiac Jun 06 '21 at 11:09
  • 3
    not true that `git pull` is useless.. just read the top answer.. it pulls in changes made by other users. it is the only way to start the merging of existing local changes with the remote ones. IMO you might as well start using pull over clone if you ever want to work collaboratively. – alchemy Apr 12 '22 at 19:11

12 Answers12

314

git clone is how you get a local copy of an existing repository to work on. It's usually only used once for a given repository, unless you want to have multiple working copies of it around. (Or want to get a clean copy after messing up your local one...)

git pull (or git fetch + git merge) is how you update that local copy with new commits from the remote repository. If you are collaborating with others, it is a command that you will run frequently.

As your first example shows, it is possible to emulate git clone with an assortment of other git commands, but it's not really the case that git pull is doing "basically the same thing" as git clone (or vice-versa).

ebneter
  • 20,795
  • 9
  • 30
  • 30
  • 11
    What specifically is it that git clone is doing that isn't accomplished by the sequence of commands that involved "git pull"? – cmcculloh Sep 02 '10 at 02:38
  • 34
    @cmcculloh: Nothing -- the sequence you describe effectively accomplishes what "git clone" does. The point is that "git pull" is used to do a variety of things beyond what you did there -- not to mention that "git pull" is actually exactly the combination of "git fetch; git merge ". IOW, you could live without clone *and* pull if you really wanted to. Further, you can pull from repositories other than the one you cloned from. I like to think of 'clone' as "make me a local copy of that repo" and 'pull' as "get me the updates from some specified remote." – ebneter Sep 02 '10 at 05:32
  • git clone requires a git init on the repo level and creates an additional layer of repo folder in the local repo. Personally I don't see the need for a .git file in the repo level and also an additional layer of repo folder. IMO, git pull is a more elegant method to create a new local repo. git init inside the project folder and no additional repo folder will be created. This is based on my limited tech exp. I might be wrong. – OLe3446 Jun 03 '22 at 03:37
163

In laymen language we can say:

  • Clone: Get a working copy of the remote repository.
  • Pull: I am working on this, please get me the new changes that may be updated by others.
Jyoti Prakash
  • 3,921
  • 3
  • 21
  • 24
147

They're basically the same, except clone will setup additional remote tracking branches, not just master. Check out the man page:

Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using git branch -r), and creates and checks out an initial branch that is forked from the cloned repository's currently active branch.

user229044
  • 232,980
  • 40
  • 330
  • 338
87

git clone means you are making a copy of the repository in your system.

git fork means you are copying the repository to your Github account.

git pull means you are fetching the last modified repository.

git push means you are returning the repository after modifying it.

In layman's term:

git clone is downloading and git pull is refreshing.

Eje
  • 354
  • 4
  • 8
38

Miss Clone: I get a fresh copy to local.

Mr Pull: I already have it locally, I just update it.


Miss Clone: I can do what you do! You are just my subset.

Mr Pull: Ditto!


Miss Clone: No, you don't create. This is what I do:

  1. Create empty bare repository in local computer.
  2. Populate remote-tracking branches (all branches in repo downloaded to local computer)
  3. Run git fetch without arguments

You only do #3, and then you merge(fetch + merge), which I do not need to do. Mine is fresh... girl!

Mr Pull: Smarty pants, no big deal, I will do a "git init" first! Then we are the same.


Miss Clone: No dear, don't you need a 'checked-out branch'... the git checkout? Who will do it? me!

Mr Pull: Oh right.. I need a checked-out local branch to act on. But wait.. you checkout master branch by default. Does anyone work on master branch? No! You are delivering a feature that is perhaps never used! I let the user decide the best branch to checkout and that is how I roll!


Git creators: Hold your horses Mr Pull, if --bare or --mirror is used with clone or init, your merge won't happen. It remains read-only. And for you Miss Clone, git checkout can be replaced with a git fetch <remote> <srcBranch>:<destBranch> unless you want to use a -s <strategy> with pull which is missing in fetch.


Miss Clone: On that same note about 'master' branch, I automatically sets up my local master branch to track the remote master branch. Your command takes dev for a ride and they are clueless!

Mr Pull waves at friends and walks away.. face slightly red..

Miss Clone: Yes, walk away but you are dependant on git checkout for remote tracking. pull wont bring changes from 'remote', when it is not tracked! And when the dev tries to push, they get merge issues! I don't depend on others to do my work. A maverick I am!

Git creators: Yes, it is tricky especially when checkout decides remote tracking. But when you do git status, or git branch -vv , it will show if you are tracking any remote branch. Based on this you can take the decision to force a remote tracking or not. It really is not Mr. Pull's fault. Devs are advised to learn more about git checkout and also commands to track origin.


Miss Clone: Somehow I feel like a winner already but let me drop this too: my command applies to all the branches in the repository. Are you that broad minded Mr. Pull?

Mr. Pull: I am broad minded when it comes to fetching all the branch names(just the 'name') from the repo. Because I don't like to fetch unnecessary branches and GBs to my hard drive (sometimes they 'pull' only to get latest branch names). But the merge will happen only on the current checked out branch. Exclusivity is the name!

Git Creators: Good job! Just one addition: Miss Clone can be restricted to just one branch if needed: git clone --single-branch --branch <branch name> <url>

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
13

clone: copying the remote server repository to your local machine.

pull: get new changes other have added to your local machine.

This is the difference.

Clone is generally used to get remote repo copy.

Pull is used to view other team mates added code, if you are working in teams.

Eje
  • 354
  • 4
  • 8
Suman Astani
  • 1,181
  • 11
  • 16
5

git clone is used for just downloading exactly what is currently working on the remote server repository and saving it in your machine's folder where that project is placed. Mostly it is used only when we are going to upload the project for the first time. After that pull is the better option.

git pull is basically a (clone(download) + merge) operation and mostly used when you are working as teamwork. In other words, when you want the recent changes in that project, you can pull.

Raman_1059
  • 51
  • 1
  • 3
3

While the git fetch command will fetch down all the changes on the server that you don’t have yet, it will not modify your working directory at all. It will simply get the data for you and let you merge it yourself. However, there is a command called git pull which is essentially a git fetch immediately followed by a git merge in most cases.

Read more: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Pulling

Eje
  • 354
  • 4
  • 8
simopr
  • 31
  • 1
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ekad Oct 01 '16 at 11:06
3

Clone-: It will create exactly duplicate copy of your remote repository project in your local machine.

Pull-: Suppose two or more than two people are sharing the same repository. (Suppose another person name is Syam) (A Repository is a place where your project exist in Github) So if Syam does some changes in the same project in his local and pushes it to the remote repository So whatever the changes Syam did those changes will not reflect in your local. So to reflect those new changes in your local you have to use git pull. Overall we use git pull to update the project.

So basically we use git clone only once whereas we use git pull many times.

3

git clone <remote-url> <=>

  • create a new directory
  • git init // init new repository
  • git remote add origin <remote-url> // add remote
  • git fetch // fetch all remote branchs
  • git switch <default_branch> // switch to the default branch

git pull <=>

  • fetch ALL remote branches
  • merge CURRENT local branch with tracking remote branch (not another branch) (if local branch existed)

git pull <remote> <branch> <=>

  • fetch the remote branch
  • merge CURRENT local branch with the remote branch (if local branch existed)
Linh
  • 57,942
  • 23
  • 262
  • 279
2

Hmm, what's missing to see the remote branch "4.2" when I pull, as I do when I clone? Something's clearly not identical.

tmp$  mkdir some_repo

tmp$  cd some_repo

some_repo$  git init
Initialized empty Git repository in /tmp/some_repo/.git/

some_repo$  git pull https://github.ourplace.net/babelfish/some_repo.git
  :
From https://github.ourplace.net/babelfish/some_repo
 * branch            HEAD       -> FETCH_HEAD

some_repo$  git branch
* master

vs

tmp$  rm -rf some_repo

tmp$  git clone https://github.ourplace.net/babelfish/some_repo.git
Cloning into 'some_repo'...
  :
Checking connectivity... done.

tmp$  cd some_repo

some_repo$  git branch
* 4.2
Scott
  • 1,247
  • 3
  • 10
  • 21
  • I notiiced this too, and I'm suspecting that changes in git defaults over time are the issue. I have 1.9.5.msysgit on windows and 2.3.2-applegit-55 on a mac. – AnneTheAgile Sep 08 '15 at 20:04
2

git clone URL ---> Complete project or repository will be downloaded as a seperate directory. and not just the changes git pull URL ---> fetch + merge --> It will only fetch the changes that have been done and not the entire project

Md Abrar
  • 51
  • 3