• How does git work and what does it do behind the command
  • Then the principle and use of Git can be more handy
  • In order to facilitate reading and understanding, the analysis is divided into two dimensions
  • From a positive perspective, init to add to commit
  • From the reverse point of view, the undo point of view
  • In order to be more intuitive, text description and screenshots are used to describe

From init to Add to commit

One, basic understanding operation1, git init// At this point, check the contents of.git
2Add something like a.j and git add.// Check the contents of.git.
// Run the command to see what is stored in the binary file
3Git ls-files-s4Git cat-file -p Specifies the hash value/ / summary:
1Git add:// ① File contents are stored in the Objects file. The first two bits of hash are the folder name, and the remaining 38 bits are the file name
// The latest hash value is recorded in index

2Git is a key-value file that records the hash value, and then use the hash value to find the file (key value) that the index record temporary storage of the corresponding value stored in the objects.Copy the code

What does hash Generation rule and multiple Add do1A. js add and git add.Git ls-files-s
2Add b.js and a lot of content, then git add.Git ls-files-s
3Add the missing contents in B. js and git add.// It can be found that the + change is not on the previous basis. It's the whole thing at this point

/ / summary:
1The hash value is related to the content of the file. If the content does not change, the hash value will not change and no additional hash content will be generated2Index records the latest Hash values of all files added this time. The calculated Hash remains unchanged for the unmodified part3Add is equivalent to making a content copy of the modified textCopy the code

Git commit-m1, git commit -m"init"
2Add again. Commit again to see the effect// The commit hash will be generated after the commit
Git ls-files-s
Git \refs\heads\master: the hash value for the latest commit is saved

/ / summary:
1Git commit generates a commit hash2The contents include tree,parent, submit user, submit time, submit message, etc.3, where tree records the contents of index in the temporary storage area at this time4Parent records the Hash value of the last commit node.5This key is stored in the current branch tree, which is currently.git\refs\heads\master// Commit is to snapshot the contents of the INDEX staging area
// Add other information about the submission to generate a submission record (Hash name, content store in objects)
Copy the code

Git add. 2, git commit -- find a new hash key and value // Git \refs\heads\master = commit tree = commit tree = commit tree = commit tree = commit tree Instead of creating a new COMMIT nodeCopy the code

5. What do you see when you switch branches1Git checkout -b feature/1

/ / check
Ref: refs/heads/feature1
Git \refs\heads\feature1
Copy the code

Two, from the reverse Angle, undo Angle analysis (I think it is better to understand with recovery)

I. Recovery workspace (rollback from temporary storage area)Copy the code

We can't restore the add stage after multiple adds, because no new nodes are created for each add, but only Index changes. The restore is from the commit, so restore to the first0Add phases, and cannot restore to the NTH recovery phaseCopy the code

Recover from the remote warehouseCopy the code