git 本地分支的文件存储在哪里

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

Where are files for local branches stored

gitgit-bash

提问by ProgramKitkat

I ran I to an interesting thing today and that has me wondering how git handles local branches. So I was running into some issues on my repository and deleted the local folder (which went to recycle bin) and recloned.(extreme maybe) After I did I realized I deleted a local branch that I never pushed because it was a personal side project. I panicked for a moment and decided to restore the folder from the recycle bin and put it in a different location and see if I could get the few files I worked on back and push it to remote. At first I just tried to search using the file explorer couldn't find the files (I was sad) then I remembered I was in a different branch before I deleted so I redirected gitbash into the new location for the project old repo and 'git branch' behold all my old branches appear (git magic #1). So I checkout the branch in question and the file explorer happens to be open to the location I was expecting the file to be and behold the file magically appears (git magic #2)

我今天遇到了一件有趣的事情,这让我想知道 git 如何处理本地分支。所以我在我的存储库中遇到了一些问题并删除了本地文件夹(它进入了回收站)并重新克隆。(可能极端)在我这样做之后我意识到我删除了一个我从未推送过的本地分支,因为它是一个个人项目. 我慌了一会儿,决定从回收站中恢复文件夹并将其放在不同的位置,看看我是否可以取回我处理过的几个文件并将其推送到远程。起初我只是尝试使用文件资源管理器搜索找不到文件(我很难过)然后我记得我在删除之前在不同的分支中,所以我将 gitbash 重定向到项目旧仓库的新位置和 'git分支'看,我所有的旧分支都出现了(git magic #1)。

That leads me to wonder where does git store all the data for local branches. I know there is a hidden folder but I searched that for the file and it didn't show up is it compressed and renamed in there?

这让我想知道 git 将本地分支的所有数据存储在哪里。我知道有一个隐藏文件夹,但我搜索了该文件,但没有显示它是在那里压缩并重命名的吗?

回答by apprenticeDev

There's a hidden folder .gitin the project root directory (it gets created when you run git initor otherwise initialize a repository), in which git stores all the "magical bits". It's not extremely magical - there's a HEADtext file which says where the "head" is at (e.g. currently checked out branch or commit), there's refsdirectory with more directories that have files corresponding to your local and remote branch names - each of these files being just a text file with commit SHA (like a dictionary, when you say "check out master branch!" git will go and look for the corresponding file, read what commit it is - and check out that commit).

.git项目根目录中有一个隐藏文件夹(它是在您运行git init或以其他方式初始化存储库时创建的),其中 git 存储所有“神奇的位”。这并不是很神奇 - 有一个HEAD文本文件说明“头”的位置(例如当前签出的分支或提交),有refs更多目录的目录,其中包含与您的本地和远程分支名称相对应的文件 - 这些文件中的每一个都是只是一个带有提交 SHA 的文本文件(就像一本字典,当你说“检查主分支!” git 会去寻找相应的文件,阅读它是什么提交 - 并检查那个提交)。

Commits refer to "objects", conveniently in objects/directory. That dir actually contains a bunch more directories with two-letter names - which are first two letters SHA hash - which together with the file name inside directory makes the full hash (of a commit, tree or blob). Inside that two-letter dir, there are actual "objects" (git magic!). Objects can be of of type "blob" or "tree", former corresponding to files and latter (loosely) to directories. Read about git objects in the docs- it's an easy read that also gives you some tools to look at individual objects.

提交指的是“对象”,在objects/目录中很方便。该目录实际上包含更多具有两个字母名称的目录 - 它们是前两个字母 SHA 散列 - 与目录内的文件名一起构成完整的散列(提交、树或 blob)。在那个由两个字母组成的目录中,有实际的“对象”(git magic!)。对象可以是“blob”或“tree”类型,前者对应于文件,后者(松散地)对应于目录。在文档中阅读有关 git 对象的内容- 它很容易阅读,还为您提供了一些查看单个对象的工具。

So, if in .git/refs/heads/masteris a text file with contents a2789da8f918ef26c90e51d05de5723e5ad543a4- that means master is at that commit. The "state" of your project for that commit is stored in .git/objects/a2/789da8f918ef26c90e51d05de5723e5ad543a4- which is tree object for project directory, listing SHAs for all the files & directories in the project dir at that moment.

因此,如果 in.git/refs/heads/master是一个包含内容的文本文件a2789da8f918ef26c90e51d05de5723e5ad543a4- 这意味着 master 在该提交中。该提交的项目的“状态”存储在.git/objects/a2/789da8f918ef26c90e51d05de5723e5ad543a4- 这是项目目录的树对象,列出当时项目目录中所有文件和目录的 SHA。

So, you can't really browse around and find actual files from different branches - git doesn't think about things in that way, it thinks about things as lists of files & dirs. When you change a file and commit it, it creates a new "object" for contents of that file, and new tree objects for parent directories (with updated SHA for your updated file) and writes a commit object (tree object listing all the files & folders in project dir). Or at least that's my understanding, more or less.

因此,您无法真正浏览并找到来自不同分支的实际文件 - git 不会以这种方式思考事物,它会将事物视为文件和目录列表。当您更改文件并提交时,它会为该文件的内容创建一个新的“对象”,并为父目录创建新的树对象(更新后的文件具有更新的 SHA)并写入一个提交对象(列出所有文件的树对象& 项目目录中的文件夹)。或者至少这是我的理解,或多或少。

Hope that helps demystify it a bit!

希望这有助于揭开它的神秘面纱!



2017-12-21 edit: updated my old answer per comment from @Herman. For what it's worth, this answer here might be a bit "too much information without enough context".

2017-12-21 编辑:根据@Herman 的评论更新了我的旧答案。对于它的价值,这里的答案可能有点“没有足够上下文的信息太多”。

The shorter answer - git stores all the data in .gitdirectory in project root, it stores references to state of your project folder rather than separate copies of files.

简短的答案 - git 将所有数据存储.git在项目根目录中的目录中,它存储对项目文件夹状态的引用,而不是单独的文件副本。

For sake of posterity, if anyone stumbles upon this answer in the future - I highly recommend this course:

为了后代,如果将来有人偶然发现这个答案 - 我强烈推荐这门课程:

https://app.pluralsight.com/library/courses/how-git-works/table-of-contents(you can find offers for free trial for pluralsight and get a lot of value out of your trial).

https://app.pluralsight.com/library/courses/how-git-works/table-of-contents(您可以找到plexsight的免费试用优惠,并从试用中获得很多价值)。