How git clone actually works

git clone fetches all remote branches, but only creates one local branch, master, for you. So when you run git branch -a, you'll see something like this:

$ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/develop
  remotes/origin/master

which means that you have one local branch master and several remote branches. When you ran git checkout develop, git creates another local branch develop to track remote branch origin/develop. git tries to synchronize tracking branches, so you don't have to do another pull after check out.

If the local and remote branches terminologies sound confusing to you, you can browse through this document. It has some nice figures to help you understand them, and how local and remote branches move when you do further commits.

You may find this answer helpful: How to clone all remote branches in Git?, the first answer.


git clone fetches all branches of the repository by default. If you want to check out all branches, you need to clone a bare copy of the repository, unset the bare flag and reset it. Let me know if you have further issues.


To put it simply, git clone repository-url does the following things, in order:

  1. Creates a new empty repository.

    git init
    
  2. Creates a remote called "origin" and sets it to the given url.

    git remote add origin repository-url
    
  3. Fetches all commits and remote branches from the remote called "origin".

    git fetch --all
    
  4. Creates a local branch "master" to track the remote branch "origin/master".

    git checkout --track origin/master
    

An interesting point is that a fork (in GitHub or Bitbucket) is just a server side clone.


git clone first creates a new empty repository. (like git init)

It then sets the given repository as a remote called "origin". (git remote add)

The main work is then done by git fetch, which is the only command talking to other repositories. It transfers all commits of the remote repository to the current repository and creates inside your local repository branches starting with "remote/origin/" corresponding with the branches on the remote repository.

If you have a default non-bare repository, it will also call git checkout to checkout usually the master branch.

If you call git branch -r it will show you the "remote" branches, i.e. those branches in your repository, which will get updated by git fetch. (You never work on these directly.)

Whenever you want to work on a branch you use git checkout which will create a copy of that branch, without the "remote/origin/" prefix. Those are the "local" branches on which you work. (git branch will show those.)

Almost everything you do will involve only your local repository. The only exception is git push, which is the only command to update remote repositories, and git fetch which is the only command to query other repositories.

git pull is just the combination of git fetch and git merge. The first fetches changes and updates remote/origin/* and the second merges those changes into your local branch.

Tags:

Git