Git详细使用教程——构建分布式版本控制系统

一.版本控制概念
1.记录开发文件的时间机器,挽回代码错误带来的损失
2.分类
本地版本控制系统
在这里插入图片描述集中化的版本控制系统
CVS、Subversion(SVN)

在这里插入图片描述分布式版本控制系统GIT

在这里插入图片描述3.产品
1)github
公共的服务器
GitHub注册
官网 :https://guides.github.com/activities/hello-world/
图示
在这里插入图片描述流程
注册账号
新建项目
克隆项目

git客户端
	前提
		1、github.com 注册账户
		2、在github上创建仓库
	1.生成本地ssh key
		ssh-keygen -t rsa -C 'git@github.com'
			邮箱要与github上注册的相同
			[root@abc ~]# ssh-keygen -t rsa -C 'git@github.com'
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:YU6tRpZ8rzqUG7qw2jaAUHSjZCwmMM2S4C2IPLR8xKc git@github.com
The key's randomart image is:
+---[RSA 2048]----+
|*B=.o            |
|%=O+ o . o       |
|=@.oo   O o      |
|. +E   * + .     |
|..      S.  .    |
|. .    .+  .     |
|   ..  o o.      |
|   .oo. o.       |
|  .oo.....       |
+----[SHA256]-----+

	2.复制 公钥,在github 中添加ssh key
		cat .ssh/id_rsa.pub 
			[root@abc ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZ99Cjo4YQXLQsRAYNPyN4/y7AR8Oih+jkgQA8tylZ3Obxgk+ZdIYij/JdFUqBT4W8OmvzBhhhnAKNNOPwVZdINhffA1nJzhjtuutn40Fvxfl775g0Hg5MFtSYD3De7gvlrBTHgmDxwVQcNqJ35F85Cbidq7EPO/bDH2FFV7MLlucQUoGb5/GgbGWzMHOqSZ0d78VeklQoE1xNlJZ0zreMnsnCoB6ZAv1awdxkl+WLmueMO4FILDwDpfoR48KevJm3NnkA8pOIum2504CWZ6EaLHz4b0A90oBU/UVQ/a4bVyxZvrZ5Z8xP+iIrgcfWVvDO1YSgnOxTWDHbaxtLj0oh git@github.com

		在github找到账户设置
			
		选择ssh key 
			
	3.测试本地客户端
		yum install git
	4.进行认证(但不能远程连接)
		ssh -T git@github.com
			[root@abc ~]# ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' 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,52.74.223.119' (RSA) to the list of known hosts.
Hi xuleicloud! You've successfully authenticated, but GitHub does not provide shell access.

	5.在本地添加远程仓库用户名
		git config --global user.name 'xuleicloud'
	6.在本地添加远程仓库邮箱
		git config --global user.email '512050951@163.com'
	7.启用默认的颜色设置
		git config --global color.ui true
			不喜欢花花绿绿的就关了。git config --global color.ui false
	8.查看配置
		git config --list
		 ls .gitconfig
		cat .gitconfig 
	9.连接远程仓库
		git remote -v
	10.设置远程仓库地址
		查看远程仓库地址
			
		 git remote add origin git@github.com:xuleicloud/xulei001.git
	11.克隆远程仓库信息
		 git clone git@github.com:xuleicloud/xulei001.git
		 ls
		cd xulei001/
		ls
	12.上传文件
		echo 1234567  >  3333.sh
			新建文件
		git init
			初始化
		git remote add origin git@github.com:xuleicloud/xulei001.git
			配置远程服务器地址
			如果远程服务器配置报错,因为之前已经配置过其他远程服务器。可以用命令清除掉
				git remote rm origin 
		git add .
			建立当前文件夹下所有的文件,为准备上传的文件
		git commit
			提交上传说明
			 git commit -m 'first commit'
		git push -u origin master
			上传

2)git

git
	纯命令行的软件
	服务器&客户端
		1.准备工作目录
			useradd git
			 mkdir /git-root/
				任意目录
			cd /git-root/
		2.创建一个分支
			git init --bare shell.git
				初始化空的 Git 版本库于 /git-root/shell.git/
				[root@abc git-root]# ls
				shell.git
		3.目录授权
			chown -R git:git shell.git/
		4.切换git
			su - git
		5.生成秘钥
			ssh-keygen -t rsa
				ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/opt/gitlab/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/opt/gitlab/.ssh/id_rsa.
Your public key has been saved in /var/opt/gitlab/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZoWt/DTfxpq1Q6iGB1jdG0VsBzPNChRQJOHTVrylD6M git@abc
The key's randomart image is:
+---[RSA 2048]----+
|          +*==*+ |
|         + o..=o=|
|        ..=.o+ * |
|       ..o.oo *  |
|       oS o  = + |
|      .o.o oEo. .|
|         o....=  |
|        . +  =.. |
|         o  o .. |
+----[SHA256]-----+

			cd .ssh/
			cp id_rsa.pub authorized_keys
			logout
		6.设置git账号的shell
			[root@abc git-root]# usermod -s /usr/bin/git-shell git
		7.测试下载仓库
			cd /opt/
			git clone git@10.18.47.23:/git-root/shell.git
				[root@abc opt]# git clone git@10.18.47.23:/git-root/shell.git
正克隆到 'shell'...
git@10.18.47.23's password: 
warning: 您似乎克隆了一个空版本库。
[root@abc opt]# ls
gitlab  shell
		8.测试上传仓库
			cd  shell
			 touch 123
			 git add .
			 git commit -m 'zdsad'
			git push -u origin master
			 rm -rf *
			git clone git@10.18.47.23:/git-root/shell.git
				[root@abc shell]# rm -rf *
[root@abc shell]# git clone git@10.18.47.23:/git-root/shell.git
正克隆到 'shell'...
git@10.18.47.23's password: 
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), done.
[root@abc shell]# ls
shell
[root@abc shell]# ls shell/
123  test1.sh

	客户端

3)gitlab
私有的git web服务器
gitlab-LOGO
在这里插入图片描述二.Gitlab部署
1.system 系统
支持的类UNIX系统
Ubuntu
Debian
CentOS
Red Hat Enterprise Linux (please use the CentOS packages and instructions)
Scientific Linux (please use the CentOS packages and instructions)
Oracle Linux (please use the CentOS packages and instructions)
不支持的类UNIX系统
OS X
Arch Linux
Fedora
Gentoo
FreeBSD
2.官网链接
https://about.gitlab.com/downloads/#centos7
3. Install and configure the necessary dependencies 安装并配置所需的依赖项

	4G以上内存
	sudo yum install curl policycoreutils openssh-server openssh-clients
		安装和配置必要的依赖关系
	sudo systemctl enable sshd
		开机自启动ssh程序
	sudo systemctl start sshd
	sudo yum install postfix
		安装邮件程序
	sudo systemctl enable postfix
	sudo systemctl start postfix
	sudo  systemctl stop firewalld
		防火墙关上!
	sudo systemctl disable firewalld
	sudo是授权超管不加也可以
  1. Add the GitLab package server and install the package 添加GitLab包服务器并安装包
    校内安装
    wget ftp://10.18.40.100/rpm-soft/gitlab-ce-9.1.0-ce.0.el7.x86_64.rpm
    提示如果是zip压缩包,请使用解压才可以使用。
    unzip 6.Git_构建分布式版本控制系统.zip
    cd 6.Git_构建分布式版本控制系统
    yum -y install gitlab-ce-9.1.0-ce.0.el7.x86_64.rpm
    官网安装(略)
    curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    sudo yum install gitlab-ce
  2. Configure and start GitLab 配置并启动GitLab
    gitlab-ctl reconfigure
    自动配置文件权限,安装数据库….
    提示!安装的时间会很长!!!
    6.Browse to the hostname and login 浏览到主机名并登录
    1)gitlab服务器的地址:http://192.168.152.132
    GITLAB占用80端口,访问时直接访问IP就可以
    (第一次会让你确认root账户登录的密码)
    2)登录:创建8位新密码 然后使用root登录即可
    3)欢迎界面(略)
    4)创建项目
    在这里插入图片描述在这里插入图片描述在这里插入图片描述推送SSH秘钥
    1秘钥提示
    在这里插入图片描述粘贴这个地址,用于下载项目(文档容器)
    2查看秘钥
    生成秘钥 ssh-keygen
    查看
    在这里插入图片描述3上传秘钥

在这里插入图片描述在这里插入图片描述
4.查看秘钥
在这里插入图片描述5链接地址
在这里插入图片描述用于命令行,或客户端程序进行下载使用。
6使用gitlab
图形操作
上传文件到gitlab
1.newfile:先新建一个文件。
2.uploadfile:再上传即可。
在这里插入图片描述在这里插入图片描述
在这里插入图片描述在这里插入图片描述下载文件到gitlab客户端

在这里插入图片描述在这里插入图片描述在这里插入图片描述命令行操作
1.域名解析
vim /etc/hosts
192.168.152.132 gitlab.example.com
2.克隆服务器的文件

git config --global user.name "git"
git config --global user.email "git@gitlab.example.com"
git clone git@gitlab.example.com:root/project2.git
注意您粘贴的地址中,使用了什么用户名。

3.上传文件

cd project2  进入工作目录
echo 1234567  >  3333.sh   新建文件
git init   初始化
git remote add origin git@gitlab.example.com:root/project2.git
配置远程服务器地址
如果远程服务器配置报错,因为之前已经配置过其他远程服务器。可以用命令清除掉
git remote rm origin 
git add .  建立当前文件夹下所有的文件,为准备上传的文件
git commit   提交上传说明   git commit -m 'first commit'
git push -u origin master  上传

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值