[英]How is a branch different from a folder, in a GitHub repository?
I'm new to GitHub, and I don't exactly understand what a branch is. 我是GitHub的新手,我不太了解分支是什么。 I got a feeling it is like a folder.
我觉得它就像一个文件夹。 Please explain: what exactly is the difference between folder and a branch?
请说明:文件夹和分支之间到底有什么区别?
Good answers so far; 到目前为止,答案很好; I'll add my two cents.
我加两分钱。 You already know what a folder is, so next we have to think about what a commit is.
您已经知道文件夹是什么,因此接下来我们必须考虑什么是提交。 A commit is a snapshot of a folder - specifically, the root folder of your repository at some point in time.
提交是文件夹的快照,特别是某个时间点的存储库的根文件夹。 A branch is simply a pointer to a commit.
分支只是指向提交的指针 。 Take a look at the following image from the excellent Git book :
看一下优秀的Git书中的以下图像:
In this picture, the gray boxes are commits. 在此图片中,灰色框是提交。 You can see that each one has its own snapshot of the main folder.
您可以看到每个文件夹都有自己的主文件夹快照。 The orange boxes are branches - note how they are just pointers to a commit.
橙色框是分支-请注意它们仅是提交的指针。 In this case, there are two branches (
v1.0
and master
), and they both happen to point to the same commit. 在这种情况下,有两个分支(
v1.0
和master
),并且它们都碰巧指向同一提交。 HEAD
points to the branch you are currently on. HEAD
指向您当前所在的分支。 So if you were to make a new commit in the above case, master
would point to that new commit, whose parent would be f30ab
. 因此,如果您要在上述情况下进行新的提交,则
master
将指向该新提交,其父对象为f30ab
。 v1.0
would still point to f30ab
. v1.0
仍然指向f30ab
。
So while branches can somewhat act like folders in the sense that different branches can "contain" different (versions of) files, in reality it is different commits that contain these different (versions of) files, and a branch simply points to a commit. 因此,尽管从某种意义上说,分支可以像文件夹一样工作,因为不同的分支可以“包含”不同的(版本)文件,但实际上,不同的提交包含这些不同的(版本)文件,而分支只是指向提交。
BTW, as others have mentioned, you should really read the Git book . 顺便说一句,正如其他人所提到的,您应该真正阅读Git书 。 Working with Git is so much more pleasurable when you know the fundamentals.
当您了解基本知识后,与Git一起工作就变得更加愉快。
In a way, they are both sets of files. 在某种程度上,它们都是文件集。 However, their concepts come from different environments and the usage is very different.
但是,它们的概念来自不同的环境,用法也非常不同。
A folder is a feature of file system. 文件夹是文件系统的功能。 I hope I don't have to explain more since it's a basic computing concept.
我希望我不必多解释,因为它是一个基本的计算概念。 You can nest folders and they have no special features regarding version control systems (mainly history).
您可以嵌套文件夹,并且它们没有关于版本控制系统的特殊功能(主要是历史记录)。
A branch is a feature of version control systems. 分支是版本控制系统的功能。 It allows you to manage more development tasks at a time.
它允许您一次管理更多的开发任务。 They contain history and you can check at any time from which commit they diverted.
它们包含历史记录,您可以在任何时间检查它们从哪个提交转移。 However, you cannot nest them in Git.
但是,您不能将它们嵌套在Git中。
I would start to learn how to use branches in Git on https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging . 我将在https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging上开始学习如何在Git中使用分支。
read the article to understand how github works. 阅读文章以了解github如何工作。
A branch is like a work in progress, you make a copy of your main code (or part of it) make changes (add new feature or fix a bug) and when you have tested it that it works perfectly fine you merge it back to main branch. 分支就像进行中的工作,您可以复制主代码(或部分代码)进行更改(添加新功能或修复错误),并且在对其进行测试以使其工作正常时,可以将其合并回主枝。 So whole idea is basically have a development branch and a stable working branch which you can use if something goes wrong during changes.
因此,整个想法基本上是由一个开发分支和一个稳定的工作分支组成,如果在更改期间出现问题,可以使用该分支。
As Melebius said, they both are ways to keep two different versions of your code. 正如Melebius所说,它们都是保留代码的两个不同版本的方法。
A folder is a feature of the filesystem. 文件夹是文件系统的功能。
A branch is a feature of version control systems, which means that Git tracks at which point in the version history you created it. 分支是版本控制系统的功能,这意味着Git会跟踪您在其创建的版本历史中的哪一点。
Git lets you merge a branch back into mainline, merging them. Git使您可以将分支合并回到主线,并进行合并。 You might create a branch to work on something that may or may not work out, making several commits till you get it to a working state, at which point you merge.
您可以创建一个分支来处理可能会或可能无法解决的问题,进行几次提交,直到将其恢复为工作状态为止,此时您将合并。
Or move individual commits from mainline to the branch or vice-versa. 或将单个提交从主线移到分支,反之亦然。 You might use this when you made a bug fix to the branch that you later realise also applies to mainline.
在对分支进行错误修复后,您可能会使用此功能,后来您意识到该分支也适用于主线。
All these benefits disappear with folders. 所有这些好处随文件夹消失。
On the other hand, folders have their own benefits like: 另一方面,文件夹有其自身的优点,例如:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.