Git-版本控制器

一、简介

Git 是一款开源的分布式版本控制系统,可以有效、高效的处理从很小到非常大的项目版本管理。Git 是 Linux Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源代码的版本控制软件。
官网地址

1.控制器种类

版本控制器就是用来存放代码的工具
GIT:分布式版本控制器
分布式版本控制器是没有“中央服务器”的,每个人的电脑上都是一个完整的版本库,这样在工作时就不需要联网了,因为版本库就在自己的电脑上,在多人协作时,只需要把各自的修改推送给对方,就可以互相看到对方的修改了
和集中式版本控制器相比,分布式版本控制器的安全性显然是比较好的,因为每个人的电脑都有完整的版本库,当一台电脑损坏不要紧只需要从别人电脑在复制一份就行,但是集中式的版本控制器一旦损坏,代码就会丢失
在实际使用分布式版本控制器时,很少在两人之间互相推送版本库,因为可能两个人不在一个局域网内,所以分布式版本控制器也有一个“中央服务器”,但是这个中央服务器仅仅是用来方便交换大家的修改,并不像集中式版本控制器那样中央服务器一坏数据就丢失

SVN:集中式版本控制器
CVS和SVN都是集中式的版本控制系统
集中式版本控制系统,版本库是集中存放在中央服务器的,但是因为工作使用的是自己的电脑,所以在工作之前需要从中央服务器获取最新的代码,并且在工作完成后需要再上传到中央服务器
集中式版本控制器的最大缺点就是需要联网才能工作,如果是局域网还行,带宽大,速度够大,可是如果在互联网上,遇到网速慢的话传一个10M的文件可能都需要五分钟,工作效率太慢,尤其是都是下班的时候才会上传代码,在下班点上传的请求更多,导致上传速度更慢

2.Git特点

Git优点:

  • 适合分布式开发,强调个体;
  • 公共服务器压力和数据量都不会太大;
  • 速度快、灵活;
  • 离线工作;

Git缺点:

  • 代码保密性差,一旦开发者把整个库克隆下来就可以完全公开所有代码和版本信息;
  • 权限控制不友好;如果需要对开发者限制各种权限的建议使用 SVN。

3.Git工作流程

自己创建编写的文件是工作区,在此编写代码,这个文件会上传到暂存区,紧接着会上传到本地仓库,也是可以随时取消。最后是可以把代码上传到远程仓库,分享供大家访问且使用。

4.Git 核心概念

对于任何一个文件,Git对其都有四种状态,

  • 工作目录:就是你平常存放项目代码的地方。
  • 暂存区:用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息。
  • 本地仓库(版本库):就是安全存放数据的位置,这里面都是你提交的所有代码信息。
  • 远程仓库:就是托管代码的服务器,类似于 FTP 服务,能够共享数据。

二、Git环境部署

环境准备:

主机名 ip 操作系统 角色
git 192.168.10.7 Centos7.4 server
client 192.168.10.8 Centos7.4 client
注意:CentOS 7 上默认已经安装好了 Git 服务。

1.Git使用

服务端创建仓库

#这里可以直接使用root,但是在真实服务器上需要划分明确权限
[root@git ~]# mkdir project		#创建仓库目录,存放仓库
[root@git ~]# cd project/
[root@git project]# git init --bare		#初始化仓库
初始化空的 Git 版本库于 /root/project/
[root@git project]# ls
branches  config  description  HEAD  hooks  info  objects  refs

客户端测试
创建目录,拉取库到本地

[root@client ~]# mkdir git
[root@client ~]# cd git/
[root@client git]# git clone [email protected]:/root/project
正克隆到 'project'...
[email protected]'s password: 
warning: 您似乎克隆了一个空版本库。

创建项目文件

[root@client git]# cd project/
[root@client project]# echo hello > 1.txt

添加到暂缓区

[root@client project]# git add 1.txt

查看文件状态

[root@client project]# git status	
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#	新文件:    1.txt
#

提交到本地仓库
#需要填写邮箱

[root@client project]# git commit -m "1"					# -m 选项是说明信息

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@client.(none)')

配置账号和邮件

[root@client project]# git config --global user.name "zhaokyang888"		
[root@client project]# git config --global user.email "[email protected]"

重新提交修改到本地仓库

[root@client project]# git commit -m "1"		
[master(根提交) 835b0b8] 1
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

确认本地仓库和远程仓库的状态是否正常

[root@client project]# git remote add origin [email protected]:/root/project.git
fatal: 远程 origin 已经存在。

把本地仓库代码推送到远程仓库

[root@client project]# git push origin master
[email protected]'s password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 203 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/root/project
 * [new branch]      master -> master

2.Git测试

将本地仓库删除

[root@client project]# cd ..
[root@client git]# ll
总用量 0
drwxr-xr-x 3 root root 31 227 15:58 project
[root@client git]# rm -rf *
[root@client git]# ll
总用量 0

重新同步远程仓库

[root@client git]# git clone [email protected]:/root/project
正克隆到 'project'...
[email protected]'s password: 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@client git]# cd project/
[root@client project]# ll
总用量 4
-rw-r--r-- 1 root root 6 227 16:25 1.txt
[root@client project]# cat 1.txt #成功复制
hello

三、结合使用GitHab

  • GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本仓库格式进行托管,所以故名为 GitHub。
  • GIT服务器并不需要我们搭建,我们使用github就可以满足我们的需求。

1.在客户端上生成密钥对

[root@client ~]# ssh-keygen -t rsa		#连敲三下
[root@Client ~]# cat .ssh/id_rsa.pub		#查看公钥,复制

2.公钥导入GitHab

在这里插入图片描述
在这里插入图片描述
将刚才的密钥复制过来
在这里插入图片描述

测试验证

[root@git ~]# ssh -T [email protected]		#第一次显示
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
RSA key fingerprint is MD5:16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi Xy888888! You've successfully authenticated, but GitHub does not provide shell access.
[root@git ~]# ssh -T [email protected]		#如果是第二次,直接显示
Hi Xy888888! You've successfully authenticated, but GitHub does not provide shell access.

3.创建储存库

在这里插入图片描述
在这里插入图片描述

4.将本地项目上传到 GitHub

[root@git ~]# mkdir test
[root@git ~]# cd test/
[root@git test]# echo "test" > test.txt
[root@git test]# git init
初始化空的 Git 版本库于 /root/test/.git/
[root@git test]# git add 
.git/     test.txt  
[root@git test]# git add test.txt 
[root@git test]# git commit -m "test"
[master(根提交) 8af3b3f] test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@git test]# git remote add origin https://github.com/Xy888888/test.git

下面输入密码的时候,输入的是token
创建过程:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
把token复制下来,这就是密码

[root@git test]# git push -u origin master
Username for 'https://github.com': Xy888888
Password for 'https://[email protected]': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Xy888888/test.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

5.查看

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_51052245/article/details/123160662