Liao Xuefeng Git Tutorial Study Notes (1)

The author and his background: Zai Jiang, a third-year student, kindergarten English proficiency, familiar with the basic operation of Linux files (the most basic)

There may be some omissions in the content of the article. I hope you will forgive me. If it constitutes infringement or other problems, please contact me by email: [email protected]. If you need more complete knowledge, please move to Mr. Liao's Git tutorial

Source of learning content: Liao Xuefeng Git tutorial: https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

Thanks to teacher Liao Xuefeng for the tutorial

The text begins:

Introduction to Git

What is Git?

Git is the most advanced distributed version control system in the world (that is, a version control system =_=)

 Birth of Git

It took Linus two weeks to write Git because of BitMover.

Centralized vs Distributed

Centralized: The version library is centrally stored on the central server. When working, you need to use your own computer to obtain the latest version from the central server, and then the work can be started. After the work is completed, you need to push your own changes to the central server. .

The biggest feature of a centralized version control system is that it must be networked to work.

Distributed: There is no central server, and each computer is a complete version library. It does not need to be connected to the Internet when working. When multiple people collaborate, they only need to push their own changes to each other, and they can see each other's changes.

Distributed version control features: higher security, no need to worry too much in the event of an accident, can be copied from other computers, open source free (emphasis)

Install Git

(I only introduce Linux and Windows platforms here, and I have no contact with other platforms, so I am sorry to write)

Linux platform

$sudo apt-get install git 或 $git apt install git

Windows platform

Download the installer directly from the Git official website, or select a domestic mirror, then use the default settings and continue to next

After the installation is successful, find "Git" --> "Git Bash" in the start menu, and a window similar to the command prompt pops up, indicating that the Git installation is successful!

After the installation is complete, you need to perform the last step of setting, enter it in Bash;

$git config --global user.name"YourName"

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

Because Git is a distributed version control system, every machine must have an identity: your name and email address.

Create a repository

1. Create an empty directory

$ mkdir learngit

$ cd learngit

$pwd

2. Turn the directory into a Git manageable repository

$git init

Add files to the repository

All version control systems can only track changes in text files, Git is no exception, eg: .txt, .html, .exe, it is recommended to use UTF-8 for encoding

1. Add the file to the repository:

$git add readme.txt

2. Submit the file to the warehouse:

$git commit -m "wrote a readmefile"

Note: The content in double quotation marks ("") can be defined by yourself

time machine travel

After the readme.txt file is modified, check the current status of the warehouse:

$git status

Check out the specific modifications:

$git diff “readme.txt

$git add "readme.txt"

$git status #Check if the modified file is included

$git commit -m "add distributed"

$git status #View the current status of the repository

Note: "#" in Linux files represents comments, so here readers only need to regard "#" as "//"

version rollback

Check out the commit log:

$git log 或

$git log --pretty=oneline  #查看版本号(日志)

回退上一个版本:

$git reset --hard HEAD^

回退上100个版本:

$git reset --hard HEAD~100

查看文件内容:

$cat "readme.txt"

回退之后查看日志:

$git log  #无法发现最新的版本

#查看上面的命令行获取commit id(没被关闭才可看到)

回到最新版本:

$git reset --hard "commit id"

注:双引号("")里的内容请根据命令行显示的字符进行输入

查看文件内容:

$cat "readme.txt"

找不到新版本的commit id:

$git reflog  #查看Git所记录使用者的每一次命令

$git reset --hard "commit id"

$cat "readme.txt"

工作区与暂存区

工作区即电脑中可随意查看的目录

版本库即工作区的一个隐藏目录——.git目录

暂存区即版本库中被称为“stage”(或“index”)的文件

将文件往Git版本库添加:

$git add "readme.txt"将文件添加到暂存区;

$git commit "XXX"把暂存区所有内容提交到当前分支,即将需要提交的文件修改全部放至暂存区,一次性提交暂存区修改至分支。

管理修改

查看工作区版本库最新版本区别:

$git diff HEAD --"readme.txt"

再添加,提交

撤销修改

方法一:修改了文件未提交至暂存区:

删除修改,手动恢复至上一版本状态。

$git status  #查看

$git checkout --"readme.txt"  #丢弃工作区修改

注意:①"readme.txt"修改后未添加至暂存区,撤销修改就回到和版本库一模一样的状态。

   ②"readme.txt"已添加至暂存区后,又作了修改,撤销修改后就回到添加到暂存区后的状态。

   总而言之,就是让readme.txt回到最近一次的git commit或git add时的状态。

注:git checkout --reademe.txt中的“--”很重要,如果没有“--”,就变成了”切换到另一个分区”的命令。

方法二:已将“readme.txt”git add至暂存区:

$git status

$git reset HEAD "readme.txt"

$git status

$git checkout --"readme.txt"

方法三:已经提交修改至版本库还未推送至远程:

$git log 或

$git log --pretty=oneline

$git reset --hard HEAD^ 或

$git reset --hard HEAD~"number"  #参考版本回退

删除文件

前置背景:

$vi test.txt

$git add test.txt

$git commit -m "add test.txt"

$rm test.txt

回到现在:

$git status

两个选择:

①确认删除

$git rm test.txt

$git commit -m "remove test.txt"

②误删文件,恢复至最新版本

$git checkout --test.txt

注:由于只恢复至最新版本,使用者会丢失最近一次提交后修改的内容

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324867855&siteId=291194637