What is the difference between pull and clone in git?
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 开发者_StackOverflowsame thing?
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).
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.
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.
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.
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:
- Create empty bare repository in local computer.
- Populate remote-tracking branches (all branches in repo downloaded to local computer)
- 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!
精彩评论