How git clone really works

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

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

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

A branch is being developed to track a remote branch from the source.
Switch to the new "develop" branch

What really happened? Were the remote develop branches commit when running $ git clone remote-url or when I ran: $ git checkout develop or none?

Do I need to run $ git pull origin develop after check develop , or is it already done?

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

+7
source share
5 answers

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

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

which means that you have one local master branch and several remote branches. When you run git checkout develop , git creates another local develop branch in track, the remote branch origin/develop . git trying to sync tracking branches, so you don't need to do another pull after check out .

If the terminology of local and remote branches is confusing, you can view this document . This has some good numbers to help you understand them and how the local and remote branches move when you do even more.

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

+9
source

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

Then it sets the given repository as remote, called "origin". ( git remote add )

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

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

If you call git branch -r , it will show you the "remote" branches, i.e. the branches in your repository that will be updated using git fetch . (You never work on this directly.)

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

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

git pull is just a combination of git fetch and git merge . The first selection modifies and updates remote / origin / *, and the second merges these changes into your local branch.

+6
source

git clone fetches all repository branches by default. If you want to check all branches , you need to clone an open copy of the repository, disable the Naked flag and reset it . Let me know if you have additional problems.

+1
source

When you clone a repository, you will get all branches and all commits that can be reached from any of these branches.

However, you will not get the local branch of any branch other than the master. The rest are remote branches (remotes / origin / development), and you can check any of them whenever you want. git will then set up tracking between the remote branch and the local one created during the check.

+1
source

Simply put, git clone repository-url does the following to:

  • Creates a new empty repository. ( git init )
  • Installs this repository as a remote, called "origin". ( git remote add source repository-url )
  • Selects all commits and remote branches from a remote device called "origin". ( git fetch --all )
  • Creates a main branch local to track the source / leading branch of the branch ( git checkout --track origin / master )

An interesting point is that fork (in github or bitbucket) is just a server-side clone.

0
source

All Articles