1.Git基本介绍
Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
2.Git的优势
1)大部分操作可以在本地完成,不需要联网
2)完整性保证
3)尽可能添加数据而不是删除或修改数据
4)分支操作非常快捷流畅
5)与Linux命令全面兼容
3.Git环境准备
1.安装Git
[root@git-10.0.0.3 ~]# yum install git -y
2.创建本地创库目录
[root@git-10.0.0.3 ~]# mkdir demo
3.初始化git(初始化本地仓库目录)
[root@git-10.0.0.3 ~]# cd demo/
[root@git-10.0.0.3 ~/demo]# git init
Initialized empty Git repository in /root/demo/.git/
4.在本地仓库创建新文件(代替要同步的文件)
[root@git-10.0.0.3 ~/demo]# mkdir file1
[root@git-10.0.0.3 ~/demo]# mkdir file2
[root@git-10.0.0.3 ~/demo]# ls
file1 file2
5.查看git的状态
[root@git-10.0.0.3 ~/demo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# file1
# file2
nothing added to commit but untracked files present (use "git add" to track)
6.提交文件缓存区再到本地仓库
[root@git-10.0.0.3 ~/demo]# git add .
[root@git-10.0.0.3 ~/demo]# git config --global user.name "Mr zhang" #表明你的名字
[root@git-10.0.0.3 ~/demo]# git config --global user.email 2811340178@qq.com #表你的邮箱
[root@git-10.0.0.3 ~/demo]# git commit -m "更新了两个文件"
# On branch master
nothing to commit, working directory clean
提交到本地仓库流程
7.修改仓库任意一个文件测试
[root@git-10.0.0.3 ~/demo]# vim file1
[root@git-10.0.0.3 ~/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")
8.将修改的文件提交到本地仓库
[root@git-10.0.0.3 ~/demo]# git commit -a -m "新增了一行内容"
[master e548efa] 新增了一行内容
1 file changed, 1 insertion(+)
9.查看提交本地仓库的记录
[root@git-10.0.0.3 ~/demo]# git log
commit e548efa3c9abaffe6f6220f72b0a1c25e05a564a
Author: Mr zhang <2811340178@qq.com>
Date: Fri Nov 1 13:56:18 2019 +0800
新增了一行内容
commit ce251c75a10832100c3a725314f5c4422c6d27bf
Author: root <root@git-10.0.0.3>
Date: Fri Nov 1 13:38:24 2019 +0800
更新了两个文件
4.文件对比
1.本地文件和缓存区文件对比
[root@git-10.0.0.3 ~/demo]# git diff file1
2.暂存区和本地仓库对比
[root@git-10.0.0.3 ~/demo]# git diff --cached file1
5.文件回退
1.暂存区覆盖本地文件
[root@git-10.0.0.3 ~/demo]# > file1
[root@git-10.0.0.3 ~/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")
[root@git-10.0.0.3 ~/demo]# git checkout -- file1
[root@git-10.0.0.3 ~/demo]# cat file1
wewe we wqe we
2.提交到了本地仓库,恢复文件
[root@git-10.0.0.3 ~/demo]# > file1
[root@git-10.0.0.3 ~/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")
[root@git-10.0.0.3 ~/demo]# git reset HEAD file1
Unstaged changes after reset:
M file1
[root@git-10.0.0.3 ~/demo]# git checkout -- file1
[root@git-10.0.0.3 ~/demo]# cat file1
wewe we wqe we
3.如果多次修改文件,进行了多次提交
[root@git-10.0.0.3 ~/demo]# vim file1
[root@git-10.0.0.3 ~/demo]# git add .
[root@git-10.0.0.3 ~/demo]# git commit -m "更新了2行内容"
[master a6918f2] 更新了2行内容
1 file changed, 3 insertions(+), 1 deletion(-)
[root@git-10.0.0.3 ~/demo]# vim file1
[root@git-10.0.0.3 ~/demo]# git add .
[root@git-10.0.0.3 ~/demo]# git commit -m "更新了1行内容"
[master d12bd23] 更新了1行内容
1 file changed, 2 insertions(+), 1 deletion(-)
[root@git-10.0.0.3 ~/demo]# git log --oneline
d12bd23 更新了1行内容
a6918f2 更新了2行内容
e548efa 新增了一行内容
ce251c7 更新了两个文件
[root@git-10.0.0.3 ~/demo]# git reset --hard e548efa
HEAD is now at e548efa 新增了一行内容
[root@git-10.0.0.3 ~/demo]# cat file1
wewe we wqe we
[root@git-10.0.0.3 ~/demo]# git reflog
e548efa HEAD@{0}: reset: moving to e548efa
d12bd23 HEAD@{1}: commit: 更新了1行内容
a6918f2 HEAD@{2}: commit: 更新了2行内容
e548efa HEAD@{3}: commit: 新增了一行内容
ce251c7 HEAD@{4}: commit (initial): 更新了两个文件
恢复文件流程