John Wick

Back

Git Quick Start

21 Jul, 2023
2 mins (399 words)

Basic concepts

Most Git-related articles tell you what commands Git has and how using them can speed up your development process, but few explore the inner workings of Git.

Consider the three basic concepts of a Git repository: Blob, Tree, and Commits.

Blob

Blob stands for Binary Large Object. Here’s a summary from ChatGPT:

  • Blob is one of the basic objects in Git for storing file contents.
  • Each Blob object is identified by the contents of the file and a SHA-1 hash value.
  • Blob objects, along with Git’s tree objects and commit objects, make up Git’s three basic object types.

Tree

Git uses tree objects to store folders. Each tree contains a set of subtrees (subfolders) or Blobs (files). It’s also identified by a hash value, and it’s worth noting that any modification to a sub-file will cause the hash value of the root tree to change, which can affect the whole tree.

Commits

Commits are composed of the Tree and some other metadata, also identified by hash values.

Ref

Git indicates a set of refs (starting with a null pointer) through a directed acyclic graph. These references are mainly for easy access because the hashes are too long. For example, a HEAD Ref is a pointer to the most recent commit.

Three regions

  • Working Directory: indicates the directory currently in use;
  • Staging Area: This can also be thought of as a cache or index where your changes are stored;
  • Repository: Commit the contents of the Staging Area to get the Git Directory, also known as a tree or database.

Merge and Rebase

The Chinese translation for merge and rebase, literally can be understood, the following appropriate expansion:

  • Git Merge keeps the original commits, and then has a new commit pointing to the HEAD, which may contain some new code created to resolve conflicts.
  • Git Rebase will copy the original Commits, and then continue to add new nodes backward using the old HEAD as a starting point, which ensures that the DAG graph remains a straight line.

Rebase is generally preferred so that your commits “stand out” (“the cream rises to the top”), but be careful not to use it if other people are developing based on your commits.

Conclusion

Finally, try to develop in Intellij, because its Local History can be a real life saver, and sometimes git reflog doesn’t always work.

Posted at 21 Jul, 2023