git clone 的实际工作原理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16427600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:30:22  来源:igfitidea点击:

How git clone actually works

git

提问by Tamás Pap

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

我在 Github 上有一个存储库,有 2 个分支:masterdevelop.

When I clone the repository and run $ git branchit shows only the masterbranch.
If I run $ git branch -aI can see all the remote branches.

当我克隆存储库并运行$ git branch它时,它只显示master分支。
如果我运行,$ git branch -a我可以看到所有远程分支。

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

现在,如果我执行 a $ git checkout develop,我会收到消息:

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

分支开发设置为从源跟踪远程分支开发。
切换到一个新的分支 'develop'

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

究竟发生了什么?是develop在我运行$ git clone remote-url时获取远程分支的提交,还是运行时获取:$ git checkout develop或两者都不获取?

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

$ git pull origin develop退房后我需要develop做吗,还是已经完成了?

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

请帮助我了解clone远程有多个分支时的工作原理。

采纳答案by Yang

git clonefetches 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 clone获取所有远程分支,但只master为您创建一个本地分支 , 。所以当你运行时git branch -a,你会看到这样的东西:

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

which means that you have one local branch masterand several remote branches. When you ran git checkout develop, git creates another local branch developto trackremote branch origin/develop. gittries to synchronize tracking branches, so you don't have to do another pullafter check out.

这意味着您有一个本地分支master和多个远程分支。当您运行时git checkout develop,git 创建另一个本地分支develop跟踪远程分支origin/developgit尝试同步跟踪分支,因此您不必pullcheck 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 中克隆所有远程分支?,第一个回答。

回答by rouble

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

简单来说git clone repository-url,就是依次做以下事情:

  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 localbranch "master" to track the remote branch "origin/master".

    git checkout --track origin/master
    
  1. 创建一个新的空存储库。

    git init
    
  2. 创建一个名为“origin”的远程并将其设置为给定的 url。

    git remote add origin repository-url
    
  3. 从名为“origin”的远程获取所有提交和远程分支。

    git fetch --all
    
  4. 创建一个本地分支“master”来跟踪远程分支“origin/master”。

    git checkout --track origin/master
    

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

有趣的一点是,fork(在 GitHub 或 Bitbucket 中)只是服务器端的克隆。

回答by michas

git clonefirst creates a new empty repository. (like git init)

git clone首先创建一个新的空存储库。(喜欢git init

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

然后它将给定的存储库设置为名为“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.

然后主要工作由 完成git fetch,这是与其他存储库交谈的唯一命令。它将远程存储库的所有提交传输到当前存储库,并在本地存储库中创建以“remote/origin/”开头的分支,与远程存储库上的分支相对应。

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

如果你有一个默认的非裸存储库,它也会调用git checkout通常的 master 分支来检出。

If you call git branch -rit 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.)

如果您调用git branch -r它,它将向您显示“远程”分支,即您存储库中的那些分支,它们将通过git fetch. (你永远不会直接处理这些。)

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

每当您想在一个分支上工作时,您都会使用git checkout它来创建该分支的副本,而没有“remote/origin/”前缀。这些是您工作的“本地”分支机构。(git branch将显示那些。)

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 fetchwhich is the only command to query other repositories.

您所做的几乎所有事情都只涉及您的本地存储库。唯一的例外是git push,这是更新远程存储库git fetch的唯一命令,也是查询其他存储库的唯一命令。

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

git pull只是相结合git fetchgit merge。第一个获取更改并更新 remote/origin/* ,第二个将这些更改合并到您的本地分支中。

回答by Andreas Wederbrand

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

当您克隆存储库时,您将获得所有分支以及可以从这些分支中的任何一个访问的所有提交。

You will however not get a local branch of any other branch than master. The other ones are there as remote branches (remotes/origin/development) and you can check out any of those whenever you want. git will then set up tracking between the remote branch and the local one that you created when you did the check out.

但是,除了 master 之外,您不会获得任何其他分支的本地分支。其他的作为远程分支(远程/起源/开发)存在,您可以随时查看其中的任何一个。然后,git 将在远程分支和您在签出时创建的本地分支之间设置跟踪。

回答by hd1

git clonefetches 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.

git clone默认情况下获取存储库的所有分支。如果要检查所有分支,则需要克隆存储库的裸副本,取消设置裸标记并重置它。如果您有其他问题,请告诉我。