本地使用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=zzg
user.email=zzg@tedu.cn
core.editor=vim
[root@localhost]# cat /root/.gitconfig
[user]
name = zzg
email = zzg@tedu.cn
[core]
editor = vim
Git工作流程
工作区、暂存区和版本库
• **工作区:**就是你在电脑里能看到的目录
• **暂存区:**英文叫stage, 或index。一般存放在 “.git目录下” 下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)
• **版本库:**工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
工作区、暂存区和版本库(续1)
创建仓库
• Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在Git 的仓库中运行,所以 git init是使用 Git 的第一个命令
[root@localhost ~]# mkdir py_git
[root@localhost ~]# cd py_git/
[root@localhost py_git]# echo '<h1>Hello World!</h1>' > 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 -s
A 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 -s
A 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 '<h2>Heloo Git</h2>' >> 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 -s
A hosts
M 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.py
git 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 hosts
rm 'hosts'
[root@localhost py_git]# ls
index.html
[root@localhost py_git]# git status -s
D 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]# ls
hosts index.html
删除文件的完整过程
[root@localhost py_git]# git rm hosts
rm 'hosts'
[root@localhost py_git]# git status -s
D 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]# ls
index.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 -s
R 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 89f031add0828b3dcc19e886cc7cfb9821d75043
Author: zzg <zzg@tedu.cn>
Date: Tue Mar 31 16:19:39 2020 +0800
modify index.html, add hosts
commit 70724cdddac43b380d7e50f638a904dd67d52654
Author: zzg <zzg@tedu.cn>
Date: Tue Mar 31 16:00:58 2020 +0800
.................
[root@localhost py_git]# git checkout 70724cdddac43b380d7e50f638a904dd67d52654
[root@localhost py_git]# git log
commit 70724cdddac43b380d7e50f638a904dd67d52654
Author: zzg <zzg@tedu.cn>
Date: Tue Mar 31 16:00:58 2020 +0800
project init
现在目录下出现了index.html文件
[root@localhost py_git]# ls
index.html
[root@localhost py_git]# git checkout master
之前的 HEAD 位置是 70724cd... project init
切换到分支 'master'
[root@localhost py_git]# ls
index.html mima
分支管理
• git允许不同的用户创建不同的分支实现代码管理,还可以把分支合并到主干分支。默认情况下,git有一个名为master的主干分支
[root@localhost py_git]# git branch # 查看分支
* master
[root@localhost py_git]# ls
index.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]# ls
index.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 fn1
Merge made by the 'recursive' strategy.
anaconda-ks.cfg | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)
create mode 100644 anaconda-ks.cfg
[root@localhost py_git]# ls
anaconda-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镜像
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 images
REPOSITORY TAG IMAGE ID CREATED SIZE
gitlab_zh latest 1f71f185271a 2 years ago 1.63 GB
• 将物理主机ssh端口改为2222后,起动容器
[root@localhost ~]# vim /etc/ssh/sshd_config
Port 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密码
添加gitlab项目
• 创建群组 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-origin
error: 不能重命名配置小节 '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