git-部署

环境:
    server    192.168.19.129  充当中央服务器
    client        192.168.19.130

所有机器关闭防火墙和selinux

安装:所有机器都安装
   [root@git-server ~]# yum install -y git
   [root@git-server ~]# git --version 
   git version 1.8.3.1
   
准备:
    因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。
    注意git config命令的--global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置。

所有的机器都添加,只要邮箱和用户不一样就可以。   
[root@server ~]# git config --global user.email "chen@163.com"     ----设置邮箱
[root@server ~]# git config --global user.name "chen"              ----添加用户
[root@server ~]# cat /root/.gitconfig       #查看全局配置
[user]
	email = chen@163.com
	name = chen
[root@server ~]# git config --global color.ui true		#语法高亮
[root@server ~]# git config --list			#查看全局配置
user.email=chen@163.com
user.name=chen
color.ui=true

#一、git使用
创建版本库:

  1. 创建一个空目录:在中心服务器上创建
[root@server ~]# mkdir /git_test
[root@server ~]# useradd git   #创建一个git用户用来运行git
[root@server ~]# passwd git    #给用户设置密码
[root@server ~]# cd /git_test/
  1. 通过git init命令把这个目录变成Git可以管理的仓库:
    第1种情况:可以改代码,还能上传到别人的机器,别人也能从你这里下载但是别人不能上传代码到你的机器上。
    第2种情况:只是为了上传代码用,别人从这台机器上下载代码也可以上传代码到这台机器上,经常用于核心代码库。

创建裸库: 适用于作为远程中心仓库使用
创建裸库才可以从别处push(传)代码过来,使用–bare参数------裸
git init --bare 库名字

创建一个裸库

[root@server git_test]# git init --bare testgit
初始化空的 Git 版本库于 /git_test/testgit/
[root@server git_test]# chown git.git /git_test/ -R
[root@server git_test]# cd testgit/
[root@server testgit]# ls
branches  config  description  HEAD  hooks  info  objects  refs

###1. 客户端

#配置免密登录
[root@client ~]# ssh-keygen      #生成秘钥
[root@client ~]# ssh-copy-id -i git@192.168.19.129      #将秘钥传输到git服务器中的git用户

#克隆git仓库
[root@client ~]# yum install -y git
[root@client ~]# git clone git@192.168.19.129:/git_test/testgit
正克隆到 'testgit'...
warning: 您似乎克隆了一个空版本库。

[root@client ~]# ls    #查看仓库已经克隆下来了
testgit

#设置邮箱和用户
[root@client ~]# git config --global user.email "chao@163.com"
[root@client ~]# git config --global user.name "chao"

#####创建文件模拟代码提交到仓库

#在testgit目录下创建一个测试文件test.txt
[root@client ~]# cd testgit/
[root@client testgit]# vim test.txt
test

#把文件添加到暂存区:使用 "git add" 建立跟踪
[root@client testgit]# git add test.txt
注: 这里可以使用 git add * 或者 git add -A  添加所有文件

#提交文件到仓库分支
[root@client testgit]# git commit -m "test1"     # -m描述
[master(根提交) 7cb1388] test1
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

#查看git状态
[root@client testgit]# git status 
# 位于分支 master
无文件要提交,干净的工作区

#修改文件后再此查看状态
[root@client testgit]# echo '123' >> test.txt 

[root@client testgit]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	修改:      test.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

#先add
[root@client testgit]# git add *

#再次提交commit
[root@client testgit]# git commit -m "test2" test.txt
[master 1aac28a] test2
 1 file changed, 1 insertion(+)

###2. 版本回退
已经提交了不合适的修改到版本库时,想要撤销本次提交,使用版本回退,不过前提是没有推送到远程库。

#####查看现在的版本 (显示的哪个版本在第一个就是当前使用的版本。)

[root@client testgit]# git log
commit 1aac28a8dae77baa3f367193e86b58fbf8fe180b
Author: chao <chao@163.com>
Date:   Thu Mar 26 16:37:48 2020 +0800

    test2

commit 7cb138899165e6ea06d545bd0351d78af2b86c11
Author: chao <chao@163.com>
Date:   Thu Mar 26 16:35:00 2020 +0800

    test1

#####版本回退(切换)
在Git中,上一个版本就HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100(一般使用id号来恢复)

#回到上一个版本
[root@client testgit]# git reset --hard HEAD^
HEAD 现在位于 7cb1388 test1

#回到指定的版本(根据版本号)
[root@client testgit]# git reset --hard 1aac28
HEAD 现在位于 1aac28a test2

#注:消失的ID号:
回到早期的版本后再查看git log会发现最近的版本消失,可以使用reflog查看消失的版本ID,用于回退到消失的版本
[root@client testgit]# git reflog
1aac28a HEAD@{0}: reset: moving to 1aac28
7cb1388 HEAD@{1}: reset: moving to HEAD^
1aac28a HEAD@{2}: commit: test2
7cb1388 HEAD@{3}: commit (initial): test1

###3. 删除文件
#####1. 工作区:

[root@client testgit]# touch a.txt

[root@client testgit]# rm a.txt      #未添加到暂存区,可直接删除
rm:是否删除普通空文件 "a.txt"?y

[root@client testgit]# git status
# 位于分支 master
无文件要提交,干净的工作区

#####2. 已从工作区提交到暂存区:
方法一: git rm --cache 文件名(暂存区删除)

[root@client testgit]# touch a.txt

[root@client testgit]# git status 
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	a.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

[root@client testgit]# git add a.txt

[root@client testgit]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    a.txt
#

[root@client testgit]# git rm --cache a.txt     #暂存区删除
rm 'a.txt'

[root@client testgit]# ls
a.txt  test.txt

[root@client testgit]# git status 
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	a.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

[root@client testgit]# rm a.txt 
rm:是否删除普通空文件 "a.txt"?y

[root@client testgit]# git status 
# 位于分支 master
无文件要提交,干净的工作区

方法二:git rm -f 文件名 (文件和暂存区一起删除)

[root@client testgit]# touch b.txt

[root@client testgit]# git add b.txt

[root@client testgit]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    b.txt
#

[root@client testgit]# git rm -f b.txt     #暂存区和本地文件一起删除
rm 'b.txt'

[root@client testgit]# ls
test.txt

[root@client testgit]# git status 
# 位于分支 master
无文件要提交,干净的工作区

###4. 修改文件
暂存区和本地文件一起修改git mv 旧名称 新名称

[root@client testgit]# touch c.txt

[root@client testgit]# git status 
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	c.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

[root@client testgit]# git add c.txt

[root@client testgit]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    c.txt
#

[root@client testgit]# git mv c.txt d.txt

[root@client testgit]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    d.txt
#

[root@client testgit]# ls
d.txt  test.txt

[root@client testgit]# git rm -f d.txt
rm 'd.txt'

###5. 将代码上传到仓库的master分支

[root@client testgit]# vim a.txt
hello

[root@client testgit]# git add a.txt

[root@client testgit]# git commit -m "add"
[master 1381ca8] add
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

[root@client testgit]# git push origin master      #上传到中心仓库master分支
Counting objects: 9, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (9/9), 626 bytes | 0 bytes/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To git@192.168.19.129:/git_test/testgit
 * [new branch]      master -> master

测试:在客户端将仓库删除掉然后在克隆下来查看仓库中是否有文件

[root@client testgit]# cd

[root@client ~]# ls
testgit

[root@client ~]# rm -rf testgit/
[root@client ~]# ls

[root@client ~]# git clone git@192.168.19.129:/git_test/testgit
正克隆到 'testgit'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
接收对象中: 100% (9/9), done.

[root@client ~]# cd testgit/
[root@client testgit]# ls
a.txt  test.txt

[root@client testgit]# cat a.txt 
hello

#二、创建分支并且合并分支
每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支。截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支。HEAD严格来说不是指向提交,而是指向mastermaster才是指向提交的,所以,HEAD指向的就是当前分支。

注意:刚创建的git仓库默认的master分支要在第一次commit之后才会真正建立。然后先git add .添加所有项目文件到本地仓库缓存,再git commit -m "init commit"提交到本地仓库,之后就可以随心所欲地创建或切换分支了。

客户端操作:

[root@client ~]# git clone git@192.168.19.129:/git_test/testgit
正克隆到 'testgit'...
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
接收对象中: 100% (9/9), done.

[root@client ~]# cd testgit/

[root@client testgit]# git status 
# 位于分支 master
无文件要提交,干净的工作区

#创建分支
[root@client testgit]# git branch dev

[root@client testgit]# git branch      #查看分支 * 在哪 当前就在哪个分支
  dev
* maste

#切换分支
[root@client testgit]# git checkout dev 
切换到分支 'dev'

[root@client testgit]# git branch 
* dev
  master

#在dev分支创建一个文件
[root@client testgit]# ls
a.txt  test.txt

[root@client testgit]# vim b.txt
nihao

[root@client testgit]# git commit -m "add dev"
[dev b4c02bd] add dev
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

#切回master分支
[root@client testgit]# git checkout master 
切换到分支 'master'

[root@client testgit]# ls
a.txt  test.txt

#把dev分支的工作成果合并到master分支上
[root@client testgit]# git merge dev 
更新 1381ca8..b4c02bd
Fast-forward
 b.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

[root@client testgit]# ls
a.txt  b.txt  test.txt

#删除分支
[root@client testgit]# git branch -d dev
已删除分支 dev(曾为 b4c02bd)。

[root@client testgit]# git branch
* master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值