git状态如何在内部工作? [英] How does git status work internally?

查看:130
本文介绍了git状态如何在内部工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







如何找出文件的SHA-1是什么?



使用 git ls-tree 内部命令找出你的文件的SHA-1是什么。










当您刚刚添加时,Git开始跟踪内容它到了临时区域。

一旦添加了文件,git将以下信息存储在文件中:

 如果你有一个带有字符串hello的文件,它将如下所示:
blob 5 \\ \\0Hello

现在git计算这个文件的SHA-1(使用 sha1sumn )用z-lib压缩它,并用这个SHA-1保存文件作为`.git / objects'的名字。



当git打包存储库时,它将进入包文件。



如何查看文件内容?



因为文件是用z-lib压缩的,所以我们有几个选项来获取内容:


  • 第一个选项是查看压缩文件存储在文件中

  • 其次是查看解压缩的内容(如上所述)
  • 仅在没有任何git元数据的情况下查看文件的内容。





为了显示git实际使用上面描述的内容,这里是和git在后台执行的计算SHA- 1








这是一个提交以及它如何处理文件名的说明。




From the git object model, files and folders are saved to locations in the .git folder by their sha1 hash.

How does git internally know if a file has been deleted, added, or edited (specifically, how does it compute the changes you see when you type git status)? Does the system determine this information purely from the sha1?

解决方案

The answer is bit too long and will take a while to write so here is the summary.

The short answer is that git uses the SHA-1 to check track changes but the name of the file is stored somewhere else.


The content is stored in a pack (*1- read below) while the names are stored in a idx.
When you run git status, git check to see if already have this path in the idx file (metadata) and based upon the result it decide if it's a new file or not.

If it's not a new file, git comparing the SHA-1 to track changes.


Why do I have to use git mv to move files and not a simple mv?

When you execute git status git search your working directory looking for a match between the "registered" paths in the idx file and your working directory.

When you move a file with mv your working directory does not have the "original" path stored by git, and since git can't find the "registered" path anymore, the file is marked as deleted.

But on the same time git see a new file, the new path you just the moved the file to, so the new file will be marked as a new file.

On the other hand, when using git mv, git updates the metadata to point to the new name and the content will be marked as rename. In this case git updates the registered path of the file in the idx file.
If you move and update it will be marked as rename + modified as well.


How can I find out what is the SHA-1 of a file?

Use the git ls-tree internal command to find out what is the SHA-1 of your files.


Note

Git starts to track content when you just add it to the staging area.
Once the file is added git store the following information in the file

[blob][1 white space][content length][null][content]

If you have a file with the string `hello` it will look like this:
blob 5\0Hello

Now git calculate the SHA-1 of this file (using sha1sumn) compress it with z-lib and save the file with this SHA-1 as the name nuder the `.git/objects'.

When git packs the repository it will go into the pack file.

How can I view the content of the file?

Since the file is compressed with z-lib we have several options to get the content:

  • First option is to view the zipped stored in the file
  • Second is to view the unzipped content (as explained above)
  • See the content of the file only without any git metadata.

And to show that git actually use the content as described above here is the same command as git executes behind the scenes to calculate the SHA-1


This is the illustration of a commit and how it handles file names.

(http://shafiulazam.com/gitbook/1_the_git_object_model.html)

这篇关于git状态如何在内部工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆