Git Study Notes (1), Git Study Notes (
Version Control System Overview
A version control system is a system that records changes in the content of several files for future reference to specific version revisions. The system can not only control the version of text files of software source code, but also control the version of any other types of files. With the version control system, we can trace a file back to the previous state, or even trace the entire project back to the previous state at a certain time point; we can compare the details of the changes in the file and find out who modified the last part, which leads to a weird problem and who reported a functional defect.
Types of Version Control Systems
1. Centralized Version Control system (CVCS): such as CVS, Subversion, and Perforce. Such systems have a single centrally managed server that stores the revision of all files, and the collaborators connect to the server through the client to retrieve the latest files or submit updates.
Advantage: everyone can see to some extent what others are doing in the project, and the administrator can easily control the permissions of each developer, managing a CVCS is far easier than maintaining the local database on each client.
Disadvantage: single point of failure (spof) of the central server. If the central server fails, for example, the central server disk fails or crashes, it may fail to submit updates or data loss.
2. Distributed Version Control System (DVCS): such as Git, Mercuril, Bazaar, and Darcs. The client not only extracts the latest file snapshot, but also completely mirrors the original code repository. In this way, any server that works collaboratively can be recovered from a local warehouse from any image. Because every extraction operation is actually a complete backup of the code repository.
Git Introduction
Git was developed by Linux's originator Linus Torvalds and used to manage Linux kernel code. It is fast, easy to design, and strongly supports non-linear development modes (allowing thousands of parallel development branches) fully Distributed and capable of efficient management of ultra-large scale projects similar to Linux kernel.
Git features
1.Directly record snapshots, instead of comparing differences:Git only cares about whether the overall file data changes, while most other systems only care about the specific differences in file content, such systems (CVS, Subversion, Perforce, Bazaar, etc) which files are updated each time. Git, however, does not store the data differences before and after changes. Instead, it takes snapshots of the changed files and records them in a micro file system. Each time an update is submitted, it will preview the fingerprint information of all files and take a snapshot of the file, and then save an index pointing to the snapshot. To improve performance, if the file remains unchanged, Git will not save it again, but only make a link to the last saved snapshot. Git works as follows:
Svn works as follows:
2.Almost all operations are performed locally:Most Git operations only need to access local files and resources without connecting to the network. Because Git is on a local disk
Save the historical updates of all the current projects, so the processing speed is fast. If you use CVCS, you cannot do anything without a network or disconnecting the VPN. However, if Git is used, it can be submitted frequently when there is no network, and uploaded to the remote repository when there is a network.
3.Always maintain data integrity:Before saving it to Git, all data must be calculated by the content checksum and the result is used
The unique identifier and index of the data. In other words, it is impossible for Git to know nothing after you modify a file or directory. As a Git design philosophy, this feature is built at the bottom of the overall architecture. So if the file becomes incomplete during transmission, or the file data is missing due to disk damage, Git can immediately detect it.
4.Most operations only add data:Most common Git operations only add data to the database. Any irreversible operation, such as data deletion, makes it difficult to roll back or reproduce a previous version. In other VCS, if an update is not submitted, some modifications may be lost or confused. However, in Git, once a snapshot is submitted, there is no need to worry about data loss, this is especially true for the habit of regularly pushing data to other warehouses.
Basic Git workflow
1. modify some files in the working directory
2. Take a snapshot of the modified file and save it to the temporary storage area.
3. Submit the update and permanently dump the file snapshots saved in the temporary storage area to the Git directory.
Two methods to obtain the Git repository of a project
1. Create a Git repository by importing all files in the existing Directory. Enter the project directory and execute the $ git init statement. After initialization, a directory named. git appears under the current directory. All the data and resources required by Git are stored in this directory. However, at present, all the files and directories in the project are initialized only according to the existing structure framework, but the project has not been tracked and managed. When you run the git add command to add file data in the project, Git starts to track and manage the file data.
2. clone from the existing repository of the remote server. The command format is git clone <url>, for example, $ git clonegit: // github.com/schacon/grit.git. This will create a directory named "grit" under the current directory, which contains. git directory, used to save all downloaded version records, and then extract the latest file copy. The gerit directory also contains all files in the project. Note that when the code repository is cloned from the remote server, git is on the local master branch, and there is no data on the local master branch, to obtain data (data in the remote code repository), you need to switch to the branch in the remote code repository; or, merge the branch in the remote code repository to the local master branch, the local master branch has all the file data of the project. If you want to define the Directory Name of the newly created Project during cloning, you can specify a new name after the clone command, for example, $ git clone git: // github.com/schacon/grit.git MyGrit.
Ignore some files
If you do not want some files to appear in the list of untracked files, such as automatically generated log files and temporary files created during compilation, we can not include these files in Git management. This can be done by defining a file named. gitignore, which lists the file modes to ignore. The format of file. gitignore is as follows:
● All blank lines or lines starting with '#' are ignored by Git.
● The standard glob mode can be used for matching. * The last matching mode and the backslash (/) indicate that the directory is to be ignored. * To ignore files or directories other than the specified mode, you can add an exclamation point (!) before the mode (!) .
For example:
# This is a comment-it will be ignored by Git
*. A # ignore all files ending with.
! Lib. a # Except lib.
/TODO # ignore the TODO file in the root directory of the project, excluding subdir/TODO
Build/# ignore all files in the build/directory
Doc/*. txt # will ignore doc/notes.txt, but does not include doc/server/arch.txt
Gitdiff Introduction
Git diff is used to view temporary and non-temporary updates. It not only lists which files have been modified, but also lists which updates have not been saved yet, and which updates have been saved for future submission, the added and deleted lines are also displayed in the file patch format. & Git diff compares the difference between the current file in the working directory and the snapshot in the temporary storage area, & git diff-cached or $ git diff-staged compares the differences between the files that have been saved and the snapshots that were last submitted.