What is “version control”? Why should I care? Version control is a system that records changes to the contents of one or more files for future reference of revisions to a particular version.

Git installed

Go to the git official website to download the installation package (Windows as an example, MAC installation steps are similar, you can search by yourself), as shown below:

Click Download 2.30.1 for Windons to Download and install.

Then run the git version command. If the version number is displayed, the installation is successful.

Git initial configuration

After git is installed successfully, it is important to configure git user information. Every time we commit, we refer to the user’s information, indicating who committed the update.

Git config --global user.email '[email protected]' // Check the existing configuration  git config --listCopy the code

As of now, the initial configuration is complete.

Git has three major work areas and workflows

  • Workspace: the directory you see on your computer.
  • Staging area: stage or index. Git /index. This is why the staging area is sometimes called an index.
  • The workspace has a hidden directory. Git, which is not a workspace but a git repository.

1. Initialize git in a file (create git repository) or clone repository

If you are creating a project, go to the root directory where you want to manage the project and run git init.

For a remote clone repository, run git clone remote repository address.

2. Check the file status

Run git status to check the file status. For any file, there are only three states in Git: Committed, Modified, and staged. Committed indicates that the file is securely stored in the local database. Modified indicates that a file has been modified but has not been submitted for saving. Staging means putting the modified file in the list to save for the next commit.

3. Place the file in the temporary storage area

Before the git add filename command is executed, the file state is modified as shown below:

After executing the command, the state of the file is temporary, as shown below:

4. Submit the file from the staging area to the warehouse

Run git commit -m to commit to the local repository.

5. Modify submitted files

Git add file name -> git commit -m

6. Commit the local warehouse to the remote warehouse

Git remote add git remote add git remote add git remote add git remote add git remote add git remote add git remote add

Git push -u repository name branch name

Git internals

Because Git was originally a toolset for a version control system, rather than a complete, user-friendly version control system, it also includes a subset of subcommands for doing low-level work. These commands are designed to be linked together in UNIX command-line style or invoked by scripts to do the job. This part of the command is often called a “plumbing” command, while the friendlier command is called a “porcelain” command.

Git object

At the heart of Git is a simple key-value data store. You can insert any type of content into a Git repository, and it returns a unique key that can be retrieved again at any time.

There are two problems with Git objects, however. The first is that it is not practical to remember the sha-1 value for each version of a file, and the second is that in this (simple version control) system, file names are not saved — we only save the contents of the file. Hence the following solution tree object.

The tree object

It solves the problem of filename preservation and allows us to group multiple files together. Git stores content in a manner similar to UNIX file systems, but with some simplification. Everything is stored as tree objects, which correspond to UNIX directory entries, and data objects, which roughly correspond to inodes or file content. A tree object contains one or more tree entries. Each entry contains a SHA-1 pointer to a data object or subtree object, and the corresponding mode, type, and file name information.

Git update-index: git write-tree: git write-tree: git write-tree: git write-tree A tree object is a version snapshot.Copy the code

However, the problem remains: to reuse these snapshots, you must remember all three SHA-1 hashes. Also, you have no idea who saved the snapshots, when they were saved, or why. This is the basic information that a Commit Object can store for you.

Submit the object

You can create a commit object by calling the commit-tree command, specifying the SHA-1 value of a tree object and the parent commit object (if any) of the commit.

A commit object is a wrapper around a tree object (described in more detail) that really represents a project version, whereas a Git object only represents a version of a file.Copy the code

In git version control, the most important object is the three objects, git object, tree object, commit object specific underlying commands and implementation refer to git documentation

The underlying principles of some upper-level commands

  • git add

You first create a Git object to add to the local repository, and then add the Git object to the buffer.

For example, now I create a file version. HTML and execute the following command:

  • git commit

Create a tree object (version snapshot) from the contents of the cache, and then create a commit object.