git版本控制工具
一、git版本控制工具
1、作用介绍
git, 版本控制器, 记录文本文件的版本变化,便于回退
典型的工具:
git
svn
2、git安装
2.1 安装git
[root@localhost ~]# yum install -y git
[root@localhost ~]# rpm -q git
git-1.8.3.1-25.el7_9.x86_64
2.2 初始化设置
[root@localhost ~]# git config --global color.ui true
二、git的使用
1、创建git仓库
# 一个仓库对应一个项目
[root@localhost ~]# mkdir /opt/gitlearn
[root@localhost ~]# cd /opt/gitlearn
[root@localhost gitlearn]#
[root@localhost gitlearn]# git init
Initialized empty Git repository in /opt/gitlearn/.git/
[root@localhost gitlearn]#
[root@localhost gitlearn]# ls -a
. .. .git
2、提交修改
# git add 文件名称
# git commit -m "说明信息"
3、查看git仓库的状态
# git status
4、版本回退
# git reflog
# git reset --hard <版本ID>
5、撤销修改
5.1 暂存区、工作区
git add 将修改记录到 暂存区
git commit 将修改记录到工作区
[root@localhost gitlearn]# 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: file01
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost gitlearn]#
[root@localhost gitlearn]# git add file01
[root@localhost gitlearn]#
[root@localhost gitlearn]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file01
#
[root@localhost gitlearn]#
[root@localhost gitlearn]# git commit -m "文件中添加3"
[master 6545b5c] 文件中添加3
1 file changed, 1 insertion(+)
[root@localhost gitlearn]#
[root@localhost gitlearn]# git status
# On branch master
nothing to commit, working directory clean
5.2 撤销未提交到暂存区
[root@localhost gitlearn]# git checkout -- file01
5.3 撤销提交到暂存区的修改
[root@localhost gitlearn]# git reset HEAD file01
Unstaged changes after reset:
M file01
[root@localhost gitlearn]# git checkout -- file01
5.4 撤销提交到工作区的修改
[root@localhost gitlearn]# git reflog
c2fec1a HEAD@{0}: commit: 文件中添加4
6545b5c HEAD@{1}: commit: 文件中添加3
b3d23b4 HEAD@{2}: reset: moving to b3d23b4
8cff443 HEAD@{3}: reset: moving to 8cff443
b3d23b4 HEAD@{4}: commit: 文件中添加2222
8cff443 HEAD@{5}: commit (initial): 文件中添加1111
[root@localhost gitlearn]# git reset --hard 6545b5c
三、git分支 branch
git仓库默认存在一个分支,名称master
支持按需创建分支
分支间是隔离的
1、查看分支
[root@localhost gitlearn]# git branch
* master
2、创建分支
[root@localhost gitlearn]# git branch release-v1.1
[root@localhost gitlearn]#
[root@localhost gitlearn]# git branch
* master
release-v1.1
3、删除分支
[root@localhost gitlearn]# git branch
dev
* master
release-v1.1
[root@localhost gitlearn]# git branch -D dev
Deleted branch dev (was 6545b5c).
[root@localhost gitlearn]#
[root@localhost gitlearn]# git branch
* master
release-v1.1
5、切换分支
[root@localhost gitlearn]# git checkout release-v1.1
Switched to branch 'release-v1.1'
[root@localhost gitlearn]#
[root@localhost gitlearn]# git branch
master
* release-v1.1
5、分支的合并
[root@localhost gitlearn]# git branch
* master
release-v1.1
[root@localhost gitlearn]# git merge release-v1.1
Updating 6545b5c..df15548
Fast-forward
file01 | 1 +
1 file changed, 1 insertion(+)
四、git服务器
1、公有git服务器
gitHub, www.github.com 国外
gitee, www.gitee.com 国内
# 下载仓库
# git clone xxxxxxxxxxxxxxxxxxxxxxxxxxx
# 上传
# git push -u origin master