本文共 9335 字,大约阅读时间需要 31 分钟。
• 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
• **工作区:**就是你在电脑里能看到的目录
• **暂存区:**英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)
• **版本库:**工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
• 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: zzgDate: 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无文件要提交,干净的工作区
[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
• 导入中文版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 # 日志文件
• 访问http://127.0.0.1,第一次需要设置root密码
• 创建群组 group
使用群组管理项目和人员是非常好的方式
• 创建项目 project
存储代码的地方,里面还包含问题列表、维基文档以及其他一些Gtab功能
• 创建成员 member
添加你的团队成员或其他人员到Gtab
• 创建名为devops的group(组)
• 创建群组(续1)
为devops组中的成员创建用户zzg。新建用户的时候,不能创建密码。用户建立好之后,修改用户,可以为其加密码。
• 创建名为myproject的项目
配置新建用户可以免密推送代码
• 使用新创用户devops
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
粘贴到密钥这里
上传代码到gitlab服务器
[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
转载地址:http://gqnwi.baihongyu.com/