技术流ken

运维拯救世界

1901课堂记录: git分布式版本控制–技术流ken

Git和SVN的对比

 

1.git是分布式的,svn是集中式的。(最核心)

2.git是每个历史版本都存储完整的文件,便于恢复,svn是存储差异文件,历史版本不可恢复。(核心)

3.git可离线完成大部分操作,svn则不能。

4.git有着更优雅的分支和合并实现。

5.git有着更强的撤销修改和修改历史版本的能力

6.git速度更快,效率更高。

基于以上区别,git有了很明显的优势,特别在于它具有的本地仓库。

 

git安装

 

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

 

git常用操作

 

git add: 添加文件至暂存区域

git branch: 查看分支和创建分支

git checkout: 进行撤销也可以进行分支切换

git clone: 克隆远程主机仓库

git commit: 把暂存区域的文件提交至仓库中

git init: 初始化目录(工作目录)

git merge: 合并分支

git pull: 拉取远程主机的仓库

git push: 把本地仓库推送到远程主机

git reset: 撤销操作

git status: 查看git目录中文件状态

 

git使用演示

 

第一步:创建一个目录

[root@ken ~]# mkdir /ken
[root@ken ~]# cd /ken

 

第二步:初始目录为git工作目录

[root@ken ken]# git init
Initialized empty Git repository in /ken/.git/
[root@ken ken]# ls -a
. .. .git

 

第三步:创建文件并提交

[root@ken ken]# touch test
[root@ken ken]# ls
test
[root@ken ken]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use “git add <file>…” to include in what will be committed)
#
# test
nothing added to commit but untracked files present (use “git add” to track)
[root@ken ken]# git add .    #表示把当前所有的文件都进行提交
[root@ken ken]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use “git rm –cached <file>…” to unstage)
#
# new file: test
#
[root@ken ken]# git commit -m “v1”

*** Please tell me who you are.

Run

git config –global user.email “you@example.com”
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@ken.(none)’)
[root@ken ken]#
[root@ken ken]# git config –global user.email “you@example.com”
[root@ken ken]# git config –global user.name “Your Name”
[root@ken ken]# git commit -m “v1”
[master (root-commit) 6c53735] v1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test
[root@ken ken]# git status
# On branch master
nothing to commit, working directory clean

 

第四步:查看已经提交的版本
[root@ken ken]# git log
commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

第五步:写入内容至文件并提交

[root@ken ken]# echo “111” >> test
[root@ken ken]#
[root@ken ken]#
[root@ken ken]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)
[root@ken ken]# git add test
[root@ken ken]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# modified: test
#
[root@ken ken]# git commit -m “v2”
[master aa70338] v2
1 file changed, 1 insertion(+)
[root@ken ken]# git log
commit aa70338daf23b487237c9f51d10a80c6bd757e75
Author: Your Name <you@example.com>
Date: Thu May 23 15:01:37 2019 +0800

v2

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

 

第六步:回滚到V1版本

[root@ken ken]# git log
commit aa70338daf23b487237c9f51d10a80c6bd757e75
Author: Your Name <you@example.com>
Date: Thu May 23 15:01:37 2019 +0800

v2

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1
[root@ken ken]# git reset –hard 6c537358b0c8bae465
HEAD is now at 6c53735 v1
[root@ken ken]# cat test

 

 

 撤销工作区的文件

 

第一步:查看当前文件状态

[root@ken ken]# git status
# On branch master
nothing to commit, working directory clean

 

第二步:编辑文件并查看文本状态

[root@ken ken]# echo “222” >> test
[root@ken ken]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)

 

第三步:撤销工作内容

[root@ken ken]# git checkout — test

 

第四步:查看当前文本状态

[root@ken ken]# git status
# On branch master
nothing to commit, working directory clean

 

 撤销暂存区的文件

 

第一步:查看状态

[root@ken ken]# git status
# On branch master
nothing to commit, working directory clean

 

第二步:编辑文本并查看状态

[root@ken ken]# echo “222” >> test
[root@ken ken]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)

第三步:进行提交add并查看状态

[root@ken ken]# git add test
[root@ken ken]# git status
# On branch master
# Changes to be committed:
# (use “git reset HEAD <file>…” to unstage)
#
# modified: test
#

 

第四步:回撤到工作区并查看状态

[root@ken ken]# git reset HEAD test
Unstaged changes after reset:
M test
[root@ken ken]# git status
# On branch master
# Changes not staged for commit:
# (use “git add <file>…” to update what will be committed)
# (use “git checkout — <file>…” to discard changes in working directory)
#
# modified: test
#
no changes added to commit (use “git add” and/or “git commit -a”)

 

第五步:撤销工作区的文件并查看

[root@ken ken]# git checkout — test
[root@ken ken]# git status
# On branch master
nothing to commit, working directory clean
[root@ken ken]# cat test
[root@ken ken]#

 

 回滚到任意版本操作演示

第一步:提交多个版本

[root@ken ken]# echo “222” >> test
[root@ken ken]# git add .
[root@ken ken]# git commit -m “v3”
[master a34b925] v3
1 file changed, 1 insertion(+)
[root@ken ken]# echo “444” >> test
[root@ken ken]# git add .
[root@ken ken]# git commit -m “v3”
[master 33bc97c] v3
1 file changed, 1 insertion(+)

 

第二步:查看所有版本

[root@ken ken]# git log
commit 33bc97c79532c02c2a0705b3d2dad85168b4790f
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:11 2019 +0800

v3

commit a34b925d69a5aa37f59a43df3ad25b638a69429e
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:03 2019 +0800

v3

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

第三步:回滚到V1版本

[root@ken ken]# git reset –hard 6c537358b
HEAD is now at 6c53735 v1

 

第四步:查看当前说出的版本

[root@ken ken]# git log
commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

第五步:回滚到v2版本

[root@ken ken]# git reflog
6c53735 HEAD@{0}: reset: moving to 6c537358b
33bc97c HEAD@{1}: commit: v3
a34b925 HEAD@{2}: commit: v3
6c53735 HEAD@{3}: reset: moving to 6c537358b0c8bae465
aa70338 HEAD@{4}: commit: v2
6c53735 HEAD@{5}: commit (initial): v1
[root@ken ken]# git reset –hard aa70338
HEAD is now at aa70338 v2
[root@ken ken]# git log
commit aa70338daf23b487237c9f51d10a80c6bd757e75
Author: Your Name <you@example.com>
Date: Thu May 23 15:01:37 2019 +0800

v2

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1
[root@ken ken]# cat test
111

 

回滚到v3版本:

[root@ken ken]# git reset –hard 33bc97c
HEAD is now at 33bc97c v3
[root@ken ken]#
[root@ken ken]#
[root@ken ken]# cat test
222
444

 

 

git分支查看、创建、切换

第一步:查看分支

[root@ken ken]# git branch
* master

 

第二步:创建分支并查看

[root@ken ken]# git branch ken
[root@ken ken]# git branch
ken
* master

 

第三步:切换分支并查看

[root@ken ken]# git checkout ken
Switched to branch ‘ken’
[root@ken ken]# git branch
* ken
master

 

 

git分支使用演示

 

第一步:切换分支至ken

[root@ken ken]# git checkout ken
Switched to branch ‘ken’
[root@ken ken]# git branch
* ken
master

 

第二步:编辑test文件并进行提交

[root@ken ken]# echo “555” >> test
[root@ken ken]# git add .
[root@ken ken]# git commit -m “v5”
[ken 5a92410] v5
1 file changed, 1 insertion(+)
[root@ken ken]# git log
commit 5a92410e510c862c3074794a6aa089c468bdab1a
Author: Your Name <you@example.com>
Date: Thu May 23 15:42:13 2019 +0800

v5

commit 33bc97c79532c02c2a0705b3d2dad85168b4790f
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:11 2019 +0800

v3

commit a34b925d69a5aa37f59a43df3ad25b638a69429e
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:03 2019 +0800

v3

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

第三步:切换至主分支

[root@ken ken]# git checkout master
Switched to branch ‘master’
[root@ken ken]# git branch
ken
* master

 

第四步:查看文本test

[root@ken ken]# cat test
222
444

 

第五步:分支合并

[root@ken ken]# git merge ken
Updating 33bc97c..5a92410
Fast-forward
test | 1 +
1 file changed, 1 insertion(+)
[root@ken ken]# cat test   #可以查看到分支ken编辑后的内容
222
444
555
[root@ken ken]# git log   #可以看到分支ken提交的版本了
commit 5a92410e510c862c3074794a6aa089c468bdab1a
Author: Your Name <you@example.com>
Date: Thu May 23 15:42:13 2019 +0800

v5

commit 33bc97c79532c02c2a0705b3d2dad85168b4790f
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:11 2019 +0800

v3

commit a34b925d69a5aa37f59a43df3ad25b638a69429e
Author: Your Name <you@example.com>
Date: Thu May 23 15:29:03 2019 +0800

v3

commit 6c537358b0c8bae465460aea86c4ce2ddd4b6ea4
Author: Your Name <you@example.com>
Date: Thu May 23 14:59:01 2019 +0800

v1

 

 

gitlab私有仓库的搭建

 

第一步:上传安装包

[root@ken ~]# rz

[root@ken ~]# ls
gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm

 

第二步:安装软件包

[root@ken ~]# yum install gitlab-ce-8.9.5-ce.0.el7.x86_64.rpm -y

 

第三步:修改gitlab配置文件

[root@ken ~]# vim /etc/gitlab/gitlab.rb

external_url ‘http://192.168.64.4’

 

第四步:重新配置gitlab

[root@ken ~]# gitlab-ctl reconfigure

Running handlers:
Running handlers complete
Chef Client finished, 224/314 resources updated in 04 minutes 53 seconds
gitlab Reconfigured!

 

第五步:lsof查看80端口的运行用户

[root@ken ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 4011 root 6u IPv4 35583 0t0 TCP *:http (LISTEN)
nginx 4020 gitlab-www 6u IPv4 35583 0t0 TCP *:http (LISTEN)

 

第六步:浏览器访问

第七步:创建项目并使用

 

[root@ken ~]# mkdir /test2
[root@ken ~]# cd /test2
[root@ken test2]# ls

[root@ken test2]# ls -a
. ..
[root@ken test2]# git clone http://192.168.64.4/root/test.git
Cloning into ‘test’…
warning: You appear to have cloned an empty repository.
[root@ken test2]# ls
test
[root@ken test2]# ls -a
. .. test
[root@ken test2]# cd test
[root@ken test]# ls -a
. .. .git
[root@ken test]# touch test1
[root@ken test]# ls
test1
[root@ken test]# vim test1
[root@ken test]# cat test1

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

 

 

[root@ken test]# git add .
[root@ken test]# git commit -m “v1”
[master (root-commit) 70647a5] v1
1 file changed, 26 insertions(+)
create mode 100644 test1
[root@ken test]# git push -u origin master
Username for ‘http://192.168.64.4’: root
Password for ‘http://root@192.168.64.4’:
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 674 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.64.4/root/test.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

 

linux客户端免密使用gitlab

 

第一步:linux客户端生成秘钥

[root@ken ~]# ssh-keygen
Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh’.
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:2XqnUUDePGNyyIvmNpVSP3aclZJay7Yg5k8CJ1btPiY root@ken
The key’s randomart image is:
+—[RSA 2048]—-+
| . |
| +.+ . .|
| .B.B+ ..|
| .=.O++oo |
| +S+=+=++ |
| .o*+oooo. |
| =Eo=.. |
| . o*+. |
| .. |
+—-[SHA256]—–+

 

第二步:查看并复制公钥

[root@ken ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0vaHB5bIq+w5zy5kWCwHlxSrVPoStgXj14hawcx3a0uVLh53RiOpF2zFNAIYR1IvlCnCdcPoHhNYu9FC3gnVpPYDqMmkTOzevpseVGOVknnJLN/j0ibAhEQibB3EL3iY+gnTI6kEhxmOBXHPtUGMuK6/o7aYOlPdIFcyIAvKw86HoEOdPnaXw1Rnsfmeb0zQ+jgyuUpM8oizmb2J4QALPc0tVyZ2mya6XeJG6Omi6/yY7HuNBq0YlXes2bxgW8VbTJs5fw4E738vTdRp4b6F4wFLDwkuh1HHCECFLG9jIWQYDa1dbKNaew7R+trBwGFlAh4ywoOZFROexKfLHjnKn root@ken

 

第三步:公钥信息写入到gitlab

点击左上角的几个横杠并选择profile setting,并选择SSH-keys,黏贴公钥保存即可

 

第四步:进行测试

注意:在拉取和推送的时候都要选择ssh地址

 

window客户端免密使用gitlab

 

接下来的操作都是在window系统中的操作

第一步:安装git客户端工具

 

第二步:创建空目录

 

第三步:生成秘钥

ssh-keygen

找到公钥并复制到gitlab中

 

第四步:进行验证

 

 

发表评论

邮箱地址不会被公开。