Git installation methods for different systems and Git initial configuration. Let’s start today’s lecture.

1 About version control

Git is the most popular version control system out there, and there isn’t even a word for it. When using Git, have you ever wondered what a version control system is and why we need it?

As the name implies, a version control system is essentially a system in which changes to one or a series of files are recorded in chronological order and the previous system is viewed.

Using a version control system in project development has the following benefits:

  • Collaboration: Using a version control system allows efficient sharing of files among development teams, enabling efficient consolidation of changes made by any team member to any file at any time, on any device, resulting in a unified version
  • Restore the previous version: it is easy to revert to the last tested version of the system using the version control system
  • The backup: With a distributed version control system, each member of the team can have a local copy of the complete project ifGitServer corruption is available from team member’s local repository
  • Document what happened: Using version control, when committing any changes to a team project, provide a brief description of the changes so that the relationship between releases can be documented

The development of version control system has gone through local version control system, centralized version control system and distributed version control system. This article does not introduce the detailed process of each stage. Interested friends can check online information or read the book. Here are a few things to know:

  1. Each stage has its own classic version control system,RCSIs a common tool for local version control systems,SVNIs a common tool for centralized version control systems,GitIs a common tool for distributed version control systems
  2. Distributed version control systems are the most popular version control systems, but in some cases, local version control systems and centralized version control systems are also used, such asMac OS XThe system, whenever you install developer tools, will include onercsThe command

A brief history of Git

Git was born with the development of Linux and is the second great work of Linux. The story of Linux and Git goes like this:

Two well-known tools for centralized version control systems, CVS and SVN, were created in 1985 and 2000 respectively, and Linux was released in 1991. As we all know, the Linux kernel is a very large open source software project. According to our idea, managing such a large project requires a version control system, but the father of the Linux system is “not interested” in CVS and SVN, and opposes the use of these systems in Linux projects. Between 1991 and 2002, their team members passed patches and archives to maintain the code.

As the number of contributors to the Linux open source project increased, Linux was pressured to choose a version control system, but not the well-known SVN of centralized version control systems, but a commercial version control system, which was not centralized but distributed version control system. It took them about three years to use this system. Why is that? Because the Linux contributors are so powerful.

In 2005, one of the big names in the Linux project decided to decompilate the commercial version control system they were using to develop a similar system, but the company that developed the commercial system learned of this and demanded that it withdraw its free license to the Linux community. As a result, the Linux community chose to develop a distributed version control system tool, learning from its previous use of version control systems in the process, resulting in the most popular Git in existence. Since Git was born in 2005, many open source projects have migrated to Git from SVN or other version control systems.

Git was developed on Linux, but it can be deployed on all major operating systems, including Linux, Windows, and Mac OS X.

3 the Git basis

3.1 Git workspace, staging area, and repository

Git workspace, staging, and repository concepts are as follows:

  • Workspace: A directory of folders visible on the computer that corresponds to a version of the project being checked out
  • Temporary storage: usually refers to.gitOne in the directoryindexFile, so the staging area is also called an index, which stores relevant information for the next commit
  • Version libraries: Local version libraries generally refer to workspaces.gitDirectory, which holds the project’s metadata and object database, isGitThe most important part of

The corresponding states of a file in these three areas are: Modified, temporary, and committed. Modified indicates that the file has been modified in the workspace, temporary indicates that the modified file has been identified and added to the staging area, and committed indicates that the modified file has been fully saved to local data.

3.2 Snapshots, not differences

A snapshot is a snapshot of a system or application at a certain point in time. Git uses snapshots to manage data.

Most version control systems other than Git store information as a list of files, treat the information as a set of files, and record changes made to those files over time.

Git treats data as a set of snapshots of the system, and each time a change is committed, a snapshot of the current state of all the files is taken and the references that performed the latest snapshot are updated. Each snapshot contains complete data. Before creating a new snapshot, Git will read all the data in the workspace, save it, and then process it. After comparing the snapshot with the last snapshot, the system deletes the unchanged file data from the redundant file data in the prestorage data to retain the Pointers to this part in the previous version, while the modified file data is retained. This process greatly improves efficiency.

3.3 All operations are performed locally

Unlike centralized version control systems, The vast majority of Git operations are performed on the developer’s PC, and it is generally not required to pull information from other computers on the network, which is unusually fast.

3.4 Git Integrity

Git checksums all data before storage, and then references the data with checksums. This rule is extremely important. Because, based on this rule, it is not possible to change the contents of a file or directory without Git’s knowledge, allowing Git to detect lost information or corrupted files during data transfer.

Git uses a checksum mechanism called SHA-1 hash, which results in a string of 40 hexadecimal digits, calculated based on the contents of the file or Git directory structure. Git does not store messages in an object repository through file names, but through hashes of information.

3.5 Git does not introduce secondary directories

Many version control systems, such as CVS and SVN, create directories or files under each directory in the workspace. These directories can be dangerous if exposed to the server because they expose a list of files under the directory, invalidating the server’s configuration to prevent directory browsing. In addition, original copies of files are stored in the secondary directory of the SVN in each directory, which increases the searching time of files. Git doesn’t have this problem. Git does not create secondary directories or files in every directory in the workspace, except for the.git (local version library) created in the workspace root.

4 Git Installation Method

4.1 Installing Git in Linux

There are two ways to install Git in Linux: one is to install the compiled Git software package through the Linux package manager; the other is to download the Git source code and compile and install Git based on it.

4.1.1 Installing Git using the Linux Package Manager

Installing Git through the Linux package manager is the easiest way to do it, and it is automatically configured for things like command auto-populate, but there is a disadvantage that Git installed this way may not be up to date.

On some older Linux systems, this installation is a matter of caution. On these older Linux systems, the Git installation package name was not Git but Git-core, because the name Git was taken up by the GNU interactive tools. However, in newer versions of Linux, the GNU Interactive tool gives up the name, and you can install Git using the package name git. However, common Linux systems, such as Centos 7.6, Ubuntu 18.06 and Ubuntu 20.04, do not need to consider this problem.

  • inUbuntu 10.10And later versions,Debian(squeeze)And higher versions:
sudo apt install git-all
Copy the code

Git help -w

git help -w

git help


  • inUbuntu 10.04And previous versions,Debian(lenny)And previous versions:
sudo apt install git-core
sudo apt install git-doc git-svn git-emial git-gui gitk
Copy the code

Git-core is the required installation package and the others are optional.

  • inRHEL,FedoraandCentOSIn:
yum install git
yum install git-svn git-doc git-mail git-gui gitk
Copy the code

Git packages distributed on these systems are called Git

4.1.2 Installation from Git source code

Visit the official website of Git and download the Git source code, for example, git-2.30.0tar. gz. The installation process is as follows:

  1. Unzip the source code package and go to the appropriate directory
The tar - XZVF git - 2.30.0. Tar. GzcdGit - 2.30.0. Tar. GzCopy the code
  1. Refer to the decompressionINSTALLFiles installed
make prefix=/usr/local all
sudo make prefix=/usr/local install
Copy the code

/usr/local Git installation directory. After the installation, you can find the git command in the corresponding installation directory

  1. Refer to other resources to install optional software, such as:git-doc

4.2 Installing Git on a Windows Vm

There are several ways to install Git on Windows. Here are two ways:

  1. inGit official websiteDownload the installation packageGit for WindowsIt is important to note that this project is independentGitthe
  2. The installationWindowsVersion of theGitHubSoftware, after installation, will automatically provide the command line versionGit, as well asGUI

4.3 Installing Git on a Mac

There are several ways to install Git on a Mac. Here are two ways:

  1. forMavericks10.9 and later, when first executed on a terminalgitCommand, the system automatically checks whether the installation is completeGitIf not, it will be installed automatically
  2. Download the binary installation program from the official website of Git

5 Git initialization

You’ve learned what Git is and have it installed on your computer. Before using Git, you need to configure Git only once.

Git config provides Git config tools to get and set configuration variables. These Settings will exist:

  • System files (e.g./etc/gitconfig), this file contains the values of all users of the system and their repositories, which can be passedgit config --systemOption sets configuration variables for the system
  • Global file (in user’s home directory.gitconfigFile), which contains the configuration variables set by the current system usergit config --globalOption to set global configuration variables
  • Warehouse home directory file (.git/configFile) for a single repository

The configuration file in the repository has the highest priority, followed by the global configuration file, and finally the system and configuration file. That is, if the same configuration is used in all three files, the configuration file in the repository will be used.

5.1 Configuring the User Name and Email Address

You can run the following command to configure the user name and email address:

git config [--system][--global] user.name <your_user_name>
git config [--system][--global] user.emial <your_emial>
Copy the code

You can use these commands to tell Git the name and email address of the current user, which will be used when submitting information. If you use the –system option, you need to have administrator rights

5.2 Configuring the Git Editor and Colors

You can use the following command (using emacs as an example) to configure the editor that Git commands invoke. If you do not have an editor, you will use the system’s default editor, such as vim

git config [--system][--global] core.editor emacs
Copy the code

Or on Windows (Notepad++ as an example) :

Git config [--system][--global] <notepad++. Exe > -multiinstCopy the code

Use the following command to enable the color display:

git config [--system][--global] color.ui true
Copy the code

5.3 Viewing Personal Settings

You can use the command git config –list to see all the configurations currently available to Git.

6 Check Git help

You can run git help

, git

help, or man git

to view the help information of a specified command