Introduction to installing GIT and git commands under Linux

Posted by knox203 on Tue, 01 Feb 2022 20:52:16 +0100

preface

Environment: CentOS 7 9 git version 1.8.3.1

Git official website

https://git-scm.com/

Install Git

[root@git ~]# yum install git						#git installation, directly use yum to install

View version

[root@git ~]# git --version							#View git version
git version 1.8.3.1

git personal identity settings

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

[root@git ~]# git config --global user.name "lisi"						#Set user name
[root@git ~]# git config --global user.emain "456789123@qq.com"			#Set mailbox
[root@git ~]# git config --global color.ui true							#enable ui color
[root@git ~]# git config --list											#View configuration
user.name=lisi
user.emain=456789123@qq.com
color.ui=true
[root@git ~]#

Git's three core frameworks

Working Directory: the directory where developers usually store project code;
Stage: it is used to temporarily store the changes of developer code. In fact, it is just a file to save the list information of files to be submitted;
git Repository: it is a safe place to store data. git repository has all versions of data codes submitted by developers, and HEAD points to the latest version put into the repository.

Git's workflow is generally:
1. Add and modify code files in the working directory;
2. Put the code files requiring version management into the temporary storage area;
3. Submit the files in the staging area to the Git warehouse.

Therefore, the files managed by Git have three states: modified, staged and committed, corresponding to each process above 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									#Create a project code directory
[root@git ~]# cd Dosier_Project/									#Enter directory
[root@git Dosier_Project]# git init									#Initialize the directory so that the directory is under git version control
Initialized empty Git repository in /root/Dosier_Project/.git/		#An empty git repository was initialized
[root@git Dosier_Project]# ll -al									#View this directory
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						
#It is found that the git init initialization command is generated Git directory, this The GIT directory is the GIT warehouse, which will be in dosier in the future_ All operations under the project directory can be controlled by git version
[root@git Dosier_Project]# 

2. Simulation development engineer develops code file

[root@nginx Dosier_Project]# cd /root/Dosier_Project/				#Go to the project directory
[root@nginx Dosier_Project]# vim dossier.sh 						#Create a code file
[root@nginx Dosier_Project]# 

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

[root@nginx Dosier_Project]# git status								#View 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)
#Explanation: Untracked files indicates that there are Untracked files (the one in red below)
#The so-called "untracked" files refer to those newly added files that have not been added to the staging area or submitted. They are in a state of impunity. Once you add them to the staging area or submit them to the Git warehouse, they begin to be "tracked" by Git
[root@nginx Dosier_Project]# git add dossier.sh 					#Submit the file to the temporary storage area with git add command
[root@nginx Dosier_Project]# git status         					#View status again
# On branch master
#
# Initial commit
#
# Changes to be committed:											#Changes to be submitted, waiting to be submitted
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   dossier.sh										#There is already a file in the staging area
#
[root@nginx Dosier_Project]#

4. git commit submits all files in the temporary storage area to the git warehouse
git commit is to submit all files in the temporary storage area to the git warehouse, not to submit a file.

[root@git Dosier_Project]# git commit -m "lisi add dossier.sh file"		#Submit the files in the temporary storage area to the git warehouse, and the - m parameter indicates the written comments
[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				#Check the status again, and you will be prompted that there is no to commit in the temporary storage area, and the working directory file has not been edited
# On branch master
nothing to commit, working directory clean
[root@git Dosier_Project]# 

5. Continue to simulate version 2 and version 3

#Simulation version 2. Simulation version 2 is modified to dossier_1.sh file, add a dossier_2.sh file
[root@git ~]# cd ~/Dosier_Project/
[root@git Dosier_Project]# echo "I am good dossier_1" >> dossier_1.sh 		#Modified dossier_1.sh file
[root@git Dosier_Project]# touch dossier_2.sh								#Newly added dossier_2.sh file
[root@git Dosier_Project]# echo "I am dossier_2" >> dossier_2.sh
[root@git Dosier_Project]# git add *										#The asterisk matches all (equivalent to -- all), and all files are submitted to the temporary storage area
[root@git Dosier_Project]# git commit -m "modified dossier_1.sh,add dossier_2.sh"	#git commit submits the staging area file to the warehouse
[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										#Check the status. The three regions are consistent
# On branch master
nothing to commit, working directory clean

#Continue to simulate version 3. Continue to simulate version 3 and add a dossier for the new version_ 3. Sh file
[root@git Dosier_Project]# touch dossier_3.sh								#Add a new dossier_3.sh file
[root@git Dosier_Project]# echo "I am dossier_3">> dossier_3.sh
[root@git Dosier_Project]# git add dossier_3.sh 							#Put dossier_3.sh submit to staging area
[root@git Dosier_Project]# git commit -m "Add dossier_3.sh"					#Submit the contents of temporary storage area to git warehouse
[master 72cd563] Add dossier_3.sh
 1 file changed, 1 insertion(+)
 create mode 100644 dossier_3.sh
[root@git Dosier_Project]# git status										#Check the status. The three regions are consistent
# On branch master
nothing to commit, working directory clean
[root@git Dosier_Project]# 

Above, we have created three versions, and version 1 is the creation dossier_1.sh File, version 2 is modified dossier_1.sh File, add a new one dossier_2.sh File, simulation version 3 is a new one dossier_3.sh file

6. git log command and git reflog command to check which versions are available in the GIT warehouse
Both git log and git reflog commands can check which versions are available in the GIT warehouse, but the difference between them is that git log can only see the current version, while git reflog can see all versions, that is, after you fallback the version, GIT reflog can view the version before the fallback. Generally, use git reflog.

[root@git Dosier_Project]# git log									#The git log command checks which versions are available in the git repository
commit 72cd563e2e0241670d7ebe8c541f28a12875e1e8
Author: lisi <456789123@qq.com>
Date:   Mon Jan 31 18:05:42 2022 +0800

    Add dossier_3.sh

commit beff2eaf816f345ba39779494752228a06ac1499
Author: lisi <456789123@qq.com>
Date:   Mon Jan 31 18:01:03 2022 +0800

    modified dossier_1.sh,add dossier_2.sh

commit ae2c1b8752efd01ef265e2227dd0b4ceb81310f4
Author: lisi <456789123@qq.com>
Date:   Mon Jan 31 17:53:32 2022 +0800

    Add dossier_1.sh
[root@git Dosier_Project]# git reflog								#The git reflog command checks which versions are available in the git repository
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 realize the free fallback of the version

[root@git Dosier_Project]# git reflog								#The git reflog command checks which versions are available in the git repository. The preceding character is the version number
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  				#Go back to version 2, that is, modify dossier_1.sh file, newly added dossier_2.sh
HEAD is now at beff2ea modified dossier_1.sh,add dossier_2.sh
[root@git Dosier_Project]# ll										#Check the file. There is no dossier_3.sh file
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_ The last line of 1.sh is actually added in version 2
I am dossier_1
I am good dossier_1
[root@git Dosier_Project]# git reset --hard ae2c1b8      			#Fallback to version 1
HEAD is now at ae2c1b8 Add dossier_1.sh
[root@git Dosier_Project]# ll
total 4
-rw-r--r-- 1 root root 15 Jan 31 18:38 dossier_1.sh					#Fallback to version 1
[root@git Dosier_Project]# cat dossier_1.sh 						#Fallback to version 1
I am dossier_1	
[root@git Dosier_Project]#
[root@git Dosier_Project]# git reflog								#The git reflog command can see the records of all your versions that have been rolled back
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					#For later experiments, let's go back to version 3
HEAD is now at 72cd563 Add dossier_3.sh
[root@git Dosier_Project]# ll 										#Back to version 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 the file modification (the file is not submitted to the temporary storage area)
If the development engineer modifies a file and the file is not submitted to the temporary storage area, the modification of the file needs to be revoked. You can restore the initial state of the file by deleting the contents of the file. However, if there are many modifications and you don't even know what to delete, you can use the git checkout command:

[root@git Dosier_Project]# echo "testesshfkshdsjdhjshka" >> dossier_3.sh 
[root@git Dosier_Project]# cat dossier_3.sh
I am dossier_3
testesshfkshdsjdhjshka
[root@git Dosier_Project]# git checkout -- dossier_3.sh				#Undo the file modification, pay attention to the command -- followed by a space followed by the file name
[root@git Dosier_Project]# cat dossier_3.sh            				#Successfully revoked
I am dossier_3
[root@git Dosier_Project]#

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

[root@git Dosier_Project]# echo "boss is good ">>dossier_3.sh 		#Edit a little content
[root@git Dosier_Project]# git add dossier_3.sh						#When submitting to the staging area, you suddenly back off and need to revoke the file from the staging area
[root@git Dosier_Project]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)						#You have been prompted to use git reset HEAD to undo the staging area file
#
#       modified:   dossier_3.sh
#
[root@git Dosier_Project]# git reset HEAD dossier_3.sh				#Undo the specified file in the staging area
Unstaged changes after reset:
M       dossier_3.sh
[root@git Dosier_Project]# 

10. Delete a file and git rm submit it to the staging area

[root@git Dosier_Project]# rm  -rf dossier_1.sh						#When a file is not needed, directly rm -rf delete a file
[root@git Dosier_Project]# git status								#View status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)	#In fact, you have been prompted to use the add or rm parameter here
#   (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					#Submit staging area
[root@git Dosier_Project]# 	git commit -m "delete dossier_1.sh"		#commit the contents of the staging area to the git warehouse

git branch management

Later, we will talk about GitHub remote warehouse. All members of the development team need to submit code to the remote warehouse. Why do we need branches?
Imagine that if there is only one master branch, each team member will clone A code from the master branch of the remote warehouse and download it to his computer for coding. If member A has not finished coding and has only developed some functions, but he is afraid that the development code will be lost due to problems on his computer, so A needs to submit the code to the remote warehouse, Is it difficult for A to directly submit the unfinished code to the main branch of the remote master? Then came A newcomer F, f to download the unfinished master branch code?
In fact, it's not. In enterprises, remote warehouses usually create multiple branches, at least one dev branch. A can first submit code to the dev branch, newcomer F clones code from the master branch, and others clone branches from the master. Then everyone submits to the dev branch. Finally, the warehouse administrator leader decides whether to merge dev branch code to the master branch, The code of the master branch is the last software code provided to the user, so you must ensure that the code of the master branch is the most correct.

[root@git Dosier_Project]# git branch						#List the branches. There is only one master branch. An asterisk indicates that it is also in the master branch
* master
[root@git Dosier_Project]# git branch dev					#Create a dev branch
[root@git Dosier_Project]# git checkout dev					#Switch to dev branch
Switched to branch 'dev'
[root@git Dosier_Project]# git branch						#Look at the branch. Now it's actually on the dev branch
* dev
  master
[root@git Dosier_Project]# touch dossier_4.sh				#Create a dev branch
[root@git Dosier_Project]# echo "dev branch" >>dossier_4.sh 
[root@git Dosier_Project]# git status						
# On branch dev												#Now the added file is on the dev branch
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       dossier_4.sh
nothing added to commit but untracked files present (use "git add" to track)
[root@git Dosier_Project]# git add dossier_4.sh 			#Submit staging area
[root@git Dosier_Project]# git commit -m "add dossier_4.sh on dev branch"		#Submit the warehouse and notice that the dev branch is submitted
[dev 00444bd] add dossier_4.sh on dev branch
 1 file changed, 1 insertion(+)
 create mode 100644 dossier_4.sh
[root@git Dosier_Project]# git checkout master				#Switch to the master branch
Switched to branch 'master'
[root@git Dosier_Project]# ll								#Didn't see dossier_4.sh file, because the master has not been merged
total 12
-rw-r--r-- 1 root root 35 Jan 31 20:08 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 19:58 dossier_3.sh
[root@git Dosier_Project]# git merge dev					#Merge dev branch to master branch
Updating 72cd563..00444bd
Fast-forward
 dossier_4.sh | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 dossier_4.sh
[root@git Dosier_Project]# ll								#You can already see dossier_4.sh file
total 16
-rw-r--r-- 1 root root 35 Jan 31 20:08 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 19:58 dossier_3.sh
-rw-r--r-- 1 root root 11 Jan 31 21:25 dossier_4.sh
[root@git Dosier_Project]# git branch -d dev				#Delete branch

summary

1,git Installation of
[root@git ~]# yum install git										#git installation, directly use yum to install
[root@git ~]# git --version											#View git version
git version 1.8.3.1
2,Create project directory and initialize
[root@git ~]# mkdir Dosier_Project									#Create a project code directory
[root@git ~]# cd Dosier_Project/									#Enter directory
[root@git Dosier_Project]# git init									#Initialize the directory so that the directory is under git version control
[root@git Dosier_Project]# ll -a 									#The. git directory is the git repository
drwxr-xr-x  3 root root  78 Jan 31 18:04 .
dr-xr-x---. 5 root root 215 Jan 31 17:52 ..
drwxr-xr-x  8 root root 166 Jan 31 18:05 .git
[root@nginx Dosier_Project]# vim dossier.sh 						#Create a code file and edit and save the code
[root@nginx Dosier_Project]# git status 							#View git status
[root@nginx Dosier_Project]# git add dossier.sh						#The git add command puts the file into the temporary storage area
[root@nginx Dosier_Project]# git add *								#The git add command puts the file into the temporary storage area, and the asterisk matches all files
[root@nginx Dosier_Project]# git add --all							#The git add command puts the file into the temporary storage area, - all means all, which is equivalent to*
[root@nginx Dosier_Project]# git commit -m "Add dossier.sh"			#git commit means that all files in the temporary storage area are submitted to the git warehouse, and the - m parameter is annotated
[root@nginx Dosier_Project]# git log								#git log view the current version in git warehouse
[root@nginx Dosier_Project]# git reflog 							#git reflog view all versions in git warehouse, including the version records of fallback
[root@git Dosier_Project]# git reset --hard 72cd563					#Free fallback version: first check the version number of GIT warehouse with git reflog
[root@git Dosier_Project]# git checkout -- dossier_3.sh				#Undo the file modification, pay attention to the command -- followed by a space followed by the file name
[root@git Dosier_Project]# git rm dossier_1.sh						#After rm deletes a file, it submits the deleted file to the temporary storage area instead of git add
[root@git Dosier_Project]# git branch								#View branch
[root@git Dosier_Project]# git checkout dev							#Switch branch
[root@git Dosier_Project]# git merge dev							#Currently, it operates under the master branch, merging the dev branch to the master branch
[root@git Dosier_Project]# git branch -d dev						#Delete branch

https://www.jianshu.com/p/e57a4a2cf077

Topics: Linux Operation & Maintenance git