简体   繁体   English

GitHub存储库中的分支与文件夹有何不同?

[英]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.0master ),并且它们都碰巧指向同一提交。 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: 另一方面,文件夹有其自身的优点,例如:

  • You already know how to use them. 您已经知道如何使用它们。
  • If you want to diff two folders, recursive diff tools exist. 如果要比较两个文件夹,则存在递归比较工具。 But to diff two Git branches, you need a tool that's made for Git, which yours may not be. 但是要区分两个Git分支,您需要一个为Git制作的工具,而您可能没有。
  • You can open projects in two folders in two windows of your IDE and manually compare them, or apply changes one by one from one folder to another. 您可以在IDE的两个窗口中的两个文件夹中打开项目,然后手动进行比较,或者将更改从一个文件夹一个接一个地应用于另一个文件夹。
  • With branches, I sometimes accidentally make commits to the wrong branch. 使用分支时,有时我会意外地提交到错误的分支。 With folders, I've learnt to check which folder I've committed to. 使用文件夹,我学会了检查已提交到哪个文件夹。
  • You can use Spotlight on the Mac, or grep on the command line, to search for something, like a class name, and it will find across all folders, but not across all branches. 您可以在Mac上使用Spotlight或在命令行上使用grep来搜索某些内容(例如类名),它将在所有文件夹中找到,但不能在所有分支中找到。
  • Branches require you to follow a Git-specific procedure to track a remote branch, or to create a remote branch when you push a commit that creates a new local branch. 分支要求您遵循Git特定的过程来跟踪远程分支,或在您推送创建新本地分支的提交时创建远程分支。
  • If your repo has gotten too big, it's easy to see which folders are taking up space. 如果您的仓库太大,则很容易查看哪些文件夹占用了空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将分支从一个存储库移动到不同存储库的另一个分支 - How to move branch from one repository to another branch of different repository 将分支克隆到Github中的其他文件夹 - Clone branch to different folder in Github 如何从Github存储库中删除文件夹? - How to remove a folder from Github repository? 如何从github的存储库中删除文件夹? - How to remove a folder from a repository in github? 是否可以从不同的文件夹切换 git 存储库分支? - Is it possible to switch git repository branch from different folder? GitHub-将更改合并到与主文件夹不同的文件夹中 - GitHub - merging changes to a branch in different folder from master 如何将代码从新的本地文件夹推送到现有 Github 存储库的主分支并保留提交历史? - How do I push code from a new local folder to an existing Github repository's main branch and keep commit history? 如何将分支从一个GitHub存储库复制到另一个? - How to copy a branch from one GitHub repository to another? 如何检测文件夹是否为github存储库 - How to detect if a folder is a github repository 如何通过终端将包含 HTML 和 js 文件的文件夹远程推送到某人 GitHub 帐户上的存储库分支? - How to push a folder having HTML and js files remotely to a branch of a repository on someone's GitHub account by Terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM