The principles and use of Git (1): Basic operations of Git (including: version rollback)

1. First introduction and installation of Git

1.What is Git

Git is a version controller
Insert image description here
Here we focus on the use of Git under the Linux operating system
Because in the future development process, Linux The operating system is used more frequently
and Git was originally developed under the Linux operating system

2. How to install Git

1.git command and git help ("man manual" under Git)

First we can use the git command to check whether we have Git installed.

git
如果结果中有:
command not found:说明我们还没有安装Git

If the result is like this
Insert image description here
it means we have already installed Git
Here we can use it

git help Git下的具体命令/概念

To view the usage documentation of the corresponding command/concept
Insert image description here
Similar to the man manual, press the q key to exit

2. Install Git under centos

安装git命令:
sudo yum install -y git
查看git安装的版本:
git --version

Insert image description here
Insert image description here

3. Install Git under ubantu

安装git命令:
sudo apt-get install -y git
查看git安装的版本:
git --version

2. Pre-operation and pre-knowledge of Git

If we want to version control files
we must first create a warehouse

Warehouse: a file directory for version control

1. Create a Git local repository

Insert image description here
We are currently in the gitblog directory, and we want to create a local git warehouse in this directory
Use the command:

git init

Insert image description here
This .git directory is used by Git to track and manage the warehouse. Do not manually modify the files in this directory. If the files are messed up, the Git warehouse will be destroyed.

There are a lot of details about the Git repository. If you are interested, you can take a look.

tree .git

Insert image description here
We will introduce some of the attributes here later.

In order to facilitate understanding in future operations
we will often look at this Git repository like this

2. Configure Git

The first thing we need to do after installing Git is
Set the user name and email address
How to configure it?

git config user.name "用户名称"
git config user.email "email地址"

还有一个选项:--global:
git config --global user.name "用户名称"
git config --global user.email "email地址"

这个--global的意思就是:如果加上这个--global
那么就表示这台机器上的所有的Git仓库都会使用这个配置.

如果你希望在不同仓库中使用不同的用户名或email地址
那么可以不加这个选项

View configuration:

git config -l

Delete configuration:

git config --unset user.name
git config --unset user.email

git config --global --unset user.name
git config --global --unset user.email

Insert image description here
Next I am going to configure a username and email
This time I configure one without the –global option
Insert image description here
Insert image description here
How to configure and how to view the configuration , I have introduced you how to delete the configuration
You can configure it yourself

3. Understand Git partitions

1. Workspace

The workspace is the directory where we write code or files
which is where we are currently

/home/wzs/wzsblog/gitblog

Under this path (that is, the directory containing .git)
Note:
Even .git is indeed under the directory gitblog a>
But .git does not belong to the workspace!!!

Usually we write code in the workspace (this is why it is called a workspace)
After writing, if you want to hand over the file to Git for management< /span> command to add the file to the temporary storage area
You need to execute the git add

2. Temporary storage area

The temporary storage area can also be called the index area
It is usually stored in the index file in the .git directory

is called the staging area because only after executing the git commit command can the files in the staging area be added to the repository and Git can manage this. File!!

It is called the index area because the temporary storage area actually stores not the file itself, but the index of the file.

We will introduce this in detail later, but you can just know this for now.

When we execute thegit add command on the modified or newly added files in the workspace
, the file index in the temporary storage area will be updated

3. Version library

The repository is actually this hidden file.git
All files in this repository will be managed by Git
Every file modification, Deletions will be tracked and recorded by Git
so that we can track history or "restore" (that is, version rollback, which we will introduce later) at any time

4. Summary of partition relationships

Insert image description here
We will introduce the git add and git commit commands below.

3. Add files

Below we use a scenario to introduce the use of git add and git commit.

Scenario:
We want to create a test.txt file in the workspace
and then transfer this file to git add and git commit Git management
This is a test.txt file I created
Insert image description here
For convenience, echo and redirection are used to create a new test.txt and write < /span>
hello git
Insert image description here

1.git add

Insert image description here
Let’s choose the first one to demonstrate.
If you are interested, you can also try the others

Here is a command:

git status
这个命令可以查看当前git仓库的状态
他会提示我们接下来的操作

Insert image description here
It prompts us to perform the git commit operation next

2.git commit

Insert image description here
Let's demonstrate it below
Insert image description here

3. git log to view historical commit records

So far, we have successfully submitted the test.txt file to the local warehouse and handed it over to Git for management.
However, as we write more and more code, the files we need to maintain also increase. More and more
we will inevitably forget a lot of information

So git log comes in handy

git log

It can be used to view historical submission records
Insert image description here
But what happens when we submit more files?
For example, I created and submitted another 3 files
Insert image description here
then git log
Insert image description here

Note: The git log command is the actual commit log from the latest to the farthest

This is just 2 submissions. What if we have to make dozens or hundreds of submissions in the future?
Then git log is not "understanding" "Okay...

4.git log --pretty=oneline

Is there an easy way?

git log --pretty=oneline

Insert image description here
At this time, only the commit id and the submission information we wrote will be displayed
and each submission information only occupies a row

4. Preliminary understanding of the .git directory

Currently we have added files to the local warehouse
Let’s go there again

tree .git

Look at the directory structure of .git
Let’s get a preliminary understanding of what is in .git

1. Preliminary introduction

Insert image description here

2.HEAD and master branches

Let’s take a look below
What does HEAD store? What does master store?
Insert image description here

3.object and commit id

Let’s take a look at what this object stores
Insert image description here
Let’s check this latest commit
The commit id is: 76ba3c583103ad526fb5cd5281a539eac782dc49

git cat-file 76ba3c583103ad526fb5cd5281a539eac782dc49

Insert image description here
Here we only introduce the -p option
If you are interested in other options, you can try them
Insert image description here
Insert image description here
At this point, you only need to know:

tree的commit id 保存的是当前提交和当前提交之前的所有提交的commit id

parent的commit id保存的是上一次提交时产生的commit id

其中,我们git log显示的都是parent的commit id
如果我们想要查看具体某个文件的修改
先查看git log显示的那一次提交的commit id的内容
然后tree 
然后就能看到具体的文件的commit id了
然后就可以使用具体文件的commit id来查看对应文件的修改了

4. Summary

Insert image description here
Later in the learning process
it is best for everyone to match common git operations with changes in the structure and content of the .git directory
This helps us understand the detailed process of git

5. git diff to view changes

We can see that the above method of viewing modifications is really troublesome
Isn’t there a better way for us to view modifications?< a i=2> Of course

First, let’s modify test.txt
Insert image description here
We use append redirection to add two new lines of content
At this time, the local warehouse The test.txt file in is different from the test.txt file in our workspace
We can also use it

git status

To see if the file has been modified again after our last submission
Insert image description here
Insert image description here
Insert image description here
At this time, because we have neither add, let alone commit
Therefore, the contents in the current staging area and the version library (local warehouse) are the same
So at this time
the results displayed by git diff and git diff HEAD are the same
Insert image description here
Next, we git add
to make the contents of the workspace and the staging area consistent.
Insert image description here
At this time, git diff is the same as git diff The results of HEAD are inconsistent
Then I git commit
Insert image description here
Insert image description here
At this time, the files in the workspace are no different from the files in the staging area and the repository.

6. Version rollback

We have also mentioned before that Git can manage historical versions of files, which is also an important capability of version controllers
If one day you find that the previous code appears A very big problem requires starting over from a specific historical version.
At this time, the version rollback function is needed

1.git reset

Insert image description here
Regarding the specific three options
–soft --mixed --hard, you can see this table
Insert image description here
Next we modify test1.txt This file
and perform add and commit operations to update the contents of the staging area and version library to
test1
Insert image description here
Insert image description here
preparation The work is done

We stipulate:
All initial states of these three options are the current state (the initial versions are all current versions)
must be rolled back The content in test1.txt is the version of test1.txt
Insert image description here
which is this version

2.Demonstration

1.–soft

Insert image description here
Next, let’s use git diff to see if only the repository has been rolled back
Insert image description here
It can be seen that –soft will indeed only roll back the repository
Without rolling back the staging area and work area
Then we commit and adjust to the initial state we specified
Insert image description here

2.–mixed

Next we use –mixed to roll back
Insert image description here
Then we add commit and adjust it to the initial state we specified
Insert image description here

3.–hard

Next we use –hard to roll back
Insert image description here

4.The essence of version rollback

Insert image description here

5. Regret medicine in version rollback

But what if I accidentally use –hard and regret it later?
In real life, there is often no regret medicine
But git Gave us a chance
we could use

git reflog
这个命令是用来记录本地的每一次命令的
我们可以找到我们进行版本回退前的那个版本
然后我们就可以再次使用git reset回退到那个版本
就相当于吃了后悔药了

After the hard demonstration just now, I regretted it
I want to take the regret medicine
Let me demonstrate it to you and prove the version rollback. The essence
Insert image description here
At this time, the version stored in the master is indeed the version we have previously specified to fall back to.
Next we use git reflog
Insert image description here
It can be seen that Linux is not only an operating system, it is also a philosophy and an art
Insert image description here
At this time, the rollback is successful
The regret medicine has taken effect This also confirms the essence of version rollback
Insert image description here
And the version stored in the master branch has also changed at this time

7. Undo modifications

If we have been writing code in our workspace for a long time, and the more we write, the less we can continue. We feel that what we have written is really rubbish and want to restore it to the previous version
What should we do at this time?
Insert image description here
Below we will introduce three situations.
This time we will operate test2.txt
Modify the content of test2.txt first
Insert image description here

1.Not added

Direct execution

git checkout -- test2.txt

Just discard the modifications in the workspace
Insert image description here
Successfully undo the previous modifications
Then we restore it to the way it was before and perform add to prepare for the next step
Insert image description here

2. Already added, not yet committed

At this point we will roll back the version of the staging area first
Use the –mixed option

git reset --mixed HEAD

Insert image description here
After rolling back the staging area through version rollback, we only need to roll back the workspace.

git checkout -- test2.txt

Insert image description here
Rollback successful
Then continue to prepare for the next step
Insert image description here

3. Already committed, but not pushed yet

The following will explain what push is.
We will use it at this time

git reset --hard HEAD^
这里为什么是HEAD^呢
因为我们commit之后把版本库的HEAD指针更新了
所以要回退到上一次HEAD指向的版本

Insert image description here
In fact, the situation is the same as direct version rollback.

4.Already pushed

Note: push can only be done after commit
Insert image description here

8. Delete files

Note that in Git not only creating a new file is a modification operation
deleting a file is also a modification operation
Next we want to delete test2.txt

1. Can I rm directly?

Let’s demonstrate:
Insert image description here

2. Accidental deletion

git checkout -- test2.txt

Insert image description here
Successful recovery

3. Really want to delete

git rm test2.txt
git commit -m "提交信息"

Insert image description here
Insert image description here
Successfully deleted

The above is the entire content of the principles and uses of Git (1): basic operations of Git and version rollback. I hope it can be helpful to everyone!

Guess you like

Origin blog.csdn.net/Wzs040810/article/details/134567705