• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

代码上线——Git基本命令介绍和相关操作【显哥出品,必为精品】

Gitlib lixian 5年前 (2020-05-11) 1938次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、DevOps介绍

MTTR、MTTF、MTBF是体现系统可靠性的重要指标,但是三者容易混淆,下文使用图解方式解释三者之间的区别,希望能起到解惑的效用。

MTTF (Mean Time To Failure,平均无故障时间),指系统无故障运行的平均时间,取所有从系统开始正常运行到发生故障之间的时间段的平均值。 MTTF =∑T1/ N

MTTR (Mean Time To Repair,平均修复时间),指系统从发生故障到维修结束之间的时间段的平均值。MTTR =∑(T2+T3)/ N

MTBF (Mean Time Between Failure,平均失效间隔),指系统两次故障发生时间之间的时间段的平均值。 MTBF =∑(T2+T3+T1)/ N

很明显:MTBF= MTTF+ MTTR

平均故障时间

1个9 (1-90%)= 876

2个9 (1-99%)*365*24= 87.6

3个9:(1-99.9%)*365*24=8.76小时,表示该系统在连续运行1年时间里最多可能的业务中断时间是8.76小时。

4个9:(1-99.99%)*365*24=0.876小时=52.6分钟,表示该系统在连续运行1年时间里最多可能的业务中断时间是52.6分钟。

5个9:(1-99.999%)*365*24*60=5.26分钟,表示该系统在连续运行1年时间里最多可能的业务中断时间是5.26分钟。

那么X个9里的X只代表数字3~5,为什么没有1~2,也没有大于6的呢?我们接着往下计算: 1个9:(1-90%)*365=36.5天 2个9:(1-99%)*365=3.65天 6个9:(1-99.9999%)*365*24*60*60=31秒

二、版本控制系统

VCS Version Control System

版本控制系统就是一种能够记录一个或者多个文件的内容变化,方便以后来查询特定的版本的内容的系统

记录文件的所有历史变化 每一次的commit提交

随时恢复到任何一个状态

多人协作开发的

为什么要用版本控制系统

什么是程序 开发写的代码文件 存放在磁盘中 文件

常见的版本控制系统工具

SVN 集中式的版本控制系统 只有一个中央仓库 如果中央数据仓库宕机了或者补课访问,所有人使用不了SVN,无法进行上传和下载

Git 分布式版本控制系统 开源的 GitHub 公有的 Gitlab 私有的

三、Git安装部署

1.环境准备

[root@gitlab ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@gitlab ~]# uname -r
3.10.0-957.el7.x86_64
[root@gitlab ~]# getenforce 
Disabled
[root@gitlab ~]# iptables-save 
[root@gitlab ~]# date
Mon May 11 19:50:08 CST 2020
[root@gitlab ~]# ll /etc/yum.repos.d/
total 36
-rw-r--r--. 1 root root 2523 Feb 11 22:55 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Nov 23  2018 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Nov 23  2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 Nov 23  2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Nov 23  2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Nov 23  2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 5701 Nov 23  2018 CentOS-Vault.repo
-rw-r--r--. 1 root root  664 Feb 11 22:55 epel.repo

2.安装Git

[root@gitlab ~]# yum install -y git
[root@gitlab ~]# git --version
git version 1.8.3.1

3.配置git

[root@gitlab  ~]# git config  --global   user.name  "lx"	
[root@gitlab ~]# git config  --global   user.email "lixian656@qq.com"
[root@gitlab ~]# git config  --global   color.ui true      

4.检查配置

[root@gitlab ~]# git config  --list
user.name=lx
user.email=lixian656@qq.com
color.ui=true
[root@gitlab ~]# cat .gitconfig
[user]
	name = lx
	email = lixian656@qq.com
[color]
	ui = true

5.Git初始化

#针对一个目录进行初始化  初始化为一个仓库    

[root@gitlab ~]# mkdir git_test
[root@gitlab ~]# ll git_test -d
drwxr-xr-x 2 root root 6 2020-05-11 12:06 git_test
[root@gitlab ~]# cd git_test/
[root@gitlab ~/git_test]# git init 
Initialized empty Git repository in /root/git_test/.git/

[root@git ~/git_test]# ll .git/
total 12
drwxr-xr-x 2 root root   6 2020-05-11 12:06 branches		#分支目录 
-rw-r--r-- 1 root root  92 2020-05-11 12:06 config			#项目的配置选项 
-rw-r--r-- 1 root root  73 2020-05-11 12:06 description		#git web程序使用的
-rw-r--r-- 1 root root  23 2020-05-11 12:06 HEAD			#指向当前的分支  指针 
drwxr-xr-x 2 root root 242 2020-05-11 12:06 hooks			#钩子文件
drwxr-xr-x 2 root root  21 2020-05-11 12:06 info			#包含了一个全局排除文件
drwxr-xr-x 4 root root  30 2020-05-11 12:06 objects			#存放所有的数据内容
drwxr-xr-x 4 root root  31 2020-05-11 12:06 refs			#存放指针指向的分支
											index			#默认没有,将代码存在在暂存区时,自动创建,保存暂存区里面的内容

四、Git的相关介绍

git_test目录的一家子

组成:

工作目录

暂存区域

本地仓库

远程仓库

创建数据,提交数据

Git四种状态

Untracked 未跟踪

Unmodified 未修改

Modified 已修改

Staged 已暂存

五、Git的基础命令

#显示Git当前仓库的状态  

[root@gitlab ~/git_test]# git status 
# On branch master		#位于master分支
#
# Initial commit		#初始的提交
#
nothing to commit (create/copy files and use "git add" to track)
#没有任何提交		创建或者拷贝文件和使用git add 命令进行建立跟踪

1.将工作目录中的文件添加到暂存区

#在工作目录中创建几个测试文件

[root@gitlab ~/git_data]# touch {1..3}.txt
[root@gitlab ~/git_data]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 11 21:14 1.txt
-rw-r--r-- 1 root root 0 May 11 21:14 2.txt
-rw-r--r-- 1 root root 0 May 11 21:14 3.txt

#将工作目录中的文件添加到暂存区  
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Untracked files:
       未跟踪的文件
#   (use "git add <file>..." to include in what will be committed)
#
#	1.txt
#	2.txt
#	3.txt
nothing added to commit but untracked files present (use "git add" to track)

[root@gitlab ~/git_data]# git add 1.txt
  #将1.txt文件添加到暂存区
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	2.txt
#	3.txt
	
[root@gitlab ~/git_data]# git add *
     #将所有的文件添加到暂存区(* 或者 .)
[root@gitlab ~/git_data]# git add .
     #将所有的文件添加到暂存区 
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#	new file:   2.txt
#	new file:   3.txt
# 

2.删除暂存区的文件和工作目录中的文件

#删除暂存区的文件和工作目录中的文件

#第一种方法:

#1.先删除暂存区中的文件
[root@gitlab ~/git_data]# git rm --cached 1.txt
rm '1.txt'
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   2.txt
#	new file:   3.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	1.txt

[root@gitlab ~/git_data]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 11 21:14 1.txt
-rw-r--r-- 1 root root 0 May 11 21:14 2.txt
-rw-r--r-- 1 root root 0 May 11 21:14 3.txt

#2.再删除工作目录中的文件
[root@gitlab ~/git_data]# rm -rf 1.txt 
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   2.txt
#	new file:   3.txt
#

#第二种方法:

#把暂存区和工作目录中的文件一起删除 
[root@gitlab ~/git_data]# git rm -rf 2.txt
rm '2.txt'
[root@gitlab ~/git_data]# git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   3.txt
#
[root@gitlab ~/git_data]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 11 21:14 3.txt

3.将暂存区中的文件提交到本地仓库

[root@gitlab ~/git_data]# touch {1,2}.txt
[root@gitlab ~/git_data]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 11 21:23 1.txt
-rw-r--r-- 1 root root 0 May 11 21:23 2.txt
-rw-r--r-- 1 root root 0 May 11 21:14 3.txt
[root@gitlab ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   3.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	1.txt
#	2.txt
[root@gitlab ~/git_data]# git add *
[root@gitlab ~/git_data]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   1.txt
#	new file:   2.txt
#	new file:   3.txt
#

#将暂存区中的文件提交到本地仓库

[root@gitlab ~/git_data]# git commit -m "add new files"
[master (root-commit) 1b14854] add new files
 3 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt
 create mode 100644 2.txt
 create mode 100644 3.txt
		
[root@gitlab ~/git_data]# git status
# On branch master
    #工作目录  暂存区  本地仓库的文件是一致的
nothing to commit, working directory clean

4.文件重命名

#文件如何重命名  

#第一种方法

#1. 本地重命名 

[root@gitlab ~/git_data]# mv 1.txt 1.log
[root@gitlab ~/git_data]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	deleted:    1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	1.log
no changes added to commit (use "git add" and/or "git commit -a")

#2. 把暂存区中的1.txt删除

[root@gitlab ~/git_data]# git rm --cached 1.txt
rm '1.txt'
[root@gitlab ~/git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	deleted:    1.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	1.log

#3. 将修改之后的文件名添加到暂存区 

[root@gitlab ~/git_data]# git add *
[root@gitlab ~/git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    1.txt -> 1.log
#

#4.将修改之后的文件提交到本地仓库 
[root@gitlab ~/git_data]# git commit -m "rename 1.txt 1.log"
[master ef4fd8e] rename 1.txt 1.log
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 1.txt => 1.log (100%)
[root@gitlab ~/git_data]# git status
# On branch master
nothing to commit, working directory clean

#第二种方法

#1. 直接重命名工作目录及暂存区的文件名称
[root@gitlab ~/git_data]# git mv 1.log 1.txt
[root@gitlab ~/git_data]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 11 21:31 1.txt
-rw-r--r-- 1 root root 0 May 11 21:23 2.txt
-rw-r--r-- 1 root root 0 May 11 21:14 3.txt
[root@gitlab ~/git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    1.log -> 1.txt
#


#2. 将修改之后的文件提交到本地仓库 

[root@gitlab ~/git_data]# git commit -m "rename 1.log 1.txt"
[master b292668] rename 1.log 1.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 1.log => 1.txt (100%)
[root@gitlab ~/git_data]# git status
# On branch master
nothing to commit, working directory clean

5.文件内容比对

#文件内容的比对  
git status  只能查看区域状态的不同,不能查看文件的内容不同之处  

#改变了工作目录中a.txt文件中的内容 
[root@gitlab ~/git_data]# echo "lixian zhen shuai" >> 1.txt 
[root@gitlab ~/git_data]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

#进行比对工作目录和暂存区中文件内容的不同之处  

[root@gitlab ~/git_data]# git diff *
diff --git a/1.txt b/1.txt
index e69de29..1104e56 100644
--- a/1.txt
+++ b/1.txt
@@ -0,0 +1 @@
+lixian zhen shuai

[root@gitlab ~/git_data]# git add *
  #添加到暂存区  
[root@gitlab ~/git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   1.txt
#
[root@gitlab ~/git_data]# git diff *	
[root@gitlab ~/git_data]#没有结果,显示工作目录和暂存区中的文件内容是一致的


#对比暂存区和本地仓库的文件内容不同之处 

[root@gitlab ~/git_data]# git diff --cached
diff --git a/1.txt b/1.txt
index e69de29..1104e56 100644
--- a/1.txt
+++ b/1.txt
@@ -0,0 +1 @@
+lixian zhen shuai

#将数据提交到本地仓库  

[root@gitlab ~/git_data]# git commit -m "modify 1.txt 111"
[master 773d9b8] modify 1.txt 111
 1 file changed, 1 insertion(+)
[root@gitlab ~/git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff --cached
[root@gitlab ~/git_data]#

6.查询历史操作记录、恢复快照

#查看Git的历史操作记录    commit提交一次 就会记录一次日志  

[root@gitlab ~/git_data]# git log
commit 773d9b850199ef027a48602a23fea73e34880371
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:43:24 2020 +0800

    modify 1.txt 111

commit b292668d4c2f0ddd616fad2f2501f60e894148df
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:37:38 2020 +0800

    rename 1.log 1.txt

commit ef4fd8e94f9978e86186d9e71e8e50345a58d789
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:36:24 2020 +0800

    rename 1.txt a.log

commit 1b1485473ee68dd1cc05c5c90e6155f892985054
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:24:22 2020 +0800

    add new files

#使用一行内容显示每次的commit提交
[root@gitlab ~/git_data]# git log --oneline
773d9b8 modify 1.txt 111
b292668 rename 1.log 1.txt
ef4fd8e rename 1.txt a.log
1b14854 add new files

#显示当前所在分支的指针 
[root@gitlab ~/git_data]# git log --oneline --decorate
773d9b8 (HEAD, master) modify 1.txt 111
b292668 rename 1.log 1.txt
ef4fd8e rename 1.txt a.log
1b14854 add new files


#显示日志中具体的信息 

[root@gitlab ~/git_data]# git log -p


#只显示最近的几条日志  

[root@gitlab ~/git_data]# git log -1
commit 773d9b850199ef027a48602a23fea73e34880371
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:43:24 2020 +0800

    modify 1.txt 111
[root@gitlab ~/git_data]# git log -2
commit 773d9b850199ef027a48602a23fea73e34880371
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:43:24 2020 +0800

    modify 1.txt 111

commit b292668d4c2f0ddd616fad2f2501f60e894148df
Author: lx <lixian656@qq.com>
Date:   Mon May 11 21:37:38 2020 +0800

    rename 1.log 1.txt


#恢复数据  


#1.改变了工作目录中的文件,没有添加到暂存区  

[root@gitlab ~/git_data]# echo "lixian shuai shuai da" >> 1.txt 
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
lixian shuai shuai da
[root@gitlab ~/git_data]# git diff *
diff --git a/1.txt b/1.txt
index 1104e56..39bd7b9 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 lixian zhen shuai
+lixian shuai shuai da
[root@gitlab ~/git_data]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")


#将暂存区文件的内容恢复到工作目录中  

[root@gitlab ~/git_data]# git checkout -- *    #覆盖所有
[root@gitlab ~/git_data]# git checkout -- 1.txt   #覆盖某个文件
[root@gitlab ~/git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai

#2. 修改了工作目录并且提交到了暂存区  

[root@gitlab ~/git_data]# echo "xian gege shuai shuai da " >> 1.txt 
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
xian gege shuai shuai da 
[root@gitlab ~/git_data]# git add *
[root@gitlab ~/git_data]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   1.txt
#
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# git diff --cached *
diff --git a/1.txt b/1.txt
index 1104e56..2e191ee 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 lixian zhen shuai
+xian gege shuai shuai da


#将本地仓库中的文件内容覆盖到暂存区  

[root@gitlab ~/git_data]# git reset HEAD *
Unstaged changes after reset:
M	1.txt
[root@gitlab ~/git_data]# git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@gitlab ~/git_data]# git diff --cached *
[root@gitlab ~/git_data]# git diff *
diff --git a/1.txt b/1.txt
index 1104e56..2e191ee 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
 lixian zhen shuai
+xian gege shuai shuai da

#将暂存区中的文件内容覆盖到工作目录 

root@gitlab ~/git_data]# git checkout -- *
[root@gitlab ~/git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai


#3. 改变了工作目录中的文件内容,并提交到暂存区和本地仓库  

[root@gitlab ~/git_data]# echo "xian gege shuai shuai da " >> 1.txt 
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
xian gege shuai shuai da 
[root@gitlab ~/git_data]# git add *
[root@gitlab ~/git_data]# git commit -m "modify 1.txt 2"
[master 50297fe] modify 1.txt 2
 1 file changed, 1 insertion(+)
[root@gitlab ~/git_data]# echo "hhhhhhhhhhha" >> 1.txt 
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
xian gege shuai shuai da 
hhhhhhhhhhha
[root@gitlab ~/git_data]# git commit -am "modify 1.txt 3"
[master d2837a2] modify 1.txt 3
 1 file changed, 1 insertion(+)
[root@gitlab ~/git_data]# git status 
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# git diff --cached

#还原某一次的快照  

[root@gitlab ~/git_data]# git log --oneline
d2837a2 modify 1.txt 3
50297fe modify 1.txt 2
773d9b8 modify 1.txt 111
b292668 rename 1.log 1.txt
ef4fd8e rename 1.txt a.log
1b14854 add new files

#从历史记录中将数据覆盖到本地仓库 暂存区 工作目录中  

[root@gitlab ~/git_data]# git reset --hard 773d9b8
HEAD is now at 773d9b8 modify 1.txt 111
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
[root@gitlab ~/git_data]# git status 
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# git diff --cached *


#日志不在记录未发生的事件  还原历史状态之后的事件都不记录
[root@gitlab ~/git_data]# git log --oneline
773d9b8 modify 1.txt 111
b292668 rename 1.log 1.txt
ef4fd8e rename 1.txt a.log
1b14854 add new files

#发现自己还原错了   

#记录总的历史记录 
[root@gitlab ~/git_data]# git reflog
773d9b8 HEAD@{0}: reset: moving to 773d9b8
d2837a2 HEAD@{1}: commit: modify 1.txt 3
50297fe HEAD@{2}: commit: modify 1.txt 2
773d9b8 HEAD@{3}: commit: modify 1.txt 111
b292668 HEAD@{4}: commit: rename 1.log 1.txt
ef4fd8e HEAD@{5}: commit: rename 1.txt a.log
1b14854 HEAD@{6}: commit (initial): add new files
[root@gitlab ~/git_data]# git reset --hard d2837a2
HEAD is now at d2837a2 modify 1.txt 3
[root@gitlab ~/git_data]# cat 1.txt 
lixian zhen shuai
xian gege shuai shuai da 
hhhhhhhhhhha
[root@gitlab ~/git_data]# git status 
# On branch master
nothing to commit, working directory clean
[root@gitlab ~/git_data]# git diff *
[root@gitlab ~/git_data]# git diff --cached *

六、Git的分支

Git 的分支,其实本质上仅仅是指向提交对象的可变指针。 主分支master

在企业中,master分支是要非常稳定的,不能直接修改master分支里面的内容,使用的时候创建一个普通分支,等完成工作之后,将其合并到master分支上。

#显示所有分支,前面的*代表在哪个分支下工作
[root@gitlab ~/tlbb]# git branch
* master

#创建分支
[root@gitlab ~/tlbb]# git branch lx
[root@gitlab ~/tlbb]# git branch
  lx
* master

#切换分支
[root@gitlab ~/tlbb]# git checkout lx
Switched to branch 'lx'
[root@gitlab ~/tlbb]# git branch
* lx
  master

使用普通分支创建文件

[root@gitlab ~/tlbb]# touch lixian.txt
[root@gitlab ~/tlbb]# git add *
[root@gitlab ~/tlbb]# git commit -m "add lixian.txt lx"
[lx babc483] add lixian.txt lx
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 lixian.txt

#显示普通分区创建的文件
[root@gitlab ~/tlbb]# git log --oneline --decorate
babc483 (HEAD, lx) add lixian.txt lx
a26c31d (origin/master, origin/HEAD, master) Merge branch 'windows' into 'master'
b87de94 (origin/windows) add new file windows.lx
0c73d71 Merge branch 'test' into 'master'
6b8e787 add test.txt file
7ba6ecf add lixian.lig file
9c34701 Merge 10.0.0.100:Dev/tlbb
a30af39 add master.txt file
a567e77 Merge 10.0.0.100:Dev/tlbb into test
0282680 add lx file
4042a69 add dev01 log file

#切换到master发现普通分支创建的文件,在master看不到,需要合并分支
[root@gitlab ~/tlbb]# git checkout master
Switched to branch 'master'
[root@gitlab ~/tlbb]# ls

#合并分支
[root@gitlab ~/tlbb]# git branch
  lx
* master
[root@gitlab ~/tlbb]# git merge lx   #合并分支,指定分支的名称
Updating a26c31d..babc483
Fast-forward
 lixian.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 lixian.txt
[root@gitlab ~/tlbb]# ls
lixian.txt

#删除分支 , -d参数删除
[root@gitlab ~/tlbb]# git branch -d lx
Deleted branch lx (was babc483).
[root@gitlab ~/tlbb]# git branch
* master

七、Git的标签

[root@gitlab ~/tlbb]# git log --oneline
babc483 add lixian.txt lx
a26c31d Merge branch 'windows' into 'master'
b87de94 add new file windows.lx
0c73d71 Merge branch 'test' into 'master'
6b8e787 add test.txt file
7ba6ecf add lixian.lig file
9c34701 Merge 10.0.0.100:Dev/tlbb
a30af39 add master.txt file
a567e77 Merge 10.0.0.100:Dev/tlbb into test
0282680 add lx file
4042a69 add dev01 log file
#给最近的一次提交打标签
[root@gitlab ~/tlbb]# git tag -a v1.0 -m "modify add lixian.txt lx"

#显示所有标签,并查看标签的内容
[root@gitlab ~/tlbb]# git tag
v1.0
[root@gitlab ~/tlbb]# git show v1.0
tag v1.0
Tagger: lx <lixian656@qq.com>
Date:   Wed May 13 14:51:41 2020 +0800

modify add lixian.txt lx

commit babc483b1ac914b218b2a2cf795ccbb45185232a
Author: lx <lixian656@qq.com>
Date:   Wed May 13 14:43:34 2020 +0800

    add lixian.txt lx

diff --git a/lixian.txt b/lixian.txt
new file mode 100644
index 0000000..e69de29

#给指定某一次的提交打标签
[root@gitlab ~/tlbb]# git tag -a v2.5 b87de94 -m "modify add new file windows.lx"
[root@gitlab ~/tlbb]# git tag
v1.0
v2.5

#根据标签恢复数据
[root@gitlab ~/tlbb]# git reset --hard v2.5
HEAD is now at b87de94 add new file windows.lx
[root@gitlab ~/tlbb]# ls
1.txt  2.txt  3.txt  dev01.log  lixian.log
[root@gitlab ~/tlbb]# git reset --hard v1.0
HEAD is now at babc483 add lixian.txt lx
[root@gitlab ~/tlbb]# ls
1.txt  2.txt  3.txt  dev01.log  lixian.log  lixian.txt

#删除标签
[root@gitlab ~/tlbb]# git tag
v1.0
v2.5
[root@gitlab ~/tlbb]# git tag -d v1.0
Deleted tag 'v1.0' (was d85263c)
[root@gitlab ~/tlbb]# git tag
v2.5

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:代码上线——Git基本命令介绍和相关操作【显哥出品,必为精品】
喜欢 (1)

您必须 登录 才能发表评论!