[Linux] Two stories take you to use the git command line

GitHub and gitee are code hosting platforms, where we upload code or files to manage our code and different versions of software.

They are indispensable tools in multi-person collaborative development. Do you know git?

The underlying logic of github and gitee comes from git, which plays an important role in it. The following is part of my understanding of git, and I hope it can help you.


Blog content:

Next, we will explain the source and function of git through two short stories. We will briefly introduce a basic function version management of git using the website gitee. Finally, we will briefly introduce how to use git in the Linux command line.

This blog mainly focuses on understanding version management and using git to submit files on Linux.

1. Historical stories

background

​ The father of Linux, Linus Benedict Torvalds (hereinafter unified: Torvalds), completed version 0.11, which is the first version of the Linux kernel, in 1991. He wanted to better develop LInux This operating system is absolutely impossible to rely on Torvalds himself. After all, in the face of millions of lines of code and different types of functions (drivers, disks, file systems, management, IO, etc.), the power of one person appears negligible.

So Torvalds made it open source, and let engineers and hackers all over the world see its value, making them participate in joint development. Engineers all over the world complete their own modules that they are good at, so what should they do with the code they write? Many engineers directly sent the code to Torvalds to copy and paste it to the appropriate location, but after all, there are too many people. If you only do this Torvalds will spend every day in copying and pasting, which is obviously not possible .

​ Regarding this problem, how should Torvalds solve it? Find someone to replace it? Liberate his labor? This is obviously not advisable. Since these codes can be integrated, it means that their own capabilities are very good. It is a bit wasteful to waste on copying and pasting, and it will also increase the cost. When engineers all over the world are using love to generate electricity for open source Linux, there is really no need to increase this overhead. Smart people all over the world cannot be stumped by this problem. They think that there is a need for a code management software, and the engineers upload the code themselves to solve this problem.

go through

​ At first, Torvalds didn’t think about creating such software by himself, but looking for the existing software jointly developed by multiple people . Fortunately, he found it (this is the idea of ​​​​the great god, if you encounter problems, go directly to find out whether Someone solved the problem, and he used it directly), but unfortunately, this software is charged. After everyone's discussion, he decided to give up using this software. The external statement is " incompatible with the spirit of open source " (Personally think: Things that cost money sometimes hinder its own promotion to a certain extent, affecting its influence and transmission speed. We usually watch something, such as a video or an article, when we think it is very good, we will To consider whether to spend money to buy the content after that, and feel a little bit unhappy when paying for it).

​ Fortunately, Torvalds finally found the software company. Their boss was very generous and respected them. He told him that he thinks the Linux community is a community that changes the world and respects them very much. Others need to spend money to use this software. Give them free access to the community. In this way, the Linux community uses this software to manage their code, and has achieved a good response.

the birth of git

​ If the story ends here, there will be no git later , and the development of things is sometimes full of drama. Most of the top engineers have a common problem, that is curiosity , they are curious about how the software is made and how it works, so many people in the Linux community began to crack the software. Until one day, an engineer with almost the same example cracked part of the content and released it to the world to announce his results, which made the boss of the software company very annoyed. He respected them and gave them free use, and in turn asked Poaching his corner, decided to terminate the free access to the community. In this way, Torvalds returned to the pre-liberation overnight, and of course he couldn't copy and paste the code, it is difficult to change from extravagance to frugality! So he spent two to three weeks to develop a version controller software , and open source it, named it git , which attracted many engineers who are interested in version control. With continuous development, we have formed what we are today. See git .

2.git version management

The function of git is not limited to version management, but also has richer branch management, rollback, project labeling, etc., which leads to the use of git multi-person collaboration within the company .

  • Here we only introduce the version management function of git

1. A short story

Next, we use a short story to briefly introduce version management :

In school, we may face the situation that the teacher asks for an experiment report. If there is a particularly responsible teacher in our school who teaches us Linux, after an experiment, let the whole class submit a related experiment report to him.

One of my roommates, Xiao A, finished the first one. He took the electronic document and went directly to the teacher to submit the report. After reading it, the teacher said that there is something wrong with you. to modify. After going to the teacher for the second time, he still said no, he needs to continue to revise. It's been a few days, and you just hand in the first report. Xiao A is just stupid. The experiment report has already changed him from what it was before, and he has no backup. He can only go back alone and feel uncomfortable.

I am a smart person, I watched the whole thing from the sidelines, and knew the teacher's temper, so I kept 10,000 minds, and kept a copy of the experiment report every time the teacher wanted to revise, and when I handed in the report for the last time, he He said the same thing to me and asked me to submit the second report directly. I found the report on the computer and submitted it without any burden.
insert image description here

In the computer world, I am a hard-working programmer . My operation is version management , and my teacher is called a product manager .

Any software has bugs, and we need to update it, which forms different versions. We generally use git to manage these different versions.

Let's continue our story. I finished the teacher's lab report by myself, but the other students in the class were not so lucky. I was enthusiastic and decided to help them manage their lab reports and let them write every revision All the reports are sent to me, and I will manage them. Those who need them are asking for me. By managing these large amounts of experimental reports, I have gradually explored a set of unique methods to manage them better. As my reputation gradually grows, Students from other colleges also had lab reports to manage, and they approached me one after another.

Faced with so many experimental reports, I am too busy to use my method to manage. At this time, I gave full play to the advantages of computer science. I wrote a network-based software, which is divided into client and service end , the server is deployed on its own cloud server, and the client is downloaded on its own computer, replacing my manual work. From then on, you don’t need to find me in the management report, just download the client and upload it directly. We call this version It is 1.0 . As more and more people use it, I think this software has a huge market and many people need to use it. I named it git and promoted it to the whole country.

After promotion, my software has become a well-known software in the country, but all its operations are based on black screens and command lines , which is really unsightly. Next I recruit and modify it to make it a website . After uploading, many operations can be implemented directly on the website, and then a new client is developed, and uploading can be submitted on this client (this article will not talk about this content), which is more convenient for people to operate. We call this version 2.0 .

Everyone should know the next story, version 2.0 is GitHub and gitee that we are familiar with . The bottom layer of these two websites is implemented based on git .

2. Understand version management

Through the above short story, we use gitee to better understand version management.

We use gitee to manage files, which can be divided into two steps:

  1. Complete the writing of code or files on the local computer
  2. Upload to the corresponding warehouse through the software, and the system will automatically manage

Every upload will be recorded, even if the same file is modified, as long as it is uploaded, we can find the modified files in different periods.

Combined with the short story, we only need to make changes in an experiment report every time, and when uploading, gitee will automatically save different versions of the same file uploaded for us each time.

As shown in the picture below, I uploaded three times after modifying a file locally, and each uploaded file can be found
insert image description here

  • The version number in the above picture is automatically generated after uploading, and a version number will be generated for each upload

Based on this principle, the update and management of each software version is easy to understand. Upload the modified software code and label it with a new version number. Different versions of the code can be obtained through different labels.

As shown in the figure below , we randomly find an open source project to observe
insert image description here

  • The version number on the left above is the label, the upload is set by yourself, representing the corresponding version, and the right is automatically generated when uploading.

Use this website to manage our experiment reports. After each modification, upload to the website, and the teacher just needs to extract which one and send it to it.

Three. The use of git

1. Warehouse creation

First, we create a warehouse on our own gitee or github. I use gitee as follows:
insert image description here
After clicking the new warehouse, the following page will appear

insert image description here
The following warehouses are obtained, in which the warehouses are open-sourced, deleted warehouses, and setting managers are all under management, which are not the main content of this article, so I won’t briefly introduce them here.

insert image description here
We have created the warehouse, and we need the link of the warehouse to use git on Linux, and clone the warehouse to the local, as follows

insert image description here

After copying, the operation on gitee is completed

2. Install git and warehouse clone

First use to git --versioncheck whether git is installed on Linux.
insert image description here
If the similar display above appears, it is installed. If it is not installed, use the following command to install it.

sudo yum install -y git

After downloading, use the following command to clone the warehouse

git clone 复制的仓库链接

insert image description here
Because our warehouse is private, we need to enter the user name and password. After the warehouse is open source, there is no need to enter it.

After cloning, the warehouse can be displayed locally, and the folder in the picture below is the warehouse

insert image description here
It can be seen that the files in the warehouse we created above are all in this directory.

The extra .gitdirectory is actually the local warehouse, which stores each uploaded record in binary form

Let's look at .gitthe catalog as follows

Note: Don't change the .git file, don't care about it

3. Upload code three tricks

add

First find a file and copy it into the warehouse

insert image description here

At this point, the file is not said to be added to the repository, it is just placed under this file.

Then use the following command to make the file managed by the warehouse and store it in the temporary storage area of ​​the warehouse

git add .

After the command is executed, you can use the following command to check whether it is stored in the temporary storage area

git status

insert image description here
Display new file followed by the file name, indicating that this file has been stored in

commit

Next, we will add all unadded files in the current directory to the local warehouse.git

Add the file using the following command

git commit -m "日志"

In the double quotes, you need to fill in an overview of the uploaded file, which needs to be certified. The clearer the better, every submission will be recorded, and you can git log指令find all the submission logs. If you leave the company, the next programmer will receive it. For your work, I checked your code and found that I couldn’t understand it. I looked at the information when you submitted it, what exactly did you do, and the result was that I couldn’t understand it. You will be scolded, so if you don’t write this well, you will be called by the boss to talk. .

insert image description here
When submitting files to our local .gitstorage, one is worried about problems with local storage, and there is an extra layer of protection in the past, and the other is to facilitate regular remote storage. In the company, let the leaders see the progress of the project in front of them, which is convenient for integration.

If we develop alone, we can go here, but now we don't care about him, our purpose is to submit the files to the local and push to the remote, so that the local and remote warehouses remain consistent.

Note: The following problems will occur in the first submission

insert image description here
We need to set up our own email and user name, so that others can check and contact us in time.

Use the two instructions in the red box in the above figure, just copy and modify the information in the double quotation marks behind

insert image description here

Add and commit are the operations of temporarily storing files in the local library, and the following is to push the files of the local library to the remote end

push

Finally, use the following command for remote push

git push

Generally, you need to enter your username and password when submitting

insert image description here
The structure shown in the above figure shows that the push is successful.

You may also encounter the following warnings when pushing:

insert image description here
This warning needs to modify the global variable, just execute the following command

git config --global push.default matching
git config --global push.default simple

After completing these three tricks, let's see if the submission is successful in gitee

insert image description here

Summarize:

  1. git add . : Submit the content to the temporary storage area
  2. git commit -m "log": submit the content to the local warehouse
  3. git push : push the content of the local warehouse to the remote end, and push it to the remote end

Guess you like

Origin blog.csdn.net/m0_52094687/article/details/128735115