Home >Operation and Maintenance >Linux Operation and Maintenance >What is the command to install git on linux

What is the command to install git on linux

WBOY
WBOYOriginal
2022-06-30 17:09:064575browse

In Linux, the command to install git is "yum install git"; yum is the abbreviation of "Yellow dog Updater Modified". yum is used to automatically upgrade, install, remove rpm packages, and collect rpm packages. Related information, checks dependencies and automatically prompts users to resolve, so yum can be used to install git.

What is the command to install git on linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What is the command to install git on Linux

Git official website

https://git-scm.com/

Install Git

[root@git ~]# yum install git						#git的安装,直接使用yum安装

View version

[root@git ~]# git --version							#查看git的版本git version 1.8.3.1

Expand knowledge:

git personal identity setting

Git is a distributed version control system, so every developer should install Git on his or her laptop and then set his or her personal identity information on git.

[root@git ~]# git config --global user.name "lisi"						#设置用户姓名[root@git ~]# git config --global user.emain "456789123@qq.com"			#设置邮箱[root@git ~]# git config --global color.ui true							#enable ui颜色[root@git ~]# git config --list											#查看配置user.name=lisi
user.emain=456789123@qq.com
color.ui=true[root@git ~]#

The three core frameworks of Git

Working Directory: It is the directory where developers usually store project code;
Staging area (Stage ): Used to temporarily store changes to developer code. In fact, it is just a file that saves the file list information to be submitted;
Git warehouse (Repository): It is a safe place to store data. The git warehouse has submitted by developers. Data codes for all versions, where HEAD points to the latest version put into the warehouse.
What is the command to install git on linux
The workflow of Git is generally:
1. Add and modify code files in the working directory;
2. Put the code files that need version management into the temporary storage area;
3. Submit the files in the temporary storage area to the Git warehouse.

Therefore, files managed by Git have three states: modified, staged and committed, which correspond to each of the above processes in turn.

git command practice

1. Create and initialize a project directory, and store the editing code in this directory;

[root@git ~]# mkdir Dosier_Project									#创建一个项目代码目录
[root@git ~]# cd Dosier_Project/									#进入目录
[root@git Dosier_Project]# git init									#初始化目录,让目录受到git版本控制
Initialized empty Git repository in /root/Dosier_Project/.git/		#初始化了一个空的git仓库
[root@git Dosier_Project]# ll -al									#查看该目录
total 0
drwxr-xr-x  3 root root  18 Jan 31 16:15 .
dr-xr-x---. 5 root root 215 Jan 31 16:14 ..
drwxr-xr-x  7 root root 119 Jan 31 16:15 .git						
#发现git init初始化命令就是生成了.git目录,这个.git目录就是git仓库,以后在Dosier_Project目录下做的所有操作就可以受到git版本控制了
[root@git Dosier_Project]#

2. Simulate development engineers to develop code files

[root@git Dosier_Project]# cd /root/Dosier_Project/				#进去到项目目录
[root@git Dosier_Project]# vim dossier.sh 						#创建一个代码文件
[root@git Dosier_Project]#

3. git add submits the code files to the temporary storage area

[root@git Dosier_Project]# git status								#查看状态
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       dossier.sh
nothing added to commit but untracked files present (use "git add" to track)
#解释:Untracked files 说明存在未跟踪的文件(下边红色的那个)
#所谓的“未跟踪”文件,是指那些新添加的并且未被加入到暂存区域或提交的文件。它们处于一个逍遥法外的状态,当你一旦将它们加入暂存区域或提交到 Git 仓库,它们就开始受到 Git 的“跟踪”了
[root@git Dosier_Project]# git add dossier.sh 					#用git add命令把文件提交暂存区
[root@git Dosier_Project]# git status         					#再次查看状态
# On branch master
#
# Initial commit
#
# Changes to be committed:											#意思是要提交的更改,等待提交
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   dossier.sh										#暂存区已经有一个文件了
#
[root@git Dosier_Project]#</file></file>

4. git commit submits all the files in the temporary storage area to the git warehouse
git commit is to transfer the temporary storage area Submitting all files to the git repository does not mean submitting a certain file.

[root@git Dosier_Project]# git commit -m "lisi add dossier.sh file"		#提交暂存区内的文件到git仓库,-m参数表示写的注释
[master (root-commit) c2b3806] lisi add dossier.sh file
 1 file changed, 2 insertions(+)
 create mode 100644 dossier.sh
[root@git Dosier_Project]# git status				#再次查看状态,提示暂存区没有要commit的,工作目录文件也没有编辑过
# On branch master
nothing to commit, working directory clean
[root@git Dosier_Project]#

5. Continue to simulate version 2 and version 3

#模拟版本2,模拟版本2为修改dossier_1.sh文件,新加一个dossier_2.sh文件
[root@git ~]# cd ~/Dosier_Project/
[root@git Dosier_Project]# echo "I am good dossier_1" >> dossier_1.sh 		#修改了dossier_1.sh文件
[root@git Dosier_Project]# touch dossier_2.sh								#新加dossier_2.sh文件
[root@git Dosier_Project]# echo "I am dossier_2" >> dossier_2.sh
[root@git Dosier_Project]# git add *										#星号匹配所有(等价于--all),把所有文件提交暂存区
[root@git Dosier_Project]# git commit -m "modified dossier_1.sh,add dossier_2.sh"	#git commit提交暂存区文件到仓库
[master beff2ea] modified dossier_1.sh,add dossier_2.sh
 2 files changed, 2 insertions(+)
 create mode 100644 dossier_2.sh
[root@git Dosier_Project]# git status										#查看状态,三大区域已经一致
# On branch master
nothing to commit, working directory clean

#继续模拟版本3,继续模拟版本3为新加一个dossier_3.sh文件
[root@git Dosier_Project]# touch dossier_3.sh								#新加一个dossier_3.sh文件
[root@git Dosier_Project]# echo "I am dossier_3">> dossier_3.sh
[root@git Dosier_Project]# git add dossier_3.sh 							#把dossier_3.sh提交到暂存区
[root@git Dosier_Project]# git commit -m "Add dossier_3.sh"					#把暂存区内容提交到git仓库
[master 72cd563] Add dossier_3.sh
 1 file changed, 1 insertion(+)
 create mode 100644 dossier_3.sh
[root@git Dosier_Project]# git status										#查看状态,三大区域已经一致
# On branch master
nothing to commit, working directory clean
[root@git Dosier_Project]# 

以上,我们就创建了3个版本,版本1为创建dossier_1.sh文件,版本2为修改dossier_1.sh文件,新加一个dossier_2.sh文件,模拟版本3为新加一个dossier_3.sh文件										#查看状态,三大区域已经一致# On branch masternothing to commit, working directory clean[root@git Dosier_Project]# 以上,我们就创建了3个版本,版本1为创建dossier_1.sh文件,版本2为修改dossier_1.sh文件,新加一个dossier_2.sh文件,模拟版本3为新加一个dossier_3.sh文件

6. Use git log command and git reflog command to check which versions are in the git warehouse
Both git log and git reflog commands can be used Check which versions are in the git warehouse, but the difference between the two is that git log can only see the current version, while git reflog can see all versions. That is to say, when you roll back the version, git reflog can see the response. To return to the previous version, you can generally use git reflog.

[root@git Dosier_Project]# git log									#git log命令查看git仓库中有哪些版本commit 72cd563e2e0241670d7ebe8c541f28a12875e1e8
Author: lisi Date:   Mon Jan 31 18:05:42 2022 +0800

    Add dossier_3.sh

commit beff2eaf816f345ba39779494752228a06ac1499
Author: lisi Date:   Mon Jan 31 18:01:03 2022 +0800

    modified dossier_1.sh,add dossier_2.sh

commit ae2c1b8752efd01ef265e2227dd0b4ceb81310f4
Author: lisi Date:   Mon Jan 31 17:53:32 2022 +0800

    Add dossier_1.sh[root@git Dosier_Project]# git reflog								#git reflog命令查看git仓库中有哪些版本72cd563 HEAD@{0}: commit: Add dossier_3.sh
beff2ea HEAD@{1}: commit: modified dossier_1.sh,add dossier_2.sh
ae2c1b8 HEAD@{2}: commit (initial): Add dossier_1.sh[root@git Dosier_Project]#

7. git reset --hard version number to achieve free version rollback

[root@git Dosier_Project]# git reflog								#git reflog命令查看git仓库中有哪些版本,前面的字符就是版本号72cd563 HEAD@{0}: commit: Add dossier_3.sh
beff2ea HEAD@{1}: commit: modified dossier_1.sh,add dossier_2.sh
ae2c1b8 HEAD@{2}: commit (initial): Add dossier_1.sh[root@git Dosier_Project]# git reset --hard beff2ea  				#回退到版本2,即修改dossier_1.sh文件,新加dossier_2.shHEAD is now at beff2ea modified dossier_1.sh,add dossier_2.sh[root@git Dosier_Project]# ll										#查看文件,已经没有了dossier_3.sh文件total 8-rw-r--r-- 1 root root 35 Jan 31 17:56 dossier_1.sh
-rw-r--r-- 1 root root 15 Jan 31 17:56 dossier_2.sh[root@git Dosier_Project]# cat dossier_1.sh 						#dossier_1.sh的最后一行也确实实在版本2时候添加的I am dossier_1
I am good dossier_1[root@git Dosier_Project]# git reset --hard ae2c1b8      			#回退到版本1HEAD is now at ae2c1b8 Add dossier_1.sh[root@git Dosier_Project]# lltotal 4-rw-r--r-- 1 root root 15 Jan 31 18:38 dossier_1.sh					#已经回退到版本1[root@git Dosier_Project]# cat dossier_1.sh 						#已经回退到版本1I am dossier_1	
[root@git Dosier_Project]#[root@git Dosier_Project]# git reflog								#git reflog命令可以看到你所有的版本已经回退版本的记录ae2c1b8 HEAD@{0}: reset: moving to ae2c1b8
beff2ea HEAD@{1}: reset: moving to beff2ea
72cd563 HEAD@{2}: commit: Add dossier_3.sh
beff2ea HEAD@{3}: commit: modified dossier_1.sh,add dossier_2.sh
ae2c1b8 HEAD@{4}: commit (initial): Add dossier_1.sh[root@git Dosier_Project]# git reset --hard 72cd563					#为了后面实验,现在还是回退到版本3吧HEAD is now at 72cd563 Add dossier_3.sh[root@git Dosier_Project]# ll 										#已经回退到版本3了total 12-rw-r--r-- 1 root root 35 Jan 31 18:44 dossier_1.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 dossier_2.sh
-rw-r--r-- 1 root root 15 Jan 31 18:44 dossier_3.sh[root@git Dosier_Project]#

8. git checkout – filename, undo file modification (the file is not submitted to the temporary storage area)
The development engineer has modified a certain file, but the file has not been submitted to the temporary storage area. The modification of the file needs to be undone. The initial state of the file can be restored by deleting the file content. However, if a lot of modifications have been made, you may not even know what to delete. content, you can use the git checkout command at this time:

[root@git Dosier_Project]# echo "testesshfkshdsjdhjshka" >> dossier_3.sh [root@git Dosier_Project]# cat dossier_3.shI am dossier_3
testesshfkshdsjdhjshka[root@git Dosier_Project]# git checkout -- dossier_3.sh				#撤销文件修改,注意命令--后面是空格接文件名[root@git Dosier_Project]# cat dossier_3.sh            				#已经撤销成功I am dossier_3[root@git Dosier_Project]#

9. git reset HEAD file, undo the file in the temporary storage area

[root@git Dosier_Project]# echo "boss is good ">>dossier_3.sh 		#编辑一点内容[root@git Dosier_Project]# git add dossier_3.sh						#提交暂存区,这时突然反悔了,需要从暂存区撤销该文件[root@git Dosier_Project]# git status# On branch master# Changes to be committed:#   (use "git reset HEAD <file>..." to unstage)						#这里已经提示了,撤销暂存区文件使用git reset HEAD##       modified:   dossier_3.sh#[root@git Dosier_Project]# git reset HEAD dossier_3.sh				#撤销暂存区内的指定文件Unstaged changes after reset:
M       dossier_3.sh[root@git Dosier_Project]#</file>

10. Delete a file and submit it to the temporary storage with git rm District

[root@git Dosier_Project]# rm  -rf dossier_1.sh						#当文件不需要了直接rm -rf删除一个文件[root@git Dosier_Project]# git status								#查看状态# On branch master# Changes not staged for commit:#   (use "git add/rm <file>..." to update what will be committed)	#这里其实已经提示你使用add或rm参数了#   (use "git checkout -- <file>..." to discard changes in working directory)##       deleted:    dossier_1.sh#no changes added to commit (use "git add" and/or "git commit -a")[root@git Dosier_Project]# 	git rm 	dossier_1.sh					#提交暂存区[root@git Dosier_Project]# 	git commit -m "delete dossier_1.sh"		#把暂存区内容commit提交git仓库</file></file>

Recommended learning: Linux video tutorial

The above is the detailed content of What is the command to install git on linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn