目录
git概述
1.版本概述
2.git状态
- 已提交(committed)表示该文件已经被安全地保存在本地数据库中了
- 已修改(modified) 表示修改了某个文件,但还没有提交保存
- 已暂存(staged) :表示把已修改的文件放在下次提交时要保存的清单中
3.git基础配置
1.安装git版本控制软件
yum install -y git
cd /etc/bash_completion.d/
source ./git
2.设置用户信息,如用户名、email等
git config --global user.name "csp"
git config --global user.email "1440350254@qq.com"
3.设置默认编辑器为vim
git config --global core.editor vim
4.查看用户配置
在哪个用户执行 就在哪个用户查看
cat .gitconfig
[core]
editor = vim
[user]
name = csp
email = 1440350254@qq.com
git应用基础
1.管理仓库
1.创建仓库
- 尚不存在项目时,可以直接创建
git init devops
初始化空的 Git 版本库于 /root/devops/.git/
[root@node1 ~]# ls
anaconda-ks.cfg devops mariadb-repo.tar.gz rabbitmq-repo.tar.gz
显然自动创建一个项目
- 在已有项目的目录中创建仓库
[root@node1 ~]# cd web
[root@node1 web]# git init
初始化空的 Git 版本库于 /root/web/.git/
- 仓库说明
初始化后,在当前目录下会出现一个名为.git 的目录·所有Git需要的数据和资源都存放在这个目录中
目前仅仅是按照既有的结构框架,初始化好了仓库中所有的文件和目录
还没有开始跟踪管理项目中的任何一个文件
2.从先有仓库克隆
直接克隆
[root@localhost ~]# git clone https://gitee.com/cym1102/nginxWebUI.git
[root@node1 git]# ls
devops nginxWebUI web
克隆时,指定本地目录
[root@localhost ~]# git clone https://gitee.com/cym1102/nginxWebUI.git mynginx
[root@node1 git]# ls
devops mynginx nginxWebUI web
2.记录更新到仓库
1.文件状态
1.工作目录下的所有文件都不外乎这两种状态:已跟踪或未跟踪
2.已跟踪的文件是指被纳入版本控制管理的文件,在上次快照中有它们的记录,工作一段时间后,它们的状态可能是未更新,已修改或者已放入暂存区
3.所有其他文件都属于未跟踪文件
2.文件状态生命周期
3.检查状态
查看拉取的项目
[root@node1 nginxWebUI] git status
# 位于分支 master
无文件要提交,干净的工作区
查看自己创建项目
[root@node1 web] git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# index.html
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
4.跟踪文件
[root@node1 web] git add index.html
[root@node1 web] git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
# (使用 "git rm --cached <file>..." 撤出暂存区)
#
# 新文件: index.html
#
跟踪多个文件
git add .
[root@node1 web] git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
# (使用 "git rm --cached <file>..." 撤出暂存区)
#
# 新文件: index.html
# 新文件: index2.html
#
忽略文件(就行不被追踪)
·可以创建一个名为.gitignore的文..gitignore的格式规范如下件
所有空行或者以注释符号 # 开头的行都会被Git忽略。
可以使用标准的 glob模式匹配。
匹配模式最后跟反斜杠(/)说明要忽略的是目录。
要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反
不想11.txt这个文件被跟踪
[root@node1 web] git 11.txt
[root@node1 web] git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
# (使用 "git rm --cached <file>..." 撤出暂存区)
#
# 新文件: index.html
# 新文件: index2.html
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# 11.txt
[root@node1 web] echo 11.txt >> .gitignore
[root@node1 web] echo .gitignore >> .gitignore
[root@node1 web] git add .
[root@node1 web] git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
# (使用 "git rm --cached <file>..." 撤出暂存区)
#
# 新文件: index.html
# 新文件: index2.html
#
提交更新(两个不同提交方式)
执行完git commit 会弹出vim编辑器 直接写入project init 就会出现一下信息
[root@node1 web] git commit
[master(根提交) 03a2c4b] project init
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 index2.html
[root@node1 web] git status
# 位于分支 master
无文件要提交,干净的工作区
[root@node1 web]#
这种提交方式不会跳出vim 对提交方式说明
[root@node1 web]# echo "sadas">>index.html
[root@node1 web]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: index.html
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@node1 web]# git add .
[root@node1 web]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 修改: index.html
#
[root@node1 web] git commit -m "add index.html"
[master d698ac7] add index.html
1 file changed, 1 insertion(+)
[root@node1 web]# git status
# 位于分支 master
无文件要提交,干净的工作区
移除文件
[root@node1 web]# git rm index.html
rm 'index.html'
[root@node1 web]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 删除: index.html
#
[root@node1 web]# git commit -m "rm index.html"
[master 51709f2] rm index.html
1 file changed, 2 deletions(-)
delete mode 100644 index.html
[root@node1 web]# git status
# 位于分支 master
无文件要提交,干净的工作区
文件移动
[root@node1 web]# git mv index2.html index.html
[root@node1 web]# git status
# 位于分支 master
# 要提交的变更:
# (使用 "git reset HEAD <file>..." 撤出暂存区)
#
# 重命名: index2.html -> index.html
#
[root@node1 web]# git commit -m "mv file"
[master cc5f708] mv file
1 file changed, 0 insertions(+), 0 deletions(-)
rename index2.html => index.html (100%)
[root@node1 web]# git status
# 位于分支 master
无文件要提交,干净的工作区
查看提交历史
[root@node1 web]# git log
commit cc5f708c68323f8d3888d0e57905a3960a4ef7a9
Author: csp <1440350254@qq.com>
Date: Wed Nov 17 02:09:55 2021 -0500
mv file
commit 51709f29d1554165111de021a1837451f089e35a
Author: csp <1440350254@qq.com>
Date: Wed Nov 17 02:04:27 2021 -0500
rm index.html
commit d698ac795f308f7ceed81e60263361edc27e9ab9
Author: csp <1440350254@qq.com>
Date: Wed Nov 17 01:59:11 2021 -0500
add index.html
commit 03a2c4b0c0cdb6e458ab7f446fc5303d7c71d30d
Author: csp <1440350254@qq.com>
Date: Wed Nov 17 09:11:12 2021 -0500
project init
取消已暂存文件
文件修改后,错误的提交到暂存区
希望将文件撤出暂存区,但是保留其修改的内容
$git reset HEAD README.md
$ git status
# Changed but not updated:
#(use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in workingdirectory)
#
# modified: README.md#
取消对文件的修改
文件修改后,后悔所做出的修改·希望文件恢复为修改前的内容
$ git checkout -- README.MD
$git status
# On branch master
nothing to commit (working directory clean)