-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_working_under_the_hood.txt
86 lines (82 loc) · 6.99 KB
/
git_working_under_the_hood.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
--------- ------------------------------------------------------------------------------------
git init | For initializing an empty GIT repository. This will create a new hidden folder called .git
---------------------------------------------------------------------------------------------
Git Object Types | BLOB, ANNOTATED TAG, COMMIT, TREE
----------------------------------------------------------------------------------------------
BLOB | Represents the files
----------------------------------------------------------------------------------------------
ANNOTATED TAG | It is a pointer to a specific commit
----------------------------------------------------------------------------------------------
COMMIT | Stores different versions of the project
----------------------------------------------------------------------------------------------
TREE | Stroes information about directories. It can be a set of BLOBS and TREES
----------------------------------------------------------------------------------------------
echo "Hello, Git" | git hash-object --stdin -w => output of echo , i.e Hello, Git are taken as input to git hash-object and used to create a new hash object (-w)
----------------------------------------------------------------------------------------------
git hash-object | This is used to create a new object in GIT and it is created in the "objects" folder.
----------------------------------------------------------------------------------------------
Hash Function | Takes variable length input and produces fixed lenght output. They are one-way function. (From Hash, we cant get i/p).
Same input will always give the same Hash value
----------------------------------------------------------------------------------------------
Type of Hash func| MD5 (128 bit long), SHA1 (160 bit long), SHA 256 (256 bit long), SHA 384 (384 bit long), SHA 512 (512 bit long)
----------------------------------------------------------------------------------------------
**Git uses SHA1**
----------------------------------------------------------------------------------------------
SHA1 | 160 bit long. represented by 40 hexadecimal charecters
----------------------------------------------------------------------------------------------
shasum | Generate Hash values in command line
----------------------------------------------------------------------------------------------
echo "Hello, Git" | shasum => will generate a 40 digit 160 bit hash value
----------------------------------------------------------------------------------------------
How many git files can be stored in the same repository ? : 2^160 since the length of a SHA1 hash is 160 bits and with Base 2 its 0 or 1 hence 2^160
----------------------------------------------------------------------------------------------
What are the chances of producing the same hash for 2 different files ? : 1/2^160 *1/2^160 = 1/2^320 = 0. 96 zeros
----------------------------------------------------------------------------------------------
git cat-file | Read the contents of any object in a Git repository
----------------------------------------------------------------------------------------------
git cat-file -p <hash> | Contents of the Object
----------------------------------------------------------------------------------------------
git cat-file -s <hash> | Size of the Object
----------------------------------------------------------------------------------------------
git cat-file -t <hash> | Type of the Object
----------------------------------------------------------------------------------------------
git hash-object ../new-file.txt -w | Create a new object with file name
----------------------------------------------------------------------------------------------
Objects created in Git are independent of file system. Git has its own database
--------------------------------------------------------------------------------t--------------
Git blobs dont store file name. It stores the name of the hash value
----------------------------------------------------------------------------------------------
BLOB's HASH = Content (Ex. Hello, Git) + Type (BLOB) + Length (11) + Delimeter (\0 i.e Null charecter) = GIT Object Content
----------------------------------------------------------------------------------------------
****** Git generates SHA1 Hash based on input + type + size *****
----------------------------------------------------------------------------------------------
****** HASH = Content + Type + Length + Delimeter ****** => This is true for any GIT OBJECT
----------------------------------------------------------------------------------------------
echo -e "blob 11\0Hello, Git" |shasum => gives the same output as echo "Hello, Git" | git hash-object --stdin -w
----------------------------------------------------------------------------------------------
TREE : A contents of the tree contains PERMISSION + (whether its a tree/blob) + SHA1 Hash + name of the file/directory
----------------------------------------------------------------------------------------------
TREE's HASH = Content (Ex. PERMISSION + (whether its a tree/blob) + SHA1 Hash + name of the file/directory) + Type (BLOB) + Length (11) + Delimeter (\0 i.e Null charecter) = GIT Object Content
----------------------------------------------------------------------------------------------
Contents of the Tree :
100644 blob b7asdasdadadau8282392naadada file1.txt
100644 blob 4400aee5adadau8282392naadhaw file2.txt
----------------------------------------------------------------------------------------------
Permission for tree | 040000 => directories, 100644 =>Regular Non-executable file
----------------------------------------------------------------------------------------------
git mk tree | Create a tree
----------------------------------------------------------------------------------------------
find .git/objects -type f | This is used to find all the files in a folder
----------------------------------------------------------------------------------------------
cat ../temp-tree.txt | git mktree => The temp-tree.txt file is used as an input to the mktree file and the temp-tree.txt had info on 2 blobs
----------------------------------------------------------------------------------------------
3 areas where files in GIT reside
=================================
1. Working Directory | Files which are present locally
2. Staging area (index) | Mandatorily used as an intermediate betweem working and GIT repository
3. Git repository | All files are saved in the name of SHA1 Hash of the object
----------------------------------------------------------------------------------------------
git ls-files -s | Used to see the files in the STAGING AREA
git read-tree <hash> | Put the content from git repository to staging
git checkout-index -a | Puts the files from staging to working directory
There can be a maximum of 256 folders in a .git/objects folder i.e 16 (hexadecimal digits) *16=256