Git Fundamentals
What is Git?
Git is a distributed version control system (DVCS), which is quite different from a centralized version control system such as SVN. The biggest difference is how they handle data:
- Most version control systems, such as SVN, store data as a series of files that record changes to project files
- Git stores data that is more like a series of snapshots, or snapshot streams, of a tiny file system
The diagram below:(SVN stores file changes)
Git creates a file snapshot with each release.
The advantage of this is that you can easily go back to any version instead of comparing every change.
How does Git manage files
1. Use hashing. Hashing is a series of encryption algorithms (Git uses SHA-1 at the bottom).
- Regardless of the size of the input data, using the same hash algorithm results in a fixed length of encryption (git uses 40 bits).
- The hash algorithm is determined, the input data is determined, and the output result is guaranteed to remain unchanged
- The hashing algorithm determines that if the input data changes, the output must change, usually by a large margin
- The hash algorithm is not reversible
- Hashing algorithm is case insensitive
2. Git views data as a set of snapshots of a small file system. Every time Git commits an update, it takes a snapshot of all the current files and stores the index of that snapshot. To be efficient, Git doesn’t re-store the file if it hasn’t been modified, but only keeps a link to the previously stored file. So the way Git works is called snapshot flow.
How does Git store files
- Git is a content addressing system
- The heart of Git is a simple key-pair database
A chestnut
git init
Copy the code
First, initialize a Git repository
You can use the Vim editor to create a new 1.txt for 11111
ls -al
Copy the code
If you look at the file list, you will find that there is not only a 1.txt file but also a.git file
brew install tree
brew install watch
Copy the code
Install the first one to display your files in a tree structure, and the other one to monitor changes to your files
watch -n .5 tree .git
Copy the code
You can open a new window and run this command to listen for file changes once every 0.5 seconds
You can see git’s tree structure
rm -r .git/hooks
Copy the code
Remove hooks for clarity, focus on objects and refs
Git add 1. TXT: git add 1.txt: git add 1.
- f7 (c6dd01 …)
git cat-file -t f7c6
git cat-file -p f7c6
Copy the code
The first command shows that the file type is BLOb, and the second command shows what the file stores
Git commit -m “first commit
- 87 (d37b26)
- fa (d18af3)
Git cat-file -t / -p you can run git cat-file -t / -p to check the file type and storage information
- 87 (D37B26) The file type is a tree. The file type is BLOb and the hash value is F7 (C6DD01…). And the name 1.txt
Git only stores one copy of a blob file in a tree. Git only stores one copy of a blob file in a tree. Git only stores one copy in a tree
- The fa (D18AF3) file type is a COMMIT, which stores the commit information — “First commit” — and stores a tree pointing to 87 (D37B26), so that each version of the commit information can be found. You can trace back to any version
You can create a new file, such as 2.txt, that contains the same contents as 1.txt. When you run git add 2.txt, git will not store the file again
TXT file. The contents of the 3. TXT file are different from those of the previous two times. This time, run git add. Git will store it and add another store of information
- 5a (cf9b10)
Git cat-file -p 5acf git cat-file -p 5acf git cat-file -p 5acf git cat-file -p 5acf git cat-file -p 5acf git cat-file -p 5acf
git commit -m "second commit"
Copy the code
There are two more stored messages
- ed (cb4edd)
- c4 (884dd6)
We found it when we ran the view command
-
Ed (CB4EDD) This message stores the following information
- There is a tree pointing to c4 (884DD6)
- There is a parent pointing to fa (D18AF3), the last commit information
- Information for the second submission
-
C4 (884DD6) stores concrete content
A small summary
- Git objects:
- Blob: Stores data
- Tree: A pointer to the file name, content, and other trees
- Commit: author information, commit information, and a pointer to the parent’s last commit
- The process is as follows:
The above image is an existing file system that has been changed to 1.txt, as shown below
When git commit is executed, the tree generates a new index to the file, and the base tree generates a new index based on the index of a tree in the staging area. Commit also generates a new COMMIT with a pointer to the new tree and the pointer to the previous parent, as shown below.
Git branch changes
Branch merge conflict:
- fast-forward:
When there is no conflict, change the pointer pointer directly
- no-fast-forward
When there is a conflict, a new pointer is created to both master and dev