这个文章会简单的介绍一下Git,主要是想记录一下关于Git的使用,我们公司使用的是Gitlab 基于web的Git仓库,Git在平时使用上 开发用的会比较多,运维相对来说比较少,现在图形化工具也很方便,但总觉得命令也要多熟悉一些。
如果想更好的学习一下Git 可以看下这个网址:https://git-scm.com/book/zh/v2/
Git是什么
Git是版本控制系统,用于记录一个或多个文件内容的变化,方便查看特定版本修订情况的系统。最早期版本控制系统一般使用的是SVN ,这个是集中版本控制系统,是单一的集中管理的服务器,开发人员通过连接到这台服务器,取出最新的代码或者提交最新的代码,不过这种模式容易产生单点故障,如果服务器宕机,那这段时间,无法对代码进行更新,还原等 无法协同工作,一旦服务器数据丢失,没有代码的备份话,数据很难还原回去。
Git属于是分布式的版本控制系统,开发人员把代码拉去到本地,是把整个仓库所有数据全部拉去到本地,相当于是在本地备份一份,一旦服务器有故障产生,开发人员不会受到影响 只是不能push代码,开始可以继续开发。在使用中也可以很灵活的创建分支。且分支之间不受影响。
Git 的提交会有点繁琐,使用Git 要掌握相关的大量命令,在本地修改好代码后,需要先提交到暂存区,再pull到本地仓库 或者是远程仓库中。
Git命操作令
🍗 创建Git账号信息等
命令:git config 设置控制 Git 外观和行为的配置变量
--global 选项让 Git 读写此文件,这会对你系统上 所有 的仓库生效。
#创建用户
[T^T] host3 ~# git config --global user.name 'test'
#配置邮箱
[^-^] host3 ~# git config --global user.email 'test@qq.com'
# 显示颜色
[^-^] host3 ~# git config --global color.ui true
配置好之后会在当前用户的家目录生成一个配置文件
[^-^] host3 ~# cat .gitconfig
[user]
name = test
email = test@qq.com
[color]
ui = true
🍗 创建本地仓库
命令:git init 初始化文件目录 为一个git仓库目录
[^-^] host3 git# mkdir demo
[^-^] host3 git# cd demo/
[^-^] host3 demo# git init #初始化文件目录 为一个git仓库目录
Initialized empty Git repository in /usr/local/src/git/demo/.git/
初始化之后会生成一个.git的文件 这里面的文件不可以删除
[^-^] host3 demo# ls .git/
branches config description HEAD hooks info objects refs
🍗 提交文件到本地仓库
注意 Git提交文件分三个流程
在本地修改好代码之后 通过 git add . 将文件上传到暂存区,再通过 git commit 提交到本地仓库中
# 创建三个文件
[^-^] host3 demo# touch file1 file2 file3
# 查看在上次提交之后是否有对文件进行再次修改
[^-^] host3 demo# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# 有三个文件没有被追踪,使用 git add 把文件提交到暂存区中
# 注意:需要把文件提交到暂存区之后才能提交到仓库中
# file1
# file2
# file3
nothing added to commit but untracked files present (use "git add" to track)
# 将file1 添加暂存区中
# 批量将所有文件提交到暂存区 . 或 ./ 或./* file* 这样的不会生效
[^-^] host3 demo# git add file1
# 查看在上次提交之后是否有对文件进行再次修改
[^-^] host3 demo# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# 新增一个文件 如果要撤销的话可以使用 git rm
# new file: file1
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file2
# file3
注意 这里还没有把文件提交到仓库中,不能把这些文件管理起来,只是提交到暂存区了
将暂存区的文件提交到本地仓库
# 把暂存区的文件提交到仓库中
#-m参数 增加备注 对你上传的信息做个描述
[^-^] host3 demo# git commit -m "new file1-3"
[master (root-commit) 4c70f85] new file1-3
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
create mode 100644 file2
create mode 100644 file3
# 这时候就相当于是做了一次快照了,三个文件发生改变 0个插入,0个删除
这张图就很好的诠释了提交的流程
🍗 修改文件名并提交带Git上
命令:git mv [new name] [old name]
[^-^] host3 demo# git mv file file1
[^-^] host3 demo# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# renamed: file -> file1
# 注意 这里的状态 不是先删除再创建 是直接修改名字
[^-^] host3 demo# git commit -m 'renamed file>file1'
[master 9120b7c] renamed file>file1
1 file changed, 0 insertions(+), 0 deletions(-)
rename file => file1 (100%)
🍗 文件对比 [本地文件,暂存区,仓库]
命令:git diff name 本地文件和缓存区对比
# 因为文件内容都是相同的,所以没有返回值(本地和暂存区对比)
[^-^] host3 demo# git diff file1
# file1文件中添加数据
[^-^] host3 demo# echo "crazy" >> file1
# 差异化对比
[^-^] host3 demo# git diff file1
diff --git a/file1 b/file1
index abe4ae4..086cc8f 100644
--- a/file1 # 变动前的版本
+++ b/file1 # 变动后的版本
@@ -1 +1,2 @@
GuGuZai # 两个文件中共同的内容
+crazy # 新增加的信息
# 提交到暂存区
[^-^] host3 demo# git add .
# 再次比对 数据一致
[^-^] host3 demo# git diff file1
图解:
命令:git diff --cached 暂存区和本地仓库对比
# 暂存区和本地仓库的比对
[T^T] host3 demo# git diff --cached file1
diff --git a/file1 b/file1
index abe4ae4..086cc8f 100644
--- a/file1
+++ b/file1
@@ -1 +1,2 @@
GuGuZai
+crazy
[^-^] host3 demo# git commit -m 'new crazy ---> file1'
[master fb50fa0] new crazy ---> file1
1 file changed, 1 insertion(+)
[T^T] host3 demo# git diff --cached file
图解
🍗 查看git commit历史快照
命令:git log
[^-^] host3 demo# git log
commit fb50fa071bb9d619ca9cc809f27f594e434978b7 # 每一次提交都会生成一个编号
Author: test <test@qq.com> # 作者信息
Date: Sun Apr 3 15:46:40 2022 +0800 # 时间
new crazy ---> file1 # -m 提交时填写的描述信息
--oneline 选项来查看历史记录的简洁的版本
[^-^] host3 demo# git log --oneline
fb50fa0 new crazy ---> file1
9120b7c renamed file>file1
fd8d8bb renamed file1>file
95633f5 change file1 GuGuZai
4c70f85 new file1-3
也可以通过 添加-1 -2的参数 输出最近的几次提交信息
[^-^] host3 demo# git log -1
commit fb50fa071bb9d619ca9cc809f27f594e434978b7
Author: test <test@qq.com>
Date: Sun Apr 3 15:46:40 2022 +0800
new crazy ---> file1
🍗 文件回退
缓存区回退到本地目录
命令:git checkout name 缓存区回退到本地目录 前提是修改文件没有提交到暂存区的情况下
# 删除file1文件数据
[^-^] host3 demo# > file1
[T^T] host3 demo# 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: file1
#
no changes added to commit (use "git add" and/or "git commit -a")
[^-^] host3 demo# git checkout file1 暂存区的内容覆盖到本地目录
[^-^] host3 demo# cat file1
GuGuZai
crazy
本地仓库回退到目录
命令 git reset HEAD name 回退name文件的当前版本
HEAD 表示当前版本 | HEAD~0 表示当前版本 |
HEAD^ 上一个版本 | HEAD~1 上一个版本 |
HEAD^^ 上上一个版本 | HEAD^2 上上一个版本 |
HEAD^^^ 上上上一个版本 以此类推 | HEAD^3 上上上一个版本 |
(可以使用左边符号也可以使用右边数字表示版本)
命令:git checkout -- name 本地仓库回退本地目录
注意 他是先把仓库中的文件回退到暂存区,暂存区再回退到本地目录
# 本地仓库回退到本地目录
[^-^] host3 demo# > file1
[^-^] host3 demo# git add .
[^-^] host3 demo# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1
#
[^-^] host3 demo# git reset HEAD file1
Unstaged changes after reset:
M file1
[^-^] host3 demo# git checkout -- file1
[^-^] host3 demo# cat file1
GuGuZai
crazy
提交多次后回退某个版本
命令 git reset --hard commit id
命令 git reflog 查看提交历史 不会因为回退而被覆盖
命令 git log 查看 commit提交日志,会因为回退 日志被覆盖
[^-^] host3 demo# echo "new" >> file1
[T^T] host3 demo# git add .
[^-^] host3 demo# git commit -m 'new ---->file1'
[master fe9de50] new ---->file1
1 file changed, 1 insertion(+)
# 回退到GuGuZai
# 通过git log 查看对应的commit id
[^-^] host3 demo# git reset --hard 95633f5
HEAD is now at 95633f5 change file1 GuGuZai
[^-^] host3 demo# cat file1
GuGuZai
# 查看log 之前的提交记录都没了 可以通过reflog来查看
[^-^] host3 demo# git log
commit 95633f5fe09f831e72988b177c6b5815d1bfd298
Author: test <test@qq.com>
Date: Sun Apr 3 15:00:14 2022 +0800
change file1 GuGuZai
commit 4c70f85fbdf40e33092ec3c9c2357e6ae906e54e
Author: test <test@qq.com>
Date: Sun Apr 3 14:49:49 2022 +0800
new file1-3
[^-^] host3 demo# git reflog
95633f5 HEAD@{0}: reset: moving to 95633f5
fdb1d88 HEAD@{1}: commit: file2--->file2
fe9de50 HEAD@{2}: commit: new ---->file1
fb50fa0 HEAD@{3}: commit: new crazy ---> file1 # 剩下的我省略了
🍗 Git分支
分支之间互不影响 各自开发各自的 分支稳定后可以和其他分支进行合并
命令 git branch 查看分支
命令 git branch name 创建分支,名字为name
命令 git ls-files 查看当前分支上的所有文件
命令 git checkout name 切换到name分支
命令 git merge master 合并分支 把master分支合并到当前分支上
命令 git branch -d name 删除分支
# 查看分支
[^-^] host3 demo# git branch
* master
创建分支
[^-^] host3 demo# git branch devops
[^-^] host3 demo# git branch
devops
* master
# 切换分支
[^-^] host3 demo# git checkout devops
Switched to branch 'devops'
# 创建文件
[^-^] host3 demo# touch file4 file5 file 6
#提交所有文件后 查看devops分支文件
[^-^] host3 demo# git ls-files
file1
file2
file3
file4
file5
file6
# 查看master分支文件
[T^T] host3 demo# git ls-files
file1
file2
file3
# 在master 创建file 12 两个文件
# 合并代码 git merge
[^-^] host3 demo# git merge master -m 'devops 合并master内容
Merge made by the 'recursive' strategy.
12 | 0
file | 0
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 12
create mode 100644 file
# 如果不先把master的信息合并一下的话 devops直接往master合并 会报错 因为数据不一致
# 把devpos里面的数据合并到master上
[T^T] host3 demo# git merge devops
Updating 32d3866..ff71239
Fast-forward
10 | 0
9 | 0
file4 | 0
file5 | 0
file6 | 0
5 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 10
create mode 100644 9
create mode 100644 file4
create mode 100644 file5
create mode 100644 file6
# 删除devops分支
[^-^] host3 demo# git branch -d devops
Deleted branch devops (was ff71239).
[^-^] host3 demo# git branch
* master
🍗 Git打标签 tag
将一个好记的名称关联到不好记的commitID上
tag不会跟着commit的更新而更新 一个commitid 可以有多个标签
命令 git tag 查看当前标签有哪些
命令 git tag -a ‘v1.0’ -m' 第一版' -a 添加标签 -m 选项指定了一条将会存储在标签中的信息
命令 git tag -d v1.0 删除标签
[T^T] host3 demo# git tag
# 信息为空是因为 这个分支还没有标签
# 创建标签
[T^T] host3 demo# git tag -a "v1.0" -m "第一版 初始化"
[T^T] host3 demo# git tag
v1.0
# 查看标签的信息
[^-^] host3 demo# git show v1.0
tag v1.0
Tagger: test <test@qq.com>
Date: Mon Apr 4 14:47:36 2022 +0800
第一版 初始化
commit ff712395674a38621d046791a53bf7099ff929b5 commit添加上了一个标签
Merge: 64cb3fb 32d3866
Author: test <test@qq.com>
Date: Sun Apr 3 18:26:00 2022 +0800
devops 合并master内容
# 基于某一个commit来打标签 一个commit 可以打多个标签
[T^T] host3 demo# git tag -a 'v2.0' fdb1d88 -m 'file2--->file2'
[^-^] host3 demo# git tag
v1.0
v2.0
[^-^] host3 demo# git show v2.0
tag v2.0
Tagger: test <test@qq.com>
Date: Mon Apr 4 14:52:47 2022 +0800
file2--->file2
commit fdb1d884218f187c576217661aa253031d2069c0
Author: test <test@qq.com>
Date: Sun Apr 3 17:35:45 2022 +0800
file2--->file2
diff --git a/file2 b/file2
index e69de29..6c493ff 100644
--- a/file2
+++ b/file2
@@ -0,0 +1 @@
+file2
# 删除tag标签
[^-^] host3 demo# git tag -d v2.0
Deleted tag 'v2.0' (was ab37b28)
[^-^] host3 demo# git tag
v1.0
🎉 以上就是本期的全部内容,如果觉得文章有用的话 可以点赞 评论 收藏 转发一条龙
如果觉得我的文对你有帮助的话 可以关注下我的公众号 一般写文第一时间发布到公众号中
公众号名称 咕咕崽
二维码: