Version control system
A version control system is software that helps software developers achieve teamwork and historical version maintenance. A version control system should have the following basic functions:
- Allow developers to work concurrently;
- [Fixed] One developer is not allowed to overwrite another developer’s changes
- Save all version history.
Version control systems can be divided into the following two categories:
- Centralized version control system;
- Decentralized (distributed) version control systems.
Git is a distributed version control system. In this chapter, we’ll focus on distributed version control systems, and Git in particular.
Distributed version control system
The main drawback of a centralized version control system, where all documents are stored and teams collaborate on a central server, is the possibility of a single point of failure on the central server: if the central server is down for an hour, there can be no collaborative development at all for that hour. In the worst case scenario, if the central server crashes completely before a successful backup, all historical versions of the project stored in the central server will be lost. At this point, it’s time to consider a distributed version control system.
Clients in a distributed version control system can not only view the latest snapshot of the project catalog, but also mirror the entire warehouse. If the server goes down, any repository image stored by the client can be used as a backup for recovery. Each inspection will form a complete backup of the warehouse. Git doesn’t rely on a central server, so developers can do everything offline. Developers can commit, branch, view logs, and more offline, and only need to connect to the network to publish their changes or get the latest version of the changes.
The advantages of Git
Free and open source
Git is distributed under the GPL open source license and is freely available throughout the Internet. You can use Git to manage property-related projects without spending a penny, and because it’s open source, you can download the source code and adapt it to your own needs.
Quick and light
Since the vast majority of operations can be done locally, speed is greatly improved. Git doesn’t rely on a central server, which is why it’s not necessary to interact with a remote server for every operation. The core of Git is written in C, which avoids the run-time waste associated with using a higher-level language. Even though Git mirrors the entire repository, the amount of data on the client side is still very small, which is a good example of how effective Git is at storing compressed data on the client side.
Default backup
When there are many mirror replicates, the possibility of data loss is greatly reduced. The data on any client is a mirror of the repository and can be used to recover in the event of a system crash or hard drive failure.
security
Git uses an encryption method called the Secure Hash algorithm (SHA1) to name and identify objects in the database. Check codes are added to each file and submission for verification, and are applied to each data retrieval. This means that it is impossible for a developer to successfully modify file data, commit information, or otherwise change a Git project database without knowing about Git.
Low requirements on hardware resources
When using a centralized version control system, the required central server must be powerful enough to support the requests of all team members. For small development teams, this is not a problem to solve, but as the team grows, the hardware limitations of the server become a bottleneck. In a distributed version control system, developers only need to connect to the server to push or pull changes, and all the heavy lifting is done on the client side, so the hardware conditions of the server can be easily planned.
Easier branch management
Centralized version control systems use a simple replication mechanism. If we create a branch, that branch copies all the project code into the new branch. This approach is inefficient and time-consuming, and deleting and merging branches in centralized version control systems is complex and time-consuming. But branch management is much easier in Git, where it takes very little time to create, delete, and merge branches.
A term used in distributed storage systems
Local Repository
All version control system tools provide a personal workspace in which the copied project can be manipulated. Developers make and submit changes in their own personal workspace that become part of the project repository. Git goes one step further by providing developers with a private copy of the entire repository, where they can do anything from adding files, deleting files, moving files, committing changes, etc.
Working Directory and Staging Area or Index
The working directory is the directory location where the document was pulled or created. In a centralized system, developers usually make changes and then submit them directly to the repository. Git does not keep track of every document that has been modified. Whenever you commit an operation, Git searches for existing documents in the staging area. Not all documents that have been modified, but only existing documents in the staging area are taken into account.
Let’s take a look at Git’s basic workflow:
- Step 1 – Modify a document in the working directory;
- Step 2 – Add this document to the staging area;
- Step 3 — Commit, which moves the document from the staging area to the local repository. Once the push is complete, the change is permanently stored in the Git repository.
If you have modified two files, sort.c and search.c, and you want to commit two files for each change, you can add one file to the staging area before committing, and then process the next file that way. The operation example is as follows. The parameters following -m are the description of this submission.
C # adds file to the staging area [jerry@CentOS ~]$git commit -- m "Added Sort operation "# Second commit [jerry@CentOS ~]$git add search. C # adds file to the staging area [jerry@CentOS ~]$git Commit -- m "Added search operation"Copy the code
Binary Large Objects (Blobs)
Blob is short for Binary Large Object. Every version of a file is rendered as Blob. A BLOB contains all of the file’s data except the file’s metadata. This is a binary file known in Git databases as a “secure hash of files”. In Git, files are handled not by name but by content.
A tree (Trees)
A tree is an object that represents a directory. It contains bloB-type files and other subdirectories, and a tree is a binary file that stores an index to a BLOB or a secure hash called a tree object.
Commits
Commit operations maintain the current state of the warehouse, and a commit is also named by a security hash. You can think of a commit object as a node in a linked list, with each commit object having a pointer to the parent commit node. From a given commit, you can go back and see the history of the commit by looking for the parent pointer. If a commit has more than one parent commit, the commit is created by merging two branches.
Branches
A branch is used to create another line that is opened. By default, Git has a master branch that works like Subversion, the SVN trunk. Typically, a branch is used for the development of a new feature, and once the new feature is developed, the branch is merged into the Master and then removed. Each branch can be indicated by the HEAD, which, without specifying it, always points to the branch’s last commit status. Whenever you complete a commit, the HEAD updates itself with the latest commit.
Tags
The tag assigns a meaningful name to a particular version in the repository. Tags are very similar to branches, except that they don’t change. This means that the tag is a branch that no one changes. Once a label is created for a particular commit, it does not update even if you complete a new commit. Typically, developers create labels for releases of their products.
Clone (s)
The clone operation creates an instance of the repository. A clone can not only view a copy of the current work, but also mirror the entire warehouse. Users can perform various operations on the local repository and only need to connect to the network when the repository instances are synchronized.
Pull (Pull)
The pull operation copies changes to the remote repository instance locally and is used in the synchronization of the two repository instances. The pull operation has the same effect as the update operation on the SVN.
Push, Push
The push operation copies changes made to a local repository instance to a remote repository. This operation is often used to permanently store changes made locally to a Git repository. The push operation has the same effect as the COMMIT operation on the SVN.
HEAD
HEAD is a pointer that always points to the latest commit in the branch. Whenever you complete a commit, the HEAD is always updated with the most recent commit. The branching heads are stored in the.git/refs/heads/ directory.
[jerry@CentOS ~]$ ls -1 .git/refs/heads/
master
[jerry@CentOS ~]$ cat .git/refs/heads/master
570837e7d58fa4bccd86cb575d884502188b0c49
Copy the code
Revision
A revision is a revision of the source code, and in Git it is represented by commits that are recognized by the secure hash algorithm.
URL
The URL represents the location of the Git repository, which is stored in the Git setup file.git\config.
[gituser@CentOS ~]$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = [email protected]:project.git
fetch = +refs/heads/*:refs/remotes/origin/*
Copy the code
I have organized the Git system articles into e-books, please click the following link to get them for free:
Link: pan.baidu.com/s/1mM6jK9B0… Extraction code: 1234
Finally, recently, many friends asked me for Linux learning roadmap, so I stayed up for a month in my spare time according to my own experience, and sorted out an e-book. Whether you are interviewing or self-improvement, I believe will help you!
Free to everyone, just ask everyone to point to me!
Ebook | Linux development learning roadmap
Also hope to have a small partner can join me, do this e-book more perfect!
Have a harvest? Hope the old iron people come to a triple whammy, give more people to see this article
Recommended reading:
- Dry goods | programmers advanced architect necessary resources free of charge
- Book | programmers must read classic books (hd PDF version)