分布式版本控制系统Git使用

Git介绍

Git(读音为/gɪt/。)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 [1] Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。
在这里插入图片描述在这里插入图片描述

Git基本操作

步骤一:部署Git服务器
1)YUM安装Git软件。

[root@git ~]# yum -y install git

2)初始化一个空仓库。

[root@git ~]# mkdir /var/git
[root@git ~]# git init /var/git/project --bare 
[root@git ~]# ls /var/git/project
config  description  HEAD  hooks  info  objects  refs   

步骤二:客户端测试
在这里插入图片描述

  1. clone克隆服务器仓库到本地。
[root@client ~]# yum -y install git
[root@client ~]# git clone root@192.168.2.100:/var/git/project 
[root@client ~]# cd project
[root@client ~]# ls 
  1. 修改git配置。
[root@client project]# git config --global user.email "you@example.com"
[root@client project]# git config --global user.name "Your Name"
[root@client project]# cat ~/.gitconfig 
[user]
    email = you@example.com
    name = Your Name

3) 本地工作区对数据进行增删改查(必须要先进入仓库再操作数据)。

[root@client project]# echo "init date" > init.txt
[root@client project]# mkdir demo
[root@client project]# cp /etc/hosts demo

4) 查看仓库中数据的状态。

[root@client project]# git status

5) 将工作区的修改提交到暂存区。

[root@client project]# git add .
  1. 将暂存区修改提交到本地仓库。
[root@client project]# git commit  -m  "注释,可以为任意字符"
[root@client project]# git status

7) 将本地仓库中的数据推送到远程服务器。

[root@client project]# git config --global push.default simple
[root@client project]# git push
root@192.168.2.100's password:  输入服务器root密码
[root@client project]# git status
  1. 将服务器上的数据更新到本地。
    备注:可能其他人也在修改数据并提交服务器,就会导致自己的本地数据为旧数据,使用pull就可以将服务器上新的数据更新到本地。
[root@client project]# git pull
  1. 查看版本日志。
[root@client project]# git log
[root@client project]# git log --pretty=oneline
[root@client project]# git log --oneline
[root@client project]# git reflog

HEAD指针操作

HEAD指针是一个可以在任何分支和版本移动的指针,通过移动指针我们可以将数据还原至任何版本。每做一次提交操作都会导致git更新一个版本,HEAD指针也跟着自动移动。
在这里插入图片描述

  1. 查看Git版本信息。
[root@client project]# git reflog
[root@client project]# git log --oneline
04ddc0f num.txt:789
7bba57b num.txt:456
301c090 num.txt:123
b427164 new.txt:third
0584949 new.txt:second
ece2dfd new.txt:first line
e1112ac add new.txt
1a0d908 初始化

2)移动HEAD指针,将数据还原到任意版本。
提示:当前HEAD指针为HEAD@{0}。

[root@client project]# git reset --hard 301c0
[root@client project]# git reflog
301c090 HEAD@{0}: reset: moving to 301c0
04ddc0f HEAD@{1}: commit: num.txt:789
7bba57b HEAD@{2}: commit: num.txt:456
301c090 HEAD@{3}: commit: num.txt:123
b427164 HEAD@{5}: commit: new.txt:third
0584949 HEAD@{6}: commit: new.txt:second
ece2dfd HEAD@{7}: commit: new.txt:first line
e1112ac HEAD@{8}: commit: add new.txt
1a0d908 HEAD@{9}: commit (initial): 初始化
[root@client project]# cat num.txt                #查看文件是否为123
123
[root@client project]# git reset --hard 7bba57b
[root@client project]# cat num.txt                #查看文件是否为123,456
123
456
[root@client project]# git reflog                #查看指针移动历史
7bba57b HEAD@{0}: reset: moving to 7bba57b
301c090 HEAD@{1}: reset: moving to 301c0
… …
[root@client project]# git reset --hard 04ddc0f    #回到最后一次修改的版本

Git分支操作

Git支持按功能模块、时间、版本等标准创建分支,分支可以让开发分多条主线同时进行,每条主线互不影响.
常见的分支规范如下:

  • MASTER分支(MASTER是主分支,是代码的核心)。
  • DEVELOP分支(DEVELOP最新开发成果的分支)。
  • RELEASE分支(为发布新产品设置的分支)。
  • HOTFIX分支(为了修复软件BUG缺陷的分支)。
  • FEATURE分支(为开发新功能设置的分支)。
    在这里插入图片描述
    步骤一:查看并创建分支
    1)查看当前分支。
[root@client project]# git status  //On branch master
nothing to commit, working directory clean
[root@client project]# git branch -v
\*   master 0dc2b76 delete init.txt

2)创建分支。

[root@client project]# git branch hotfix
[root@client project]# git branch feature
[root@client project]# git branch -v
  feature 0dc2b76 delete init.txt
  hotfix  0dc2b76 delete init.txt
* master  0dc2b76 delete init.txt

步骤二:切换与合并分支
1)切换分支。

[root@client project]# git checkout hotfix
[root@client project]# git branch -v
  feature 0dc2b76 delete init.txt
* hotfix  0dc2b76 delete init.txt
master  0dc2b76 delete init.txt

2)在新的分支上可以继续进行数据操作(增、删、改、查)。

[root@client project]# echo "fix a bug" >> new.txt
[root@client project]# git add .
[root@client project]# git commit -m "fix a bug"

3)将hotfix修改的数据合并到master分支。
注意,合并前必须要先切换到master分支,然后再执行merge命令。

[root@client project]# git checkout master
[root@client project]# cat new.txt        #默认master分支中没有hotfix分支中的数据
[root@client project]# git merge hotfix
Updating 0dc2b76..5b4a755
Fast-forward
 new.txt | 1 ++
 1 file changed, 1 insertions(+)

步骤二:解决版本分支的冲突问题
1)在不同分支中修改相同文件的相同行数据,模拟数据冲突。

[root@client project]# git checkout hotfix
[root@client project]# echo "AAA" > a.txt
[root@client project]# git add .
[root@client project]# git commit -m "add a.txt by hotfix"
[root@client project]# git checkout master
[root@client project]# echo "BBB" > a.txt
[root@client project]# git add .
[root@client project]# git commit -m "add a.txt by master"
[root@client project]# git merge hotfix
自动合并 a.txt
冲突(添加/添加):合并冲突于 a.txt
自动合并失败,修正冲突然后提交修正的结果。

2)查看有冲突的文件内容,修改文件为最终版本的数据,解决冲突。

[root@client project]# cat a.txt                #该文件中包含有冲突的内容
<<<<<<< HEAD
BBB
=======
AAA
>>>>>>> hotfix
[root@client project]# vim a.txt              #修改该文件,为最终需要的数据,解决冲突
BBB
[root@client project]# git add .
[root@client project]# git commit -m "resolved"

访问Git服务器

Git支持很多服务器协议形式,不同协议的Git服务器,客户端就可以使用不同的形式访问服务器。创建的服务器协议有SSH协议、Git协议、HTTP协议。
步骤一:SSH协议服务器(支持读写操作)
1)创建基于密码验证的SSH协议服务器。

[root@git ~]# git init --bare /var/git/base_ssh
Initialized empty Git repository in /var/git/base_ssh/

2)客户端访问的方式(web2主机操作)。

[root@client ~]# git clone root@192.168.2.100:/var/git/base_ssh
[root@client ~]# rm -rf base_ssh

3)客户端生成SSH密钥,实现免密码登陆git服务器(web2主机操作)。

[root@client ~]# ssh-keygen -f /root/.ssh/id_rsa -N ''
[root@client ~]# ssh-copy-id  192.168.2.100
[root@client ~]# git clone root@192.168.2.100:/var/git/base_ssh
[root@client ~]# git push

步骤二:Git协议服务器(只读操作的服务器)
1)安装git-daemon软件包(web1主机操作)。

[root@git ~]# yum -y install git-daemon

2)创建版本库。

[root@git ~]# git init --bare /var/git/base_git
Initialized empty Git repository in /var/git/base_git/

3)修改配置文件,启动git服务。

[root@git ~]# vim /usr/lib/systemd/system/git@.service
修改前内容如下:
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/lib/git 
--export-all --user-path=public_git --syslog --inetd –verbose
修改后内容如下:
ExecStart=-/usr/libexec/git-core/git-daemon --base-path=/var/git 
--export-all --user-path=public_git --syslog --inetd –verbose
[root@git ~]# systemctl  start  git.socket

4)客户端访问方式

[root@client ~]# git clone git://192.168.2.100/base_git

步骤三:HTTP协议服务器(只读操作的服务器)
1)安装gitweb、httpd软件包。

[root@git ~]# yum -y install httpd gitweb

2)修改配置文件,设置仓库根目录。

[root@git ~]# vim +11 /etc/gitweb.conf 
$projectroot = "/var/git";                        #添加一行
  1. 创建版本仓库
[root@git ~]# git init --bare /var/git/base_http

4)启动httpd服务器

[root@git ~]# systemctl start httpd

5)客户端访问方式
注意:调用虚拟机中的firefox浏览器,需要在远程时使用ssh -X 服务器IP,并且确保真实主机的firefox已经关闭。

[root@client ~]# firefox http://192.168.2.100/git/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值