Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How git clone actually works

Tags:

git

I have a repository on Github with 2 branches: master and develop.

When I clone the repository and run $ git branch it shows only the master branch.
If I run $ git branch -a I can see all the remote branches.

Now, if I do a $ git checkout develop, I get the message:

Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

What actually happened? Were the commits from the remote develop branch fetched when I ran $ git clone remote-url, or when I ran: $ git checkout develop, or neither?

Have I to do a $ git pull origin develop after checking out develop, or it's already done?

Please help me understand how clone works when there are multiple branches on remote.

like image 312
Tamás Pap Avatar asked May 07 '13 20:05

Tamás Pap


People also ask

What does git clone actually do?

git clone is primarily used to point to an existing repo and make a clone or copy of that repo at in a new directory, at another location. The original repository can be located on the local filesystem or on remote machine accessible supported protocols. The git clone command copies an existing Git repository.

How does GitHub cloning work?

When you clone a repository, you copy the repository from GitHub.com to your local machine. Cloning a repository pulls down a full copy of all the repository data that GitHub.com has at that point in time, including all versions of every file and folder for the project.

How does git clone work under the hood?

When you ask for a project, you clone it locally on to your machine. In other words, Git copies all the project files to your hard drive, then allows you to work on the project autonomously. All operations run locally on your machine.


1 Answers

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.

like image 53
rouble Avatar answered Oct 02 '22 20:10

rouble