1、Git的介绍
git属于分布式版本控制系统
没有中心代码仓库,所有机器之间的地位同等(每台机器上都有相同的代码)
客户端并不只提取最新版本的文件,而是把原始的代码仓库完整地克隆下来。
优点:
a.由于任何人每次提取操作,实际上都是一次对代码仓库的完整备份,因此近乎所有的操作都可以在本地执行,速度就是相当的快,并且可以在网络断开的时候操作仍然不受影响,可以频繁的进行提交更新,等到有网络的时候再上传到远程的仓库就可以了。
b.git的分支模型,相当的轻量级,被称为“必杀技”。
1.git的部署
环境:
git-server 192.168.111.4 充当中心代码仓库服务器
client 192.168.111.9 git可视化工具
所有机器关闭防火墙和selinux:systemctl stop firewalld && setenforce 0
因为Git是分布式版本控制系统,所以,每个机器都必须注册:你的名字和Email地址。
注:git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置。
&*git-server
[root@git-server ~]# yum install -y git
[root@git-server ~]# git --version
git version 1.8.3.1
[root@git-server ~]# git config --global user.email "tom@163.com" #邮箱
[root@git-server ~]# git config --global user.name "tom" #用户
&*client
[root@client ~]# yum install -y git
[root@client ~]# git config --global user.email "jack@163.com"
[root@client ~]# git config --global user.name "jack"
# cat /root/.gitconfig #查看邮箱信息
# git config --global color.ui true #语法高亮
# git config --list #查看全局配置
1.创建版本库
创建一个空目录:在中心服务器上创建
[root@git-server ~]# mkdir /git-test
[root@git-server ~]# useradd git #创建一个git用户用来运行git
[root@git-server ~]# passwd git #给用户设置密码git
[root@git-server ~]# cd /git-test/
2.创建裸库
[root@git-server git-test]# git init --bare testgit
Initialized empty Git repository in /git-test/testgit/
[root@git-server ~]# chown git.git /git-test -R #修改权限
仓库创建完成后查看库目录:
[root@git-server git-test]# cd testgit/
[root@git-server testgit]# ls
branches config description HEAD hooks info objects refs
3.client克隆库
先配置免密登录
[root@client ~]# ssh-keygen #生成秘钥
[root@client ~]# ssh-copy-id -i git@192.168.111.4 #将秘钥传输到git服务器中的git用户
克隆git仓库
[root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
Cloning into 'testgit'...
warning: You appear to have cloned an empty repository.
[root@client ~]# ls #查看仓库已经克隆下来了
anaconda-ks.cfg testgit
2.git的使用
1.创建文件模拟代码提交到仓库
1.在testgit目录下创建一个测试文件test.txt
[root@client ~]# cd testgit/
[root@client testgit]# vim test.txt #随便写点东西
2.把文件添加到暂存区:使用 "git add" 建立跟踪
[root@client testgit]# git add test.txt
注: 这里可以使用 git add * 或者 git add -A
3.提交文件到仓库分支
[root@client testgit]# git commit -m "test1"
[master (root-commit) 2b51ff9] test1
1 file changed, 2 insertions(+)
create mode 100644 test.txt
注:-m:描述
4.查看git状态
[root@client testgit]# git status
# On branch master #分支位于master
5.修改文件后再此查看状态:
[root@client testgit]# echo '1122334' >> test.txt
[root@client testgit]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
# (使用 "git add <file>..." 更新要提交的内容)
# (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
# 修改: readme.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit "
6.先add再次提交commit
[root@client testgit]# git add -A
[root@client testgit]# git commit -m "add2"
[master 73bf688] add2
1 file changed, 1 insertion(+)
[root@client testgit]# git status
# On branch master
nothing to commit, working directory clean
2.版本回退
1.查看版本
[root@client testgit]# git log
显示的哪个版本在第一个就是当前使用的版本。
2.切换指定版本(根据版本号)
[root@vm20 gittest]# git reflog
2a85982 HEAD@{0}: reset: moving to 2a859821a2385e136fe83f3a206b287eb0eb8c18
f5bc8c1 HEAD@{1}: commit: test-version2
2a85982 HEAD@{2}: commit (initial): test-version1
[root@client testgit]# git reset --hard f5bc8c1
HEAD is now at f5bc8c1 test-version2
3.删除文件
在工作区的文件可直接删除,若文件在暂存区 则需要先从暂存区移除再删除
[root@client testgit]# touch test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@client testgit]# git add test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.txt
#
[root@client testgit]# git rm --cache test.txt #从暂存区移除
rm 'test.txt'
[root@client testgit]# ls
test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@client testgit]# rm -rf test.txt
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
3.将代码上传到仓库的master分支
[root@client testgit]# vim a.txt #创建一个新文件
[root@client testgit]# cat a.txt
hello world
[root@client testgit]# git add a.txt
[root@client testgit]# git commit -m "add"
[root@client testgit]# git push origin master #上传到中心仓库master分支
Counting objects: 11, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (11/11), 828 bytes | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To git@192.168.246.214:/git-test/testgit/
* [new branch] master -> master
3.测试
在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件
[root@client testgit]# cd
[root@client ~]# rm -rf testgit/
[root@client ~]# git clone git@192.168.111.4:/git-test/testgit/
Cloning into 'testgit'...
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 11 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (11/11), done.
[root@client ~]# cd testgit/
[root@client testgit]# ls
a.txt
[root@client testgit]# cat a.txt
hello world