git git中的repository和branch有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41398489/
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 12:31:22  来源:igfitidea点击:

What is the difference between repository and branch in git?

git

提问by DaeYoung

Someone who is well versed in git could help me to understand the difference between repo and branch. I am recently introduced to git and having a bit hard time to understand them. I was told to clone a remote repo (e.g. foo) to my local box. Then create a local branch out of it. Work (update/create/delete files) on the branch and add/commit/push to the remote server (e.g. bitbucket). After 2nd set of eyes review the branch and says ok. Then it gets merged to development or master branch.

精通 git 的人可以帮助我理解 repo 和 branch 之间的区别。我最近被介绍到 git 并且有点难以理解它们。我被告知将远程仓库(例如 foo)克隆到我的本地机器上。然后从中创建一个本地分支。在分支上工作(更新/创建/删除文件)并添加/提交/推送到远程服务器(例如 bitbucket)。在第二组眼睛检查分支后,说好的。然后它被合并到 development 或 master 分支。

Then what role, a repository plays in this picture? To me all the operations I ran is against branch...

那么,repository 在这张图中扮演什么角色呢?对我来说,我运行的所有操作都是针对分支的...

回答by Val Berthe

A repositoryis your whole project (directories and files) that you clone on your computer. A branchis a version of your repository, or in other words, an independent line of development.

一个是整个项目(目录和文件),您克隆您的计算机上。一个分支是一个版本库的版本,或者换句话说,发展独立的线。

A repository can contain multiple branches, which means there are multiple versions of the repository. The purpose of versioning your code after all, is that you can work on multiple aspects of your project at the same time - each of those evolving in different branches. Git uses the expression "working tree" (representing your workbench) alongside "branches".

一个存储库可以包含多个分支,这意味着存储库有多个版本。毕竟,对代码进行版本控制的目的是,您可以同时处理项目的多个方面——每个方面都在不同的分支中发展。Git 在“分支”旁边使用表达“工作树”(代表您的工作台)。

Related: If you want to know more about the local and remote branches.

相关:如果你想了解更多关于本地和远程分支的信息



Concerning the way of dealing with branches in your initial question:

关于在您最初的问题中处理分支的方式:

Clone a remote repo (e.g. foo) to my local box. Then create a local branch out of it. Work (update/create/delete files) on the branch and add/commit/push to the remote server

将远程存储库(例如 foo)克隆到我的本地框。然后从中创建一个本地分支。在分支上工作(更新/创建/删除文件)并添加/提交/推送到远程服务器

This is a nice and clean way to work with git. I suggest you always proceed like this :

这是使用 git 的一种很好且干净的方式。我建议你总是这样进行:

----A---B---C--     (REMOTE, master)

            |
            |       (Pull to local : `git pull origin master`)
            v

----A---B---C--     (LOCAL, master)

Then create a branch from your local master (git checkout -b branch1), work on it, commit your changes and push to remote :

然后从您的本地 master ( git checkout -b branch1)创建一个分支,对其进行处理,提交您的更改并推送到 remote :

----A---B---C------     (REMOTE, master)

----A---B---C---D--     (REMOTE, branch1)

               ^
               |
               |        (Push to remote : `git push origin branch1`)

----A---B---C------     (LOCAL, master)
             \
              D----     (LOCAL, branch1)

Then, when you're happy with your feature/fix/whatever, you can merge branch1into master.

然后,当您对您的功能/修复/任何内容感到满意时,您可以合并branch1master.

回答by Jake Henningsgaard

In Git, the repository refers to your entire project. Within a single Git repository, you have at least one branch. You can use git branch newFeatureto create a new branch within your repository to track your changes to the changes to your code base that pertain to a particular new feature.

在 Git 中,存储库是指您的整个项目。在单个 Git 存储库中,您至少有一个分支。您可以使用git branch newFeature在您的存储库中创建一个新分支来跟踪您对与特定新功能相关的代码库更改的更改。

Anytime you clone a repository you are creating a copy of the project repository on your local machine. In addition, you will also be able to git pullany new changes that other developers have pushed up to the public repository (learn more hereand here).

无论何时克隆存储库,您都是在本地计算机上创建项目存储库的副本。此外,您还可以将git pull其他开发人员推送到公共存储库的任何新更改(在此处此处了解更多信息)。

回答by Randy Leberknight

A repository contains all the information about your project, including a history of all the changes. Each change is entered into the repository in the form of a "commit".

存储库包含有关项目的所有信息,包括所有更改的历史记录。每个更改都以“提交”的形式输入到存储库中。

In order to show the difference between branches and repositories I will describe the process you mentioned and point out along the way the branches and the repositories being used. This is not intended to be a git tutorial, just an example so we can talk about branches and repos.

为了显示分支和存储库之间的区别,我将描述您提到的过程,并指出使用分支和存储库的方式。这不是一个 git 教程,只是一个例子,所以我们可以讨论分支和存储库。

Typically there exists a remote repository which users can get a copy of, and to which they can submit changes. No actual development work is done directly in the remote repo.

通常存在一个远程存储库,用户可以从中获取副本,并且可以向其提交更改。没有直接在远程存储库中完成实际的开发工作。

A user gets their first copy of a remote repository by cloning it. This will create a local repository on the users machine. The local repo is a complete copy of the remote repo.

用户通过克隆获得远程存储库的第一个副本。这将在用户机器上创建一个本地存储库。本地存储库是远程存储库的完整副本。

A branch is simply a pointer to one of the commits in the repository. In the simplest case, there is the default branch called "master", and master points to the most recent commit. Each commit is identified by a number, which is a hash of the repo at that moment. For example in this case master might point to commit:

分支只是指向存储库中提交之一的指针。在最简单的情况下,有一个名为“master”的默认分支,master 指向最近的提交。每个提交都由一个数字标识,这是当时 repo 的哈希值。例如,在这种情况下,master 可能指向 commit:

2d2cd6cf6f634e88ad19fa454fdcd2e1c26009c7

A developer can clone the remote repo, and checkout branch master. Then create and checkout development branch, (e.g. featureX-dev).

开发人员可以克隆远程存储库,并检出分支 master。然后创建并签出开发分支,(例如 featureX-dev)。

git checkout -b featureX-Dev

At this point both branches (master and featureX-Dev) point to the same commit. Make changes to your files. Commit the changes to the local copy of branch featureX-dev. Now, in your local repository, branch featureX-Dev points to a newer commit than master does. Push branch featueX-dev to the remote repo so it can be reviewed.

此时两个分支(master 和 featureX-Dev)都指向同一个提交。对您的文件进行更改。将更改提交到分支 featureX-dev 的本地副本。现在,在您的本地存储库中,分支 featureX-Dev 指向比 master 更新的提交。将分支 featueX-dev 推送到远程存储库,以便对其进行。

git push -u origin featureX-dev

The -u is used the first time the branch is pushed to the remote to tell git you want to track this branch.

-u 用于第一次将分支推送到远程时告诉 git 您要跟踪此分支。

Other developers (who have already cloned the remote repo) can get branch featureX-dev from the remote repository by performing a pull.

其他开发人员(已经克隆了远程存储库)可以通过执行拉取从远程存储库中获取分支 featureX-dev。

After they review it and tell you it's ok, then you can merge branch featureX-dev with your copy of master in your local repo, and push master.

在他们查看并告诉您没问题后,您可以将分支 featureX-dev 与您本地存储库中的 master 副本合并,然后推送 master。

But wait! What if some other developer has already pushed their changes to the remote master?

可是等等!如果其他开发人员已经将他们的更改推送到远程主服务器怎么办?

You checkout master in your local repo

您在本地仓库中结帐主人

git checkout master

Then pull master from the remote

然后从远程拉主

git pull origin master

You have the new changes that someone else made, and master points to their last commit. Now you can merge your dev branch into the local copy of master

您拥有其他人所做的新更改,并且 master 指向他们的最后一次提交。现在您可以将您的 dev 分支合并到 master 的本地副本中

git merge featureX-dev

If no one has changed master there is no harm done. The merge will just add your commits to the master branch. Resolve conflicts if any are created, and then push master back to the remote.

如果没有人改变主人,也没有什么坏处。合并只会将您的提交添加到主分支。解决冲突(如果有),然后将 master 推回远程。