博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GitLab服务应用
阅读量:3945 次
发布时间:2019-05-24

本文共 9335 字,大约阅读时间需要 31 分钟。

本地使用git

本地操作

Git简介

Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件
Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持

安装及配置

Git安装后需配置用户相关信息

[root@localhost]# yum -y install git[root@localhost]# git config --global user.name 'zzg'	# 安装git版本控制软件[root@localhost]# git config --global user.email 'zzg@tedu.cn'			# 设置用户信息,如用户名、emai1等[root@localhost]# git config --global core.editor vim							# 设置默认编辑器为vim[root@localhost]# git config --list	# 查看用户配置user.name=zzguser.email=zzg@tedu.cncore.editor=vim[root@localhost]# cat /root/.gitconfig[user]	name = zzg	email = zzg@tedu.cn[core]	editor = vim

Git工作流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NLvLRO5A-1585735837671)(C:\Users\Jsck\Desktop\Gitlab\图片1\git1.png)]

工作区、暂存区和版本库

• **工作区:**就是你在电脑里能看到的目录

• **暂存区:**英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)

• **版本库:**工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库

工作区、暂存区和版本库(续1)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MiUAB5nb-1585735837672)(C:\Users\Jsck\Desktop\Gitlab\图片1\git2.png)]

创建仓库

Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在Git 的仓库中运行,所以 git init是使用 Git 的第一个命令

[root@localhost ~]# mkdir py_git[root@localhost ~]# cd py_git/[root@localhost py_git]# echo '

Hello World!

' > index.html[root@localhost py_git]# git init .初始化空的 Git 版本库于 /root/py_git/.git/[root@localhost py_git]# ls -A.git index.html

添加文件到暂存区

添加指定文件

[root@localhost py_git]# git status[root@localhost py_git]# git status -s	# 简要信息?? index.html[root@localhost py_git]# git add .				# 将目录下所有内容加入暂存区,开始跟踪[root@localhost py_git]# git status -sA  index.html[root@localhost py_git]# git rm --cached index.html   # 撤出暂存区rm 'index.html'

添加所有文件

[root@localhost py_git]# git status -s?? index.html[root@localhost py_git]# git add .  # 重新加入暂存区[root@localhost py_git]# git status -sA  index.html[root@localhost py_git]# git commit  # 确认至版本库,需要写日志project init[root@localhost py_git]# git status# 位于分支 master无文件要提交,干净的工作区[root@localhost py_git]# git status -s

确认至仓库

提交之前务必先设置用户信息

[root@localhost py_git]# echo '

Heloo Git

' >> index.html [root@localhost py_git]# cp /etc/hosts .[root@localhost py_git]# git status -s M index.html?? hosts[root@localhost py_git]# git add .[root@localhost py_git]# git status -sA hostsM index.html[root@localhost py_git]# git status[root@localhost py_git]# git commit -m "modify index.html, add hosts"[root@localhost py_git]# git status# 位于分支 master无文件要提交,干净的工作区

删除跟踪文件

要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交

git ls-files				# 查看版本库中文件git rm world.pygit commit -m '删除world.py'
[root@localhost py_git]# rm -rf * # 不会将隐藏的.git删掉[root@localhost py_git]# ls[root@localhost py_git]# git status[root@localhost py_git]# git checkout -- *[root@localhost py_git]# ll总用量 8-rw-r--r--. 1 root root 158 3月  31 16:39 hosts-rw-r--r--. 1 root root  41 3月  31 16:39 index.html[root@localhost py_git]# git rm hostsrm 'hosts'[root@localhost py_git]# lsindex.html[root@localhost py_git]# git status -sD  hosts[root@localhost py_git]# git status[root@localhost py_git]# git reset HEAD hosts重置后撤出暂存区的变更:D	hosts[root@localhost py_git]# git status[root@localhost py_git]# git checkout -- hosts[root@localhost py_git]# lshosts  index.html

删除文件的完整过程

[root@localhost py_git]# git rm hostsrm 'hosts'[root@localhost py_git]# git status -sD  hosts[root@localhost py_git]# git commit -m "del hosts"[master d4e7725] del hosts 1 file changed, 2 deletions(-) delete mode 100644 hosts[root@localhost py_git]# git status# 位于分支 master无文件要提交,干净的工作区[root@localhost py_git]# lsindex.html

改名、移动

[root@localhost py_git]# cp /etc/passwd .[root@localhost py_git]# git status -s?? passwd[root@localhost py_git]# git add .[root@localhost py_git]# git commit -m "add passwd"[master eb92308] add passwd 1 file changed, 44 insertions(+) create mode 100644 passwd[root@localhost py_git]# git mv passwd mima[root@localhost py_git]# git status -sR  passwd -> mima[root@localhost py_git]# git status[root@localhost py_git]# git commit -m "rename passwd -> mima"[master 2830428] rename passwd -> mima 1 file changed, 0 insertions(+), 0 deletions(-) rename passwd => mima (100%)[root@localhost py_git]# git status# 位于分支 master无文件要提交,干净的工作区

切换到以前版本

将HEAD指针指向以前的某个提交就可以切换到以前的某个状态了

[root@localhost py_git]# git log		 查看所有的提交................commit 89f031add0828b3dcc19e886cc7cfb9821d75043Author: zzg 
Date: Tue Mar 31 16:19:39 2020 +0800 modify index.html, add hostscommit 70724cdddac43b380d7e50f638a904dd67d52654Author: zzg
Date: Tue Mar 31 16:00:58 2020 +0800.................[root@localhost py_git]# git checkout 70724cdddac43b380d7e50f638a904dd67d52654[root@localhost py_git]# git logcommit 70724cdddac43b380d7e50f638a904dd67d52654Author: zzg
Date: Tue Mar 31 16:00:58 2020 +0800 project init现在目录下出现了index.html文件[root@localhost py_git]# lsindex.html[root@localhost py_git]# git checkout master之前的 HEAD 位置是 70724cd... project init切换到分支 'master'[root@localhost py_git]# lsindex.html mima

分支管理

git允许不同的用户创建不同的分支实现代码管理,还可以把分支合并到主干分支。默认情况下,git有一个名为master的主干分支

[root@localhost py_git]# git branch		# 查看分支* master[root@localhost py_git]# lsindex.html  mima

如果你负责项目的一个功能,可以在某一个提交节点创建分支。注意,在创建分支的时候masten分支应该是干净

[root@localhost py_git]# git branch fn1[root@localhost py_git]# git branch   fn1* master有*号的表示masten分支

在master分支提交代码

[root@localhost py_git]# cp /etc/passwd .[root@localhost py_git]# git add .[root@localhost py_git]# git commit -m "master-a1"[master b7b4882] master-a1 1 file changed, 44 insertions(+) create mode 100644 passwd[root@localhost py_git]# lsindex.html  mima  passwd

切换分支

[root@localhost py_git]# git checkout fn1切换到分支 'fn1'[root@localhost py_git]# git checkout[root@localhost py_git]# git branch * fn1  master[root@localhost py_git]# ls		# 没有passwd文件index.html  mima

在fn1分支编写程序

[root@localhost py_git]# cp ~/anaconda-ks.cfg .[root@localhost py_git]# git add .[root@localhost py_git]# git commit -m "fn1 add anaconda"[fn1 bfd40ce] fn1 add anaconda 1 file changed, 64 insertions(+) create mode 100644 anaconda-ks.cfg[root@localhost py_git]# git status# 位于分支 fn1无文件要提交,干净的工作区
  • 切换回master分支
[root@localhost py_git]# git checkout master切换到分支 'master'[root@localhost py_git]# git branch  fn1* master[root@localhost py_git]# ls		# 有passwd,没有anaconda-ks.cfg文件index.html  mima  passwd

合并分支

[root@localhost py_git]# git merge fn1Merge made by the 'recursive' strategy. anaconda-ks.cfg | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 anaconda-ks.cfg[root@localhost py_git]# lsanaconda-ks.cfg  index.html  mima  passwd[root@localhost py_git]# git branch  fn1* master
  • 删除分支
[root@localhost py_git]# git branch -d fn1已删除分支 fn1(曾为 bfd40ce)。[root@localhost py_git]# git branch* master

git服务器

使用远程服务器

搭建本地gtab服务器

导入中文版gitlab镜像

密码: 72y1

[root@localhost ~]# unzip docker.zip[root@localhost ~]# cd /root/docker/docker_pkgs/[root@localhost docker_pkgs]# yum install *.rpm[root@localhost docker_pkgs]# systemctl enable docker[root@localhost docker_pkgs]# systemctl start docker[root@localhost docker_pkgs]# cd ../images/[root@localhost images]# docker load < gitlab_zh.tar[root@localhost images]# docker imagesREPOSITORY          TAG                 IMAGE ID            CREATED             SIZEgitlab_zh           latest              1f71f185271a        2 years ago         1.63 GB

将物理主机ssh端口改为2222后,起动容器

[root@localhost ~]# vim  /etc/ssh/sshd_configPort 2022			# 17行[root@localhost ~]# systemctl restart sshd################################################[root@localhost ~]# docker run -d -h gitlab --name gitlab -p 443:443 -p 80:80 -p 22:22 --restart always -v /srv/gitlab/config:/etc/gitlab -v /srv/gitlab/logs:/var/log/gitlab -v /srv/gitlab/data:/var/opt/gitlab gitlab_zh:latest# 注意:因为服务很多,启动较慢################################################docker run -d -h gitlab --name gitlab \-p 443:443 -p 22:22 -p 80:80 --restart always \-v /srv/gitlab/config:/etc/gitlab -v \/srv/gitlab/logs:/var/log/gitlab -v \/srv/gitlab/data:/var/opt/gitlab gitlab_zh################################################sudo docker run --detach \  --hostname gitlab.example.com \  --publish 443:443 --publish 80:80 --publish 22:22 \  --name gitlab \  --restart always \  --volume /srv/gitlab/config:/etc/gitlab \  --volume /srv/gitlab/logs:/var/log/gitlab \  --volume /srv/gitlab/data:/var/opt/gitlab \  gitlab/gitlab-ce:latest#################################################[root@localhost ~]# reboot[root@localhost ~]# docker psdocker ps		# 直到显示(health)才能正常访问[root@localhost ~]# docker logs gitlab	# 日志文件

初始化gitlab服务器(续1)

访问http://127.0.0.1,第一次需要设置root密码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rPkd5eLJ-1585735837673)(C:\Users\Jsck\Desktop\Gitlab\图片\1.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5KlnxOEJ-1585735837673)(C:\Users\Jsck\Desktop\Gitlab\图片\1.1.png)]

添加gitlab项目

创建群组 group

使用群组管理项目和人员是非常好的方式

创建项目 project

存储代码的地方,里面还包含问题列表、维基文档以及其他一些Gtab功能

创建成员 member

添加你的团队成员或其他人员到Gtab

创建群组

创建名为devops的group(组)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UoUDglwa-1585735837674)(C:\Users\Jsck\Desktop\Gitlab\图片\2.png)]

创建群组(续1)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c1uiQYQk-1585735837675)(C:\Users\Jsck\Desktop\Gitlab\图片\3.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NsxIBdgt-1585735837675)(C:\Users\Jsck\Desktop\Gitlab\图片\4.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g2dXIXwQ-1585735837676)(C:\Users\Jsck\Desktop\Gitlab\图片\5.png)]

创建用户

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iuZArk2X-1585735837676)(C:\Users\Jsck\Desktop\Gitlab\图片\6.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0vicsJjO-1585735837677)(C:\Users\Jsck\Desktop\Gitlab\图片\7.png)]

为devops组中的成员创建用户zzg。新建用户的时候,不能创建密码。用户建立好之后,修改用户,可以为其加密码。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pg5jruOx-1585735837678)(C:\Users\Jsck\Desktop\Gitlab\图片\8.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iPGOgL0r-1585735837678)(C:\Users\Jsck\Desktop\Gitlab\图片\9.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QupsruzM-1585735837679)(C:\Users\Jsck\Desktop\Gitlab\图片\10.png)]

创建项目

创建名为myproject的项目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vF3hERCA-1585735837680)(C:\Users\Jsck\Desktop\Gitlab\图片\11.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XV920bvG-1585735837681)(C:\Users\Jsck\Desktop\Gitlab\图片\12.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WjKflxbs-1585735837681)(C:\Users\Jsck\Desktop\Gitlab\图片\13.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mgxuH3Li-1585735837682)(C:\Users\Jsck\Desktop\Gitlab\图片\14.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7Djr6JvI-1585735837682)(C:\Users\Jsck\Desktop\Gitlab\图片\15.png)]

用户管理

配置新建用户可以免密推送代码

使用新创用户devops

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pCmZg6ep-1585735837683)(C:\Users\Jsck\Desktop\Gitlab\图片\16.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gkfPSNmX-1585735837683)(C:\Users\Jsck\Desktop\Gitlab\图片\17.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TCmYCE6w-1585735837683)(C:\Users\Jsck\Desktop\Gitlab\图片\18.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-enuxLYcg-1585735837684)(C:\Users\Jsck\Desktop\Gitlab\图片\19.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NmlWWpOt-1585735837685)(C:\Users\Jsck\Desktop\Gitlab\图片\20.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XkmCK0dw-1585735837685)(C:\Users\Jsck\Desktop\Gitlab\图片\21.png)]

ssh免密登陆需要将用户的公钥上传:

[root@localhost py_git]# ssh-keygen -t rsa -C "your.email@example.com" -b 4096[root@localhost py_git]# ls /root/.ssh/id_rsa.pub[root@localhost py_git]# cat /root/.ssh/id_rsa.pub

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qLXAxq7P-1585735837686)(C:\Users\Jsck\Desktop\Gitlab\图片\22.png)]

粘贴到密钥这里

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sCKAYtjx-1585735837686)(C:\Users\Jsck\Desktop\Gitlab\图片\23.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HGnrtjv7-1585735837686)(C:\Users\Jsck\Desktop\Gitlab\图片\24.png)]

上传代码到gitlab服务器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-njrxKWGC-1585735837688)(C:\Users\Jsck\Desktop\Gitlab\图片\25.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8vkiibEu-1585735837689)(C:\Users\Jsck\Desktop\Gitlab\图片\26.png)]

[root@localhost py_git]# git remote rename origin old-originerror: 不能重命名配置小节 'remote.origin' 到 'remote.old-origin'[root@localhost py_git]# git remote add origin git@192.168.138.142:/devops/devops.git[root@localhost py_git]# git push -u origin --all[root@localhost py_git]# git push -u origin --tags

添加文件

[root@localhost py_git]# touch ooxx[root@localhost py_git]# git add .[root@localhost py_git]# git commit -m "ooxx 18:04"[root@localhost py_git]# git push

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uxHOD33X-1585735837689)(C:\Users\Jsck\Desktop\Gitlab\图片\27.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dgsexcFt-1585735837690)(C:\Users\Jsck\Desktop\Gitlab\图片\28.png)]

转载地址:http://gqnwi.baihongyu.com/

你可能感兴趣的文章
PHP单例模式
查看>>
PHP项目设计
查看>>
memcache的安装及管理
查看>>
git 传输
查看>>
创建新项目
查看>>
印刷工艺- 喷墨印刷
查看>>
印刷工艺流程
查看>>
印刷业ERP启蒙
查看>>
Java8 Lambda表达式使用集合(笔记)
查看>>
Java魔法师Unsafe
查看>>
spring cloud java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
查看>>
Centos系统安装MySQL(整理)
查看>>
postgresql计算两点距离(经纬度地理位置)
查看>>
postgres多边形存储--解决 Points of LinearRing do not form a closed linestring
查看>>
postgresql+postgis空间数据库总结
查看>>
spring 之 Http Cache 和 Etag(转)
查看>>
基于Lucene查询原理分析Elasticsearch的性能(转)
查看>>
HttpClient请求外部服务器NoHttpResponseException
查看>>
springCloud升级到Finchley.RELEASE,SpringBoot升级到2.0.4
查看>>
Spring boot + Arthas
查看>>