git版本控制器

一、版本控制系统/版本控制器git

1. 版本控制系统:

git   分布式  —没有中心代码库,所有机器之间的地位同等(每台机器上都有相同的代码)

image.png

svn  集中管理的 —有中心代码库,其他都是客户端

image.png

2.git与svn介绍

1.git属于分布式版本控制系统:
客户端并不只提取最新版本的文件,而是把原始的代码仓库完整地克隆下来。
优点:
a.由于任何人每次提取操作,实际上都是一次对代码仓库的完整备份,因此近乎所有的操作都可以在本地执行,速度就是相当的快,并且可以在网络断开的时候操作仍然不受影响,可以频繁的进行提交更新,等到有网络的时候再上传到远程的仓库就可以了。
b.git的分支模型,相当的轻量级,被称为“必杀技”。

缺点:
a.每个开发人员都拥有所有的代码,不利于核心代码的保密(如果有重要代码需要保密,则不建议使用git)敬业---竞业
2.svn属于集中化的版本控制系统:123
有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的成员通过客户端连接到这台服务器,进行文件上传和更新。

优点:
a.使用简单,比较符合我们的常规思维
b.同步代码比较简单,只要一步操作即可。

缺点:
a.丢失数据的风险:最显而易见的,由于集中化的特点,如果版本库的服务器磁盘发生故障等,你不能保证所有的数据已经有人提取出来了,最坏的情况是彻底的丢失整个项目的所有历史更改记录。
b.网络中断的情况下,协作就无法进行了,因为无法连接服务器进行上传和更新。

3.git 相关概念—纯命令行

工作区
版本库
暂存区
HEAD
版本号
版本日志
1.工作区(Working Directory)
存放git版本仓库的目录就是工作区(放源代码的地方)

2.暂存区:
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,(会将代码添加到缓存区,没有问题之后在提交到版本库)

3.版本库(Repository)
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。

4.HEAD:指向你最近一次提交后的结果。(现在是哪个版本,头就会指向这个版本)

image.png

image.png

1.添加文件到暂存区:

创建两个文件add到stage:
#git add  文件名
或者
#git add  *

从暂存区(stage)提交到当前master分支的HEAD:

git commit -m “版本描述信息”          #提交暂存区里的修改到版本库的分支

版本号:最核心用的是id号。每个版本都会有一个id号,也就是commit id,

查看版本号:
版本号可以通过版本日志查看

commit完成的功能类似快照,可以使用git log查看每次的commit记录

[root@vm20 gittest]# git log
commit fbecfa3d04ae5038aa11bf55942e46c840077ace                  //id号

二、git部署

环境:
    git-server    192.168.246.214  充当中央服务器
    client        192.168.246.213

所有机器关闭防火墙和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仓库都会使用这个配置。

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

创建版本库:

1.创建一个空目录**:**在中心服务器上创建

[root@git-server ~]# mkdir /git-test
[root@git-server ~]# useradd git   #创建一个git用户用来运行git
[root@git-server ~]# passwd git  #给用户设置密码
[root@git-server ~]# cd /git-test/

2.通过git init命令把这个目录变成Git可以管理的仓库:

 第1种情况:可以改代码,还能上传到别人的机器,别人也能从你这里下载但是别人不能上传代码到你的机器上。
 第2种情况:只是为了上传代码用,别人从这台机器上下载代码也可以上传代码到这台机器上,经常用于核心代码库。

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

git init --bare  库名字

创建一裸库

[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  #修改权限
2.仓库创建完成后查看库目录:
[root@git-server git-test]# cd testgit/
[root@git-server testgit]# ls
branches  config  description  HEAD  hooks  info  objects  refs
1.客户端
1.配置免密登录
[root@client ~]# ssh-keygen    #生成秘钥
[root@client ~]# ssh-copy-id -i git@192.168.246.214   #将秘钥传输到git服务器中的git用户
2.克隆git仓库
[root@client ~]# yum install -y git
[root@client ~]# git clone git@192.168.246.214:/git-test/testgit/
Cloning into 'testgit'...
warning: You appear to have cloned an empty repository.
[root@client ~]# ls  #查看仓库已经克隆下来了
anaconda-ks.cfg    testgit

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.提交文件到仓库分支:1
[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
[root@client testgit]# git add -A
8.再次提交commit:
[root@client testgit]# git commit  -m "add2" test.txt 
[master 73bf688] add2
 1 file changed, 1 insertion(+)
 [root@client testgit]# git status 
# On branch master
nothing to commit, working directory clean
2、版本回退

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

查看现在的版本:

[root@client testgit]# git log
显示的哪个版本在第一个就是当前使用的版本。

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

回到上一个版本

[root@client testgit]# git reset --hard HEAD^ 
HEAD is now at 0126755 test1
2.回到指定的版本(根据版本号): 
[root@client testgit]# git reset --hard dd66ff
HEAD is now at dd66ff9 add2
==========================================================
注:消失的ID号:
回到早期的版本后再查看git log会发现最近的版本消失,可以使用reflog查看消失的版本ID,用于回退到消失的版本
[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@git-client testgit]# git reset --hard f5bc8c1
3、删除文件

从工作区删除test.txt,并且从版本库一起删除

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
工作区:
[root@client testgit]# touch test.txt
[root@client testgit]# git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       qf.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
nothing to commit, working directory clean

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
已从工作区提交到暂存区:
第一种方法
[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)

第二种方法:
[root@client testgit]# touch  b.txt
[root@client testgit]# git add b.txt 
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   b.txt
#
[root@client testgit]# git rm -f b.txt 
rm 'b.txt'
[root@client testgit]# ls
[root@client testgit]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值