1. What is Git

This can you go to Google search out a lot of, git is a software manager, unusual is that it is distributed, not only have a central server to control the latest version of the code, and each developer has a local warehouse, so in the development process is to submit code onto a local repository again pushed to the center on the server, This advantage is that everyone is dependent on the central server to implement interaction, but it will not be a central server restrictions, even if a central server to hang, also can easily find the latest version of the code, and my own work can proceed smoothly, submitted to the local warehouse, after the repair center server, then what they warehouse push to the central server. Of course, there are many differences, but I’ll compare Git and SVN later.

Git Principle Summary

At the heart of Git is its object database, which holds Git objects, the most important of which are blob, Tree, and commit objects,

Blob objects implement logging of the contents of files,

The tree object implements the recording of file names and file directory structures.

The COMMIT object records additional information about the version submission time, version author, version sequence, and version description. These three types of objects perfectly realize the basic function of Git: recording version status.

Git references are pointer-like files pointing to the hash key value of a Git object. Git references make it easier to locate a version commit. Git branches, tags, and other functions are implemented based on Git references.

How to understand Git snapshots

What is a snapshot In computer systems, a snapshot is the state of a system at a particular point In time. I don’t think snapshots are meant to be ‘fast’. A snapshot should be understood as a record of the state of the entire system or application at one point in time. Imagine taking a picture of a table and recording the position and state of everything on the table.

We don’t need to store all the items, we just need to store this photo, and the next time we want to restore the previous state, we just need to turn out the photo at that time, and then put the items in the same position as the photo.

For example, suppose at time A you have file1 and file2 in your Git workspace, and at time B you modify file1. Git then prepares a snapshot of the current workspace pointer to the unmodified file File2 and the modified file1 data (that is, the current workspace file data state) before you are ready to make a commit. Therefore, at commit time, a snapshot is saved.

How to take a snapshot

Git reads all data in the current workspace, prestores it, and readjusts it. It and compare the last snapshot version of the content, for the file data has not changed, in current preloads git to get rid of redundant files of data, to point to retain a version of the file pointer to the data, for there are differences of file data will be preserved, finally complete the data preserved, it is carried out a snapshot.

Git vs. CVS, Subversion, etc

The difference between the two is in the way data is stored. The former is a tiny system that records and assembles a series of snapshot streams, concerned about whether the overall file data has changed. A snapshot is saved at each commit, and each snapshot contains complete data. The latter are concerned with specific differences in document content. The complete data is saved for the first time, and the subsequent saved data is not complete. Only the change information based on the previous version and the current two will be recorded. In addition, no change will be recorded.

2. Your own Git repository

To develop, submit code and interact with the central server, we first need to have our own development base, which is our own Git repository. There are two ways to own your own Git repository. One is to initialize your own git repository in your existing directory and then establish a connection with the central server to update the latest code to your own Git repository. The second is to clone an existing project into its own directory as its own Git repository.

The git init command creates and initializes a.git repository, creating a.git hidden folder in that directory. This directory is your working directory, and everything you do is init. If you think you’re ready to commit, you first commit to your local repository, this.git, and then push it to the central server. Or clone a repository locally as a Git repository. When you go into your.git directory, there are many subdirectories and files, some of which are very important. Here are a few:

The config file, which is the configuration file for your project, contains the central server information and branch information,

The HEAD file points to the current branch,

The index file is the information about the staging area,

The logs directory is full of logs from related operations. This is important because logs are the only evidence of our operations and our local version control depends on them.

The Objects directory stores all of your data, which is called snapshots,

The refs directory stores Pointers to data submission objects.

I don’t know what I’m talking about at first glance, mainly because a lot of the names in there don’t know what they are, but let’s take a look at some of the concepts of names, branches and commit objects and we’ll talk about that later in terms of version control, but let’s start with the basics.

3. Git hierarchy

When I first started to learn Git, I was always confused about what the working directory was, what the staging area was, what the local repository was, what the remote repository was, and what the snapshot was. I felt dizzy when I looked at it. But once these concepts are understood, basic development is not a problem.

Let’s take a look at git’s hierarchy as I understand it:

Git work a total of four layers, three layers of them are in front of their local is said git repository, including the working directory, the staging area and the local warehouse, working directory is we execute the command git init place, also is the place where we perform all file operations, and registers are in local warehouse. The git directory, Because they’re just for storing data. The remote repository is in the central server, where we push the remote repository after we’ve done our work, or update the latest code from the remote repository to our Git repository. Git stores a series of snapshots of files. Git then tracks these snapshots and notifies you if any of them change to either add them to the staging area or commit them to the local repository to keep your working directory clean.

Git files have two states: traced files that are submitted to the local repository, which is responsible for keeping track of them, and untraced files. So when we add a new file, it’s not tracked, because the local repository doesn’t have this file, it’s foreign, and the local repository isn’t responsible for them yet. However, if you are making changes to files that already exist in the repository, then these files are being traced and you can use Git status to check their status. You can also create a.gitignore file that specifies the types of files to ignore, and then these files will not be tracked. No matter how you change them, git status will not tell you what to do.

So when we do the file operation in the working directory, we first add the file snapshot to the staging area, then commit the newly added file snapshot in the staging area to the local repository, and then push the latest version file snapshot of the local repository to the remote repository. Git stores a series of snapshots of files as they are added to the staging area, just like a photo, leaving snapshots at different times for later retrieval. Git objects.

4. The object of git

Git is basically a content-addressing file system that stores key-value pairs and searches for values based on their keys. When it comes to addressing, git also addresses Pointers that are stored in Git objects. Git has three types of objects: Commit objects, tree objects, and blob objects. Here are the three objects:

The bloB object corresponds to the changed contents of the file in the file snapshot,

The tree object records the structural relationship between directories and files in the file snapshot. It points to the snapshot being traced.

The COMMIT object records a snapshot of each file committed to the local repository,

You can see from the figure above that there are two Pointers, one to the tree object and one to the previous commit object. During development, we will commit many snapshots of the file, so the first commit will be recorded by a COMMIT. This COMMIT has no pointer to the previous commit, because there was no previous commit, it was the first one, On the second commit, there will be a pointer to the last commit. After many commits, there will be many more Commit objects, which form a linked list. When we want to restore the version, You can restore that version of the file by finding the COMMIT object. The HEAD object actually refers to the last commit object, which is the last commit object.

Git basic operations

With all that said, it’s time to look at the basic operations, which are basically nothing more than adding, deleting, and updating files and backtracking versions. When it comes to figuring out the above, the basics are a breeze:

Git status is a command that guides you through your work and makes sure your working directory is clean. Git status is a command that guides you through your work and makes sure your working directory is clean. By clean, I mean consistent with the staging area and the local warehouse. Git log: Git log: git log: Git log: Git log: Git log: Git log

6. Learn to feel

SVN UP, SVN CI, AND SVN DI update, submit, and compare. SVN UP is to update the server code locally, and SVN CI is to submit the local code to the server. SVN DI resolves the conflict and submits it again. I had no idea what the structure was and what the results would be, and I was always careful to use it for fear of crashing the server. Learning is to know why, it will be easy to use, after understanding the basic principle, the operation of these commands is related to the use of natural. I am also a git beginner.

This article is a very in-depth analysis of git internal principles, so that I did not understand how much, ashamed: www.open-open.com/lib/view/op… More on git branches later.