Git 、Jenkins (一)Git基础

Git 、Jenkins (一)Git基础

一、Git

1.Git安装

1)系统环境准备

[root@git01 ~]# cat /etc/redhat-release  	#查看系统版本
CentOS Linux release 7.6.1810 (Core) 
[root@git01 ~]# uname -r
3.10.0-957.el7.x86_64  				   		#查看内核版本
[root@git01 ~]# getenforce 					#确认Selinux关闭状态
Disabled
[root@git01 ~]# systemctl status firewalld  #查看防火请状态 (关闭状态)
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

2) git 安装部署

[root@git01 ~]# yum install -y git   #安装 git
[root@git01 ~]# git --version        #查看 git版本
git version 1.8.3.1
[root@git01 ~]# git config
--global use global config file        #使用全局配置文件
--system use system config file        #使用系统级配置文件
--local use repository config file    #使用版本库级配置文件
[root@git01 ~]# git config --global user.name "wxw"  #配置git用户
[root@git01 ~]# git config --global user.email "2466236198@qq.com"  #配置给git邮箱
[root@git01 ~]# git config --global color.ui true #语法高亮显示
[root@git01 ~]# cat ~/.gitconfig
[user]
	name = wxw
	email = 2466236198@qq.com
[color]
	ui = true

[root@git01 ~]# git config --list   #查看配置列表信息
user.name=wxw
user.email=2466236198@qq.com
color.ui=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

3)git 初始化

[root@git01 ~]# mkdir git_test
[root@git01 ~]# cd git_test/
[root@git01 ~/git_test]# git init   #将test目录初始化仓库
Initialized empty Git repository in /root/git_test/.git/
[root@git01 ~/git_test]# git status   #查看当前git状态=
# On branch master      #位于分支 master
#
# Initial commit        #初始提交 
#
nothing to commit (create/copy files and use "git add" to track)  #无文件要提交(创建/拷贝文件并使用"git add" 建立跟踪)
#隐藏文件介绍:
branches         #分支目录
config           #定义项目特有的配置选项
description      #仅供git web程序使用
HEAD             #指示当前的分支
hooks            #包含git钩子文件
info             #包含一个全局排除文件(exclude文件)
objects          #存放所有数据内容,有info和pack两个子文件夹
refs             #存放指向数据(分支)的提交对象的指针
index            #保存暂存区信息,在执行git init的时候,这个文件还没有

二、 Git常规使用

1.Git的工作区、暂存区和版本库的含义

工作区:就是你在电脑里能看到的目录。

暂存区:英文叫stage, 或index。一般存放在 ".git目录下" 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。

版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

1)git 工作流程图:

2)git 工作关系图:

3)Git四种状态:

Untracked、Unmodified、Modified、Staged

Untracked(工作目录)-->git add -->Staged(暂存区)-->git commit版本-->Unmodified(本地仓库)-->Edit file-->Modified-->Stage the file-->Staged

4)git 常用命令

git add 加入暂存
git status 查看状态
git status -s 状态概览
git diff 尚未暂存的文件
git diff --staged 暂存区文件
git commit 提交更新
git reset 回滚
git rm 从版本库中移除
git rm --cached README 从暂存区中移除
git mv 相当于mv git rm git add 三个命令
#####################################################################################################
创建测试文件并查看
[root@git01 ~/git_test]# touch 123.txt 456.txt
[root@git01 ~/git_test]# echo "123456" >123.txt 
[root@git01 ~/git_test]# git status
# On branch master  #位于分支master
#
# Initial commit    #初始提交 
#
# Untracked files:  #未跟踪的文件:
  
#   (use "git add <file>..." to include in what will be committed)  #(使用 "git add <文件>..." 以包含要提交的内容)
#
#	123.txt  456.txt
nothing added to commit but untracked files present (use "git add" to track) #提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@git01 ~/git_test]# git add 123.txt  #提交到代码仓库
[root@git01 ~/git_test]# git status   #查看状态
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   123.txt    #新文件 
#
[root@git01 ~/git_test]# ll .git/
total 16
drwxr-xr-x 2 root root   6 Apr 20 23:46 branches  #分支目录 
-rw-r--r-- 1 root root  92 Apr 20 23:46 config  #git配置
-rw-r--r-- 1 root root  73 Apr 20 23:46 description    #git web程序使用
-rw-r--r-- 1 root root  23 Apr 20 23:46 HEAD #分支的指向
drwxr-xr-x 2 root root 242 Apr 20 23:46 hooks #钩子文件,条件触发
-rw-r--r-- 1 root root 104 Apr 21 00:35 index
drwxr-xr-x 2 root root  21 Apr 20 23:46 info    #排除的一些内容,exclude
drwxr-xr-x 5 root root  40 Apr 21 00:35 objects   #存放数据的
drwxr-xr-x 4 root root  31 Apr 20 23:46 refs     #存放指向数据(分支)的提交对象的指针
[root@git01 ~/git_test]# git add .      #使用git add . 或者 * 添加目录中所有改动过的文件
[root@git01 ~/git_test]# git status    #查看状态
# On branch master
#
# Initial commit            #初始提交
#
# Changes to be committed:   #要变更的内容
#   (use "git rm --cached <file>..." to unstage)  (使用 "git rm --cached <文件>..." 以取消暂存)
#
#
#	new file:   123.txt #新文件 
#	new file:   456.txt #新文件 
#
[root@git01 ~/git_test]# git rm --cached 456.txt  #将 456.txt文件从缓存区删除
rm '456.txt'
[root@git01 ~/git_test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   123.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	456.txt
[root@git01 ~/git_test]# rm -fr 456.txt  #将456.txt 从工作目录中删除
[root@git01 ~/git_test]# touch 789.txt
[root@git01 ~/git_test]# git rm -f 789.txt   #将789.txt 文件从暂缓区,工作目录同时删除
rm '789.txt'
[root@git01 ~/git_test]# ls 
123.txt  456.txt
[root@git01 ~/git_test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   123.txt
#	new file:   456.txt

[root@git01 ~/git_test]# git commit -m " new file 789.txt"
[master (root-commit) 65f627f]  new file 789.txt
 2 files changed, 1 insertion(+)
 create mode 100644 123.txt
 create mode 100644 456.txt
[root@git01 ~/git_test]# git status
# On branch master   #位于分支 master
nothing to commit, working directory clean #无文件要提交,干净的工作区
[root@git01 ~/git_test]# git log   #查看日志
commit 65f627f502d94c7164cd23812d83790c1c7bbfbe  #回滚需要的id
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 01:52:43 2020 +0800

     new file 789.txt
[root@git01 ~/git_test]# vim 123.html
[root@git01 ~/git_test]# mv 123.html abc.html
[root@git01 ~/git_test]# git add abc.html 
[root@git01 ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   abc.html
[root@git01 ~/git_test]# touch  news.html
[root@git01 ~/git_test]# echo "7894562133" >news.html 
[root@git01 ~/git_test]# git add news.html 
[root@git01 ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   abc.html
#	new file:   news.html
#
[root@git01 ~/git_test]# git rm -f --cached abc.html   #将abc.html从暂存区移除
rm 'abc.html'
[root@git01 ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	new file:   news.html
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	abc.html
[root@git01 ~/git_test]# echo "12324567">>news.html  
[root@git01 ~/git_test]# git diff news.html  # 对比文件、查看内容的不同
diff --git a/news.html b/news.html
index 1a137db..30eef13 100644
--- a/news.html
+++ b/news.html
@@ -1 +1,2 @@
 7894562133
+12324567
[root@git01 ~/git_test]# git mv 123.txt  23.txt  #Git重命名
[root@git01 ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    123.txt -> 23.txt
#	new file:   news.html
#
[root@git01 ~/git_test]# git log --oneline   #显示commit信息
61f463b abc.html
835bda5 abc.html
ee87ed8 news.html
65f627f  new file 789.txt
[root@git01 ~/git_test]# git log --oneline --decorate  # 显示当前的指针指向哪里,在哪个快照下工作
61f463b (HEAD, master) abc.html  #在master下
835bda5 abc.html
ee87ed8 news.html
65f627f  new file 789.txt
[root@git01 ~/git_test]# git log -p  显示具体内容变化
commit 61f463b1d4525d1ef7865e8410289341c1f49c76
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 02:30:25 2020 +0800

    abc.html

diff --git a/news.html b/news.html
deleted file mode 100644
index 1a137db..0000000
--- a/news.html
+++ /dev/null
@@ -1 +0,0 @@
-7894562133

commit 835bda53cc5fcb731350ee7ef761ed884028fe8b
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 02:15:50 2020 +0800

    abc.html

diff --git a/abc.html b/abc.html
new file mode 100644
index 0000000..7294d90
--- /dev/null
+++ b/abc.html
@@ -0,0 +1 @@
+1234567894123

commit ee87ed8273ec776dff113dc524af8d46616d49a2

[root@git01 ~/git_test]# git log 1   #只显示一条
fatal: ambiguous argument '1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
[root@git01 ~/git_test]# git log -1
commit 61f463b1d4525d1ef7865e8410289341c1f49c76
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 02:30:25 2020 +0800

    abc.html
[root@git01 ~/git_test]# echo "aaa">>23.txt 
[root@git01 ~/git_test]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	renamed:    123.txt -> 23.txt
#	new file:   news.html
#
# 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:   23.txt
#	modified:   news.html
#
[root@git01 ~/git_test]# git checkout -- 23.txt #从暂存区覆盖到工作目录
[root@git01 ~/git_test]# cat 23.txt 
123456

[root@git01 ~/git_test]# git diff --cached  #比对暂存区和本地仓库的内容
diff --git a/123.txt b/123.txt
deleted file mode 100644
index 9f358a4..0000000
--- a/123.txt
+++ /dev/null
@@ -1 +0,0 @@
-123456
diff --git a/23.txt b/23.txt
new file mode 100644
index 0000000..1f9167b
--- /dev/null
+++ b/23.txt
@@ -0,0 +1,2 @@
+123456
+bbb
diff --git a/news.html b/news.html
new file mode 100644
index 0000000..1a137db
--- /dev/null
+++ b/news.html
@@ -0,0 +1 @@
+7894562133

[root@git01 ~/git_test]# git log --oneline  #显示commit信息
61f463b abc.html
835bda5 abc.html
ee87ed8 news.html
65f627f  new file 789.txt

# Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式
# 版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录

[root@git01 ~/git_test]# git reset --hard 65f627f  #回滚
HEAD is now at 65f627f  new file 789.txt
[root@git01 ~/git_test]# git reset --hard 835bda5
HEAD is now at 835bda5 abc.html
# 刚刚的操作实际上就是改变了一下HEAD版本指针的位置,就是你将HEAD指针放在那里,那么你的当前工作版本就会定位在那里,要想把内容再还原到最新提交的版本,先看查看下提交版本号

[root@git01 ~/git_test]# git reflog  #git reflog 可查看总历史内容
835bda5 HEAD@{0}: reset: moving to 835bda5
65f627f HEAD@{1}: reset: moving to 65f627f
61f463b HEAD@{2}: commit: abc.html
835bda5 HEAD@{3}: commit: abc.html
ee87ed8 HEAD@{4}: commit: news.html
65f627f HEAD@{5}: commit (initial): new file 789.txt

三、Git常用命令

分支操作
1. git branch 创建分支
2. git branch -b 创建并切换到新建的分支上
3. git checkout 切换分支
4. git branch 查看分支列表
5. git branch -v 查看所有分支的最后一次操作
6. git branch -vv 查看当前分支
7. git brabch -b 分支名 origin/分支名 创建远程分支到本地
8. git branch --merged 查看别的分支和当前分支合并过的分支
9. git branch --no-merged 查看未与当前分支合并的分支
10. git branch -d 分支名 删除本地分支
11. git branch -D 分支名 强行删除分支
12. git branch origin :分支名 删除远处仓库分支
13. git merge 分支名 合并分支到当前分支上

暂存操作
1. git stash 暂存当前修改
2. git stash apply 恢复最近的一次暂存
3. git stash pop 恢复暂存并删除暂存记录
4. git stash list 查看暂存列表
5. git stash drop 暂存名(例:stash@{0}) 移除某次暂存
6. git stash clear 清除暂存

回退操作
1. git reset --hard HEAD^ 回退到上一个版本
2. git reset --hard ahdhs1(commit_id) 回退到某个版本
3. git checkout -- file撤销修改的文件(如果文件加入到了暂存区,则回退到暂存区的,如果文件加入到了版本库,则还原至加入版本库之后的状态)
4. git reset HEAD file 撤回暂存区的文件修改到工作区

标签操作
1. git tag 标签名 添加标签(默认对当前版本)
2. git tag 标签名 commit_id 对某一提交记录打标签
3. git tag -a 标签名 -m '描述' 创建新标签并增加备注
4. git tag 列出所有标签列表
5. git show 标签名 查看标签信息
6. git tag -d 标签名 删除本地标签
7. git push origin 标签名 推送标签到远程仓库
8. git push origin --tags 推送所有标签到远程仓库
9. git push origin :refs/tags/标签名 从远程仓库中删除标签

常规操作
1. git push origin test 推送本地分支到远程仓库
2. git rm -r --cached 文件/文件夹名字 取消文件被版本控制
3. git reflog 获取执行过的命令
4. git log --graph 查看分支合并图
5. git merge --no-ff -m '合并描述' 分支名 不使用Fast forward方式合并,采用这种方式合并可以看到合并记录
6. git check-ignore -v 文件名 查看忽略规则
7. git add -f 文件名 强制将文件提交

git创建项目仓库
1. git init 初始化
2. git remote add origin url 关联远程仓库
3. git pull
4. git fetch 获取远程仓库中所有的分支到本地

忽略已加入到版本库中的文件
1. git update-index --assume-unchanged file 忽略单个文件
2. git rm -r --cached 文件/文件夹名字 (. 忽略全部文件)

取消忽略文件
1. git update-index --no-assume-unchanged file

拉取、上传免密码
1. git config --global credential.helper store

四、Git分支

[root@git01 ~/git_test]# git branch #查看当前分支 (master 分支)
* master

[root@git01 ~/git_test]# git log  --oneline  --decorate #查看当前历史变化指向的分支
835bda5 (HEAD, master) abc.html
ee87ed8 news.html
65f627f  new file 789.txt
[root@git01 ~/git_test]# git branch test #创建一个新分支 test

[root@git01 ~/git_test]# git commit -m  'test create test.txt'   #提交到本地仓库  
[master 1d3fe1d] test create test.txt
 1 file changed, 2 insertions(+)
 create mode 100644 23.txt
[root@git01 ~/git_test]# git  merge  test  #合并分支
Already up-to-date.
[root@git01 ~/git_test]#  git log  --oneline  --decorate
1d3fe1d (HEAD, master) test create test.txt
835bda5 (test) abc.html
ee87ed8 news.html
65f627f  new file 789.txt

[root@git01 ~/git_test]# git branch -d  test  #删除分子
Deleted branch test (was 7cee7d4).
[root@git01 ~/git_test]# git branch
* master

五、Git标签

-a  指定标签名称  
-m   注释信息
[root@git01 ~/git_test]# git tag  -a v1.0  -m "modify a"    #给最近的一次操作打个标签
[root@git01 ~/git_test]# git  tag        #查看标签  
v1.0
[root@git01 ~/git_test]# git  tag  -a v0.9  a5ba74c -m "aaaaaa"        #根据某一次的历史记录进行打标签  
[root@git01 ~/git_test]# git tag
v0.9
v1.0
[root@git01 ~/git_test]# git show  v0.9        #详细查看标签内容

[root@git01 ~/git_test]# git  tag -d  v0.9        #删除标签

[root@git01 ~/git_test]# git show  v1.0   #查看标签信息
tag v1.0
Tagger: wxw <2466236198@qq.com>
Date:   Tue Apr 21 10:33:03 2020 +0800

modify a

commit 4593b12d770c1a4ba29ccc194fd6089f91c4fc80
Merge: cd56248 7cee7d4
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 10:28:39 2020 +0800

    aaaaa

diff --cc b
index b065a3f,dec2cbe..a024d0d
--- a/b
+++ b/b
@@@ -1,1 -1,2 +1,6 @@@
++<<<<<<< HEAD
 +1123
++=======
+ test
 -test
++
++>>>>>>> test
[root@git01 ~/git_test]# git  tag -d  v1.0  #删除标签
Deleted tag 'v1.0' (was 103ca50)


[root@git01 ~/git_test]# git log  --oneline 
4593b12 aaaaa
7cee7d4 modify test b
cd56248 modify master  b
1d3fe1d test create test.txt
835bda5 abc.html
ee87ed8 news.html
65f627f  new file 789.txt
[root@git01 ~/git_test]# git  tag  -a v0.9  4593b12 -m "aaaaaa" # 根据某历史记录打标签
[root@git01 ~/git_test]# git show v0.9
tag v0.9
Tagger: wxw <2466236198@qq.com>
Date:   Tue Apr 21 10:40:06 2020 +0800

aaaaaa

commit 4593b12d770c1a4ba29ccc194fd6089f91c4fc80
Merge: cd56248 7cee7d4
Author: wxw <2466236198@qq.com>
Date:   Tue Apr 21 10:28:39 2020 +0800

    aaaaa

diff --cc b
index b065a3f,dec2cbe..a024d0d
--- a/b
+++ b/b
@@@ -1,1 -1,2 +1,6 @@@
++<<<<<<< HEAD
 +1123
++=======
+ test
 -test
++
++>>>>>>> test

六、Git升级

#安装依赖软件
[root@git01 ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc gcc perl-ExtUtils-MakeMaker  -y
#卸载低版本git
[root@git01 ~]# yum remove git -y

#下载最新版本
[root@git01 ~]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.23.0.tar.xz
[root@git01 ~]#  cd /usr/local/src/
[root@git01 /usr/local/src]# mv ~/git-2.23.0.tar.xz ./
[root@git01 /usr/local/src]# tar xf git-2.23.0.tar.xz
[root@git01 /usr/local/src]# cd git-2.23.0
# make 编译安装新版 Git
[root@git01 /usr/local/src/git-2.23.0]# make prefix=/usr/local/git all
[root@git01 /usr/local/src/git-2.23.0]# make install prefix=/usr/local/git 
[root@git01 /usr/local/src/git-2.23.0]# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
[root@git01 /usr/local/src/git-2.23.0]# . /etc/profile
[root@git01 /usr/local/src/git-2.23.0]# git --version #查看新版Git
git version 2.23.0
原文地址:https://www.cnblogs.com/wangxiaopang/p/12743157.html