One article teaches you to understand Git tools

Table of contents

Introduction to Git

Git installation and configuration

new warehouse

Work Area and File Status

 git reset rollback version

Use git diff to compare differences

Introduction to Git

        Git is a free and open source distributed version control system. It uses a special database called a warehouse to record file changes. Each file in the warehouse has a complete version history, and you can see who modified it at what time What contents of which files are included. When needed, files can also be restored to a previous version. If there is no version control system, we can only manage files in the most original way, and manage files in the original way-back up multiple copies of files according to date , or save each version of the entire project to a different folder, and when many project members are modifying the same project at the same time, it is necessary to manually merge the contents of the two people, which is not only inefficient, but also difficult to manage It is more troublesome, and the version control system was born to solve these problems. With the version control system, we can track the changes of each file and make the collaboration between project members more efficient.

        Currently, there are two most popular version control systems on the market, one is a centralized version control system (SVN, CVS, etc.), the other is a distributed version control system (Git, Mercurial, etc.), and the centralized version control system— —All files are stored on the central server, and a copy is saved on everyone’s computer. When you need to modify a file, you must first download the latest version from the central server, and then add the modified content you want. After the modification is completed, upload it to the central server. The advantage of the centralized version control system is that it is very simple to use. You only need to download the latest version from the central server. After the modification is completed, upload it to the central server. There is no need to consider other issues, but its shortcomings are also very obvious, that is, the single point of failure of the central server. If the central server fails, or there is a problem with the network connection, then everyone will be unable to work and can only wait for the central server. Or the network returns to normal, and the distributed version control system does not have this problem. Everyone has a complete version library on their computer, so we can make changes locally without considering network problems, so that even if the central server fails , we can continue to work, and when we need to share our modified content with others, we only need to synchronize the warehouses with each other.

 

        Because of its free and open source, fast speed, powerful functions, support for offline work, and powerful branch management, Git has become the most advanced distributed version control system in the world. More than 90% of open source projects in the world are using Git for version control. Open source projects hosted on websites such as GitHub GitLab Gitee also use Git for version management. For developers, mastering Git is an essential skill.

Git installation and configuration

        First go to the git official website, Git (git-scm.com) , and then click Downloads

         win+R, enter cmd, and then enter git-v in the command box, if the version number below appears, it proves that the installation is successful.

         There are three main ways to use Git:

  • Command line: recommended, high efficiency, deep understanding
  • Graphical interface: use Git through some dedicated graphical interface
  • IDE plug-ins/extensions: In commonly used IDEs such as IDEA or VSCode, it can be used through plug-ins or extensions.

In order to distinguish it from the commands in linux, the commands used in git all start with git, followed by commands.

        Before starting to use git, the first step is to use the git config command to configure the user name and email address, so that when submitting, you can identify who submitted the content.

git config --global user.name "zhangyu"

local parameter: local configuration, only valid for local warehouse

The global parameter here: global configuration, effective for all warehouses.

system: system configuration, valid for all users. (generally not used)

Configure mailbox: if there is no space, you can not use double quotes

 git config --global user.email [email protected]

We can use the following command to save the username and password so that we don't need to enter it every time

git config --global credential.helper

We can use the following command to view the git configuration information

git config --global --list

 After the configuration is complete, we can use git to manage our code

new warehouse

        We can understand the warehouse as a directory. All files in this directory can be managed by Git. The modification, deletion, and addition of each file can be tracked by Git, so that the history can be tracked or restored at any time. to a previous version. Just turn a directory into a warehouse that Git can manage

Method 1: One is to create a warehouse locally on your computer.

git init

Method 2: Clone an existing warehouse from a remote server.

git clone

Note: The warehouse we created is a hidden one. You can’t view it with ls. You need ls -a to view it. We view many files in the warehouse. These files and directories are important parts of the Git warehouse. Do not delete and modify at will, otherwise the git repository will be destroyed. This is why this directory should be hidden.

        Use method 1 to create a warehouse locally

         Method 2 Use the git clone command to clone an existing warehouse from a remote server such as Github or Gitee.

git clone https://github.com/geekhall-l

Work Area and File Status

        Git's local data management is divided into three areas, namely the working directory, the staging area (Staging Area/Index) and the local warehouse (Local Repository). The folders we can see in the resource manager are Workspace, temporary storage area is a temporary storage area used to save the modified content that will be submitted to the Git warehouse. The temporary storage area is a very important area when Git performs version control. The local warehouse is created by the git init command That warehouse, which contains the complete project history and metadata, is the main location where Git stores code and version information. In simple terms, the workspace is the directory we actually operate, and the temporary storage area is an intermediate area for temporary Store the modified content to be submitted. The local warehouse is the main location for Git to store code and version information. After you modify the files in the workspace, you need to submit them to the temporary storage area, and then submit the changes in the temporary storage area to the local in the warehouse. During this process, we can use the commands provided by Git to view, compare or undo changes to ensure the accuracy and integrity of version control.

        Let's give a vivid example to illustrate the relationship between the three areas

        We can understand the warehouse as the warehouse in the factory. There are many goods and products in this warehouse. These goods and products are our files, such as our code files, text files, picture files, etc. The work area is to produce these The workshop of the goods produces, processes and modifies our goods. After the workshop produces these products, they need to be transported to the warehouse for storage. Then a transportation tool is needed between the workshop and the warehouse. For example, we use a small truck to transport, then this Inside the small truck is the temporary storage area. After the workshop produces the product, put the product on the small truck, and then transport it to the warehouse for storage. When it is connected to the work, when our code is completed to a certain stage and needs to be archived and backed up, You can put this version in the local warehouse and save it. In the version controller, this process of saving to the warehouse is called submission. However, if we need to submit every time the file is modified, it will be very troublesome, so Git gives We provide a way, that is, you can add the modified files to the temporary storage area first, and then execute the submission operation for all the files in the temporary storage area. That is, after the workshop produces the goods, it does not need to They are all sent to the warehouse, but put these goods on a small truck, and then transport them to the warehouse at one time for storage. The corresponding files in git also have several states, namely: untracked, unmodified, and Staging and committed.

  • git init creates a warehouse
  • git status View warehouse status
  • git add Add to the staging area
  • git commit commit

 ①Check the status of the current warehouse, for example, you can check which branch the current warehouse is in, which files are there, and what state these files are currently in. Now we create a zy.txt, the content inside is "This is my first file project", we check his status. We can see that the status of the file is untracked.

 ②We use git add to put the file in the pickup truck, which is the temporary storage area, and wait for the subsequent submission operation. And looking at his status, we can see that zy.txt has turned green, and the status has changed to committed. It means that it has been added to the temporary storage area and is waiting to be submitted.

 ③ Submit the files to the warehouse, use the command git commit, only the files in the temporary storage area will be submitted, and other files in the work area will not be submitted. For example, let's create a new file. The git commit command needs to use the -m parameter to specify the submitted letter when submitting. This information will be recorded in the warehouse. If the -m parameter is not specified, the git commit command will enter an interactive interface. By default Will use vim to edit the information, after the submission is complete, look at the status of the warehouse. You can see that the green one just now has disappeared. This is because we have submitted zy.txt to the warehouse for safekeeping. Because yyq.txt is no longer in the temporary storage area, it will not be submitted to the warehouse when it is submitted.

 ④The git add command can also use wildcards to add multiple files, git *.txt, and save the file names ending in txt to the storage area. It can be seen that all files ending in txt have been added to the praise storage area. The zy.sh file is not added to the temporary storage area because it does not end in txt. In addition to wildcards, the git add command can also accept folders as parameters. For example, if you want to add all files, you can use git add . Add all files in the current folder to the temporary storage area.

 We can see that all files are added to the staging area.

Next, we use vim's powerful text editor to submit to the warehouse, that is, git commit without the -m parameter. In the vim editor, use i to edit, use esc to exit, :wq to save and exit.

 ⑤Now there are two submissions in the warehouse, you can use the git log command to view the submission records, you can see that we have two submissions, each submission has a unique submission ID, where git log --oneline A concise commit history can be viewed. In this way, only the id and submitted information of each submission are displayed.

 git reset rollback version

        We often need to undo some previous modifications, or roll back to a previous version. At this time, the git reset command comes in handy. The reset command can return to a previous submitted state. The three steps of git reset This mode, that is, three different types of parameters can be added later, namely git reset --soft, git reset --hard, and git reset --mixed. That is, soft, hard, mixed. The soft parameter means to roll back to a certain version, and keep all the modified content in the work area and temporary storage area. The hard parameter means to roll back to a certain version, and discard all modified content in the work area and temporary storage area. mixed This parameter is between the soft and hard parameters. Roll back to a certain version, and only keep the modified content in the work area, and discard the modified content in the temporary storage area.

 First, we upload the files to the temporary storage area and warehouse three times, and the upload records are as follows:

 We copy three times respectively to execute different parameters,

① Let's look at the soft parameter. We execute git reset --soft + the ID of the rollback version. We see that there are only two submissions left in the history. Look at the content of the workspace and temporary storage area again, and use ls to check it. Use git ls-files to look at the content of the temporary storage area, you can know that it exists, and look at the status of the warehouse, it prompts us that it is a new file, because we use the soft parameter, so when we roll back to the previous version, it will work The storage area and temporary storage area will not be emptied, so the file "yyq3.txt" still exists.

 ②Using the hard parameter in the same way, we can see that there is no 'yyq3.txt' file in the temporary storage area or work area

③ Next, look at the third parameter, the mixed parameter. We can see that the submission history is only twice. Let's see that "yyq3.txt" in the workspace no longer exists, while "" in the temporary storage area.

If we accidentally make a mistake and use the hard parameter to delete both the temporary storage area and the work area, don’t worry, because all operations in git can be traced back. You can use git reflog to view historical operations and find misoperations version number, and then use git reset --hard version number to roll back to this version.

Use git diff to compare differences

        No parameters are added after gitdiff, and the difference between the temporary storage area and the work area is compared by default. He will display the changed files and the details of the changes. We can see that it has turned yellow, indicating that we have modified some content. Git will use the hash algorithm to generate a 40-bit hash value of the file content. Here only the first 7 digits of the hash value are displayed, and the latter 100644 indicates the permissions of the file. Red represents deleted content, and green is newly added.

 Next, we will modify the workspace, upload it to the temporary storage area again, and compare the differences. You can see that there is no difference.

 We can also compare the differences between the workspace and the repository, in git diff HEAD

We can also compare the differences between the staging area and the repository git diff --cached

 

We can also compare the differences of different version IDs using git diff version 1 version 2

 

HEAD means the current version, HEAD~ means the previous version, HEAD~2 means the two versions before HEAD

git diff HEAD~3 HEAD yyq3.txt, indicating the difference between the current version of yyq3.txt and the first three versions

To be continued~~~

 

Guess you like

Origin blog.csdn.net/zywcxz/article/details/132044047