搭建Git服务器

一丶快速搭建好一台Git服务器,并测试该版本控制软件,要求如下:

  • 安装Git软件
  • 创建版本库
  • 客户端克隆版本仓库到本地
  • 本地工作目录修改数据
  • 提交本地修改到服务器
步骤一:部署Git服务器(192.168.2.100作为远程git服务器)
1)YUM安装Git软件。

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

2)初始化一个空仓库。

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

步骤二:客户端测试(192.168.2.200作为客户端主机)

使用git常用指令列表如表-1所示。
表-1 git常用指令列表
在这里插入图片描述

1) clone克隆服务器仓库到本地。

[root@web2 ~]# yum -y install git
[root@web2 ~]# git clone root@192.168.2.100:/var/git/project
[root@web2 ~]# cd project
[root@web2 ~]# ls

2) 修改git配置。

[root@web2 project]# git config --global user.email “you@example.com”
[root@web2 project]# git config --global user.name “Your Name”
[root@web2 project]# cat ~/.gitconfig

[user]
    email = you@example.com
    name = Your Name
3) 本地工作区对数据进行增删改查(必须要先进入仓库再操作数据)。

[root@web2 project]# echo “init date” > init.txt
[root@web2 project]# mkdir demo
[root@web2 project]# cp /etc/hosts demo

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

[root@web2 project]# git status

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

[root@web2 project]# git add .

6) 将暂存区修改提交到本地仓库。

[root@web2 project]# git commit -m “注释,可以为任意字符”
[root@web2 project]# git status

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

[root@web2 project]# git config --global push.default simple
[root@web2 project]# git push
root@192.168.2.100’s password: 输入服务器root密码
[root@web2 project]# git status

8) 将服务器上的数据更新到本地(web1的数据更新到web2)。

备注:可能其他人也在修改数据并提交服务器,就会导致自己的本地数据为旧数据,使用pull就可以将服务器上新的数据更新到本地。
[root@web2 project]# git pull

9) 查看版本日志。

[root@web2 project]# git log
[root@web2 project]# git log --pretty=oneline
[root@web2 project]# git log --oneline
[root@web2 project]# git reflog

二丶HEAD指针操作

[root@web2 project]# git reset --hard 301c0
[root@web2 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@web2 project]# cat num.txt #查看文件是否为123
123
[root@web2 project]# git reset --hard 7bba57b
[root@web2 project]# cat num.txt #查看文件是否为123,456

123
456

[root@web2 project]# git reflog #查看指针移动历史

7bba57b HEAD@{0}: reset: moving to 7bba57b
301c090 HEAD@{1}: reset: moving to 301c0
… …

[root@web2 project]# git reset --hard 04ddc0f #回到最后一次修改的版本
[root@web2 project]# git log --oneline

三丶Git分支操作

步骤一:查看并创建分支
1)查看当前分支。

[root@web2 project]# git status
#On branch master
nothing to commit, working directory clean
[root@web2 project]# git branch -v

* master 0dc2b76 delete init.txt
2)创建分支。

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

  • master 0dc2b76 delete init.txt
步骤二:切换与合并分支
1)切换分支。

[root@web2 project]# git checkout hotfix
[root@web2 project]# git branch -v

feature 0dc2b76 delete init.txt
* hotfix  0dc2b76 delete init.txt
master  0dc2b76 delete init.txt
2)在新的分支上可以继续进行数据操作(增、删、改、查)。

[root@web2 project]# echo “fix a bug” >> new.txt
[root@web2 project]# git add .
[root@web2 project]# git commit -m “fix a bug”

3)将hotfix修改的数据合并到master分支。

注意,合并前必须要先切换到master分支,然后再执行merge命令。
[root@web2 project]# git checkout master
[root@web2 project]# cat new.txt #默认master分支中没有hotfix分支中的数据
[root@web2 project]# git merge hotfix
Updating 0dc2b76…5b4a755
Fast-forward
new.txt | 1 ++
1 file changed, 1 insertions(+)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值