How git clone actually works

后端 未结 5 1311
暗喜
暗喜 2021-01-31 06:04

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

When I clone the repository and run $ git branch it shows only

5条回答
  •  [愿得一人]
    2021-01-31 06:15

    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.

提交回复
热议问题