git
查看是否安装了git
rpm -qa git
安装部署
yum install git -y
#查看配置选项
git config
--global 使用全局配置文件
--system 使用系统级配置文件
--local 使用版本库级配置文件
#配置git使用用户
git config --global user.name "baishiming"
#配置git使用有效
git config --global user.email "649472820@qq.com"
#语法高亮
git config --global color.ui true
#查看当前配置
①使用命令
git config --list
②查看隐藏的配置文件
[root@instance-x0nj9foj ~]# cat .gitconfig
[user]
name = baishiming
mail = 649472820@qq.com
[color]
ui = true
[root@instance-x0nj9foj ~]# pwd
/root
#初始化
初始化工作目录,对可以是空目录,也可是已有数据的目录
创建工作目录
mkdir git_data
cd git_data/
初始化命令
[root@instance-x0nj9foj git_data]# git init
#初始化了一个空的目录位于如下位置(ll -a可进行查看)
Initialized empty Git repository in /root/git_data/.git/
#查看git仓库的状态
git status
隐藏文件介绍:
branches #分支目录
config #定义项目特有的配置选项
description #仅供git web 程序使用
HEAD #指示当前的分支
hooks # 包含git钩子文件
info # 包含一个全局排除文件(exclude文件)
objects #存放所有数据内容,有info和pack两个子文件夹(以hash形式存放)
refs #存放指向数据(分支)的提交对象的指针
index #保存暂存区信息,在执行git init 的时候,这个文件还没有
流程图

[root@instance-x0nj9foj git_data]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
9 directories, 13 files
工作目录就是初始化时的目录/root/git_data
本地仓库 objects
暂存区 index(需添加文件后会自动创建,git add)
注:
若需把代码提交到本地仓库,必须经过暂存区
只有提交到本地仓库的代码,才被管理起来
git基础命令
#查看工作区的状态
[root@instance-x0nj9foj git_data]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

#创建测试文件
touch a b c
#再次查看状态
git status
[root@instance-x0nj9foj git_data]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# a
# b
# c
nothing added to commit but untracked files present (use "git add" to track

添加a文件到暂存区(无回显,此时会后台创建index暂存区目录
git add a
git status

使用 git add .或者*添加目录中的所有被改动的文件取暂存区

从暂存区撤出文件(此时文件未被删除,只是从暂存区撤出)
git rm --cached c

删除文件
rm -f c

直接从暂存区域同工作区域一同删除文件命令
git rm -f b

提交到本仓库
#-m message 双括号里面写注释的内容
后面加对应的文件名,则对应的文件会被提交
若是不加,则默认提交所有的暂存区的文件
[root@instance-x0nj9foj git_data]# git commit -m "commit a"
[master (root-commit) 51abce0] commit a
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a

修改文件
mv a a.txt
git status
系统识别到的是源文件a被删除,新文件a.txt重新生成

正确的删除操作为
#先从暂存区撤回
git rm --cached a
git add a.txt
git status
之后再提交到仓库
git commit -m "modify a a.txt"



推荐的操作为:
#把工作区域和暂存区域的文件同时修改文件名称
git mv a.txt a
git commit -m "modify a.txt a"

文件比对
linux上用diff
git里用git diff -->比对工作目录和暂存区的文件的不同
#之前已经提交了a文件,
#现在往a文件里添加数据,然后进行比对
[root@instance-x0nj9foj git_data]# echo index > a
[root@instance-x0nj9foj git_data]# cat a
index
[root@instance-x0nj9foj git_data]# git diff

对比暂存区和本地仓库有什么不同:
git diff --cached

出现暂存区和本地仓库不一致,需要把文件添加到仓库区,就一致了。
git add a
git commit -m "add index"
git diff --cached

推荐使用:
#am 先add 在m 参数顺序不能变
git commit -am "add 123 > a"
git diff --cached
查看历史记录
#git log按q退出
git log
#一行简单的显示commit信息
git log --oneline
#显示当前的指针指向哪里
git log --oneline --decorate
#显示详细内容的变化
git log -p
#只显示1条内容
git log -1
恢复数据到历史版本
--hard后的值,可在git log --oneline里查看
git reset --hard 558bec4
#查看
git reflog
git分支

建立testing分支
git branch testing
查看分支
#有*号的代表当前的分支
[root@instance-x0nj9foj git_data]# git branch
* master
testing
分支操作练习:
touch aaa bbb ccc
git add aaa
git commit -m "add aaa"
git add bbb
git commit -m "add bbb"
git add ccc
git commit -m "add ccc"
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
ef4773b (HEAD, master) add ccc
6da082d add bbb
4676439 add aaa
此时指向的是ccc
切换分支命令
git checkout
切换到testing分支
git checkout testing
[root@instance-x0nj9foj git_data]# git branch
master
* testing
删除分支
#删除前需要切换到另一个分支
git checkout master
git branch -d testing
创建并且切换到新的分支
# -b ---> branch
git checkout -b testing
#查看下当前的指针指向
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
ef4773b (HEAD, testing, master) add ccc
#操作新的分支,开始制造差异
touch test-ddd
git add .
git commit -m "add newfile test-ddd"
git branch
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
3f577ab (HEAD, testing) add newfile test-ddd
ef4773b (master) add ccc
#返回到主干:(之前的分支操作不会影响主干)
git checkout master
git branch
[root@instance-x0nj9foj git_data]# ll
#ll可以发现分支操作的ddd文件没有在主干下
total 0
-rw-r--r-- 1 root root 0 May 12 11:45 a
-rw-r--r-- 1 root root 0 May 12 14:27 aaa
-rw-r--r-- 1 root root 0 May 12 14:27 bbb
-rw-r--r-- 1 root root 0 May 12 14:27 ccc
#当前master指向的是其之前的操作
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
ef4773b (HEAD, master) add ccc
#开始为master分支制造差异
touch master-eee
git add .
git commit -m "add newfile master-eee"
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
4035e0f (HEAD, master) add newfile master-eee
#合并操作
当前master有eee,testing有ddd
merge需要在master上操作,输完命令后会有一个确认
git merge testing
#功能写完,删除testing分支
git checkout master
git branch -d testing
#指针指向如下
[root@instance-x0nj9foj git_data]# git log --oneline --decorate
a9a72df (HEAD, master) Merge branch 'testing' merge barnch 'testting'
冲突测试
情景:
2个分支上有同名的文件,在合并时会发生冲突
先创建 testing分支
git branch testing
在master分支上为aaa文件写入
[root@instance-x0nj9foj git_data]# git branch
* master
testing
[root@instance-x0nj9foj git_data]# echo master >> aaa
[root@instance-x0nj9foj git_data]# git commit -am "modified aaa master"
[master f7fa171] modified aaa master
1 file changed, 1 insertion(+)
[root@instance-x0nj9foj git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@instance-x0nj9foj git_data]# cat aaa
master
在testing分支上为aaa文件写入
[root@instance-x0nj9foj git_data]# git checkout testing
Already on 'testing'
[root@instance-x0nj9foj git_data]# cat aaa
[root@instance-x0nj9foj git_data]# echo testing >> aaa
[root@instance-x0nj9foj git_data]# git commit -am "modified add testing"
[testing 5819976] modified add testing
1 file changed, 1 insertion(+)
[root@instance-x0nj9foj git_data]# cat aaa
testing
切换到master进行merger
[root@instance-x0nj9foj git_data]# git checkout master
Switched to branch 'master'
[root@instance-x0nj9foj git_data]# git merge testing
Auto-merging aaa
#冲突的文件会提示有冲突
CONFLICT (content): Merge conflict in aaa
Automatic merge failed; fix conflicts and then commit the result.
[root@instance-x0nj9foj git_data]# cat aaa
<<<<<<< HEAD
master
=======
testing
>>>>>>> testing
#由于是aaa文件发生的冲突,所以需要手动对冲突文件进行修改(vim aaa),保留需要保留的内容,然后commit一次
[root@instance-x0nj9foj git_data]# git commit -am "merge testing"
[master eab151c] merge testing
[root@instance-x0nj9foj git_data]# git status
# On branch master
nothing to commit, working directory clean
[root@instance-x0nj9foj git_data]# cat aaa
master
testing
git标签使用
标签也是指向了一次commit提交
#-a指定标签名字 -m 指定说明文字
git tag -a v1.0 -m "tag version v1.0"
#为某一次的提交打标签,只需加上对应的hash值
git tag -a v2.0 4035e0f -m "tag version v2.0"
#查看存在的标签
[root@instance-x0nj9foj git_data]# git tag
v1.0
v2.0
#查看v1.0的信息
git show v1.0
#数据到v1.0
git reset --hard v1.0
#删除标签 -d
git tag -d v2.0
github
创建github仓库
登录github官网
[https://github.com/](https://github.com/)
创建仓库,右上角点击+
new repository


添加仓库
git remote add origin git@github.com:18182632624/git_data.git
#回显origin代表成功,git remote查看当前的远程仓库的名称
git remote
需要先做ssh免秘钥,然后才可以推送(免秘钥配置在下文)
#推送
git push -u origin master
推送成功后就可以看到对应的仓库以及代码

ssh秘钥配置
生成key
ssh-keygen -C rsa
Enter..
Enter..
Enter..
#查询生成的秘钥
cat ~/.ssh/id_rsa.pub
复制 到 git 上 保存
Seetings

SSH and GPG keys-->New SSH key

复制仓库地址
在服务器上就可以下载
[root@instance-x0nj9foj tmp]# git clone git@github.com:18182632624/git_data.git
会生成对应的git_data文件夹
复制仓库地址

gitlab安装
机器需要最少2G内存,不然起不来
建议使用下列方法的rpm方式中的第②步安装
使用Git作为代码管理工具,并在此基础上搭建起来的web服务
centos7官网配置指南
https://about.gitlab.com/install/#centos-7
rpm包下载地址
https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-12.10.3-ce.0.el7.x86_64.rpm
yum安装
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
#ip地址更改为自己的Ip
sudo EXTERNAL_URL="https://180.76.150.201" yum install -y gitlab-ee
配置文件/etc/gitlab/gitlab.rb里面会有设定的ip
external_url 'https://180.76.150.201'
若是修改了配置文件
需重新配置并启动gitlab 命令:gitlab-ctl reconfigure && gitlab-ctl start
然后查看主件的状态gitlab-ctl status
rpm安装
依赖安装
yum install -y curl policycoreutils-python openssh-server
systemctl enable sshd
systemctl start sshd
firewall-cmd --permanent --add-service=http
systemctl reload firewalld
setenforce 0
①官方源:
wget -O gitlab.rpm https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-11.11.3-ce.0.el7.x86_64.rpm/download.rpm
rpm -ivh gitlab.rpm
#可以查看安装了哪些的文件
rpm -ql gitlab-ce-11.11.3-ce.0.el7.x86_64
修改配置文件
vim /etc/gitlab/gitlab.rb
external_url 'http://180.76.150.201'
需重新配置并启动gitlab 命令:gitlab-ctl reconfigure && gitlab-ctl start
然后查看主件的状态gitlab-ctl status
②由于网络问题,可以使用国内源
新建 /etc/yum.repos.d/gitlab-ce.repo,内容为:
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
sudo yum makecache
sudo yum install gitlab-ce
安装完成之后,vim /etc/gitlab/gitlab.rb
按照需求修改配置,
主要需要修改external_url,改成自己使用的 url 地址。
执行命令配置生效并启动:
gitlab-ctl reconfigure
gitlab-ctl restart
查看状态:
gitlab-ctl status
最后访问看是否安装成功:http://你的ip地址
可以安装汉化补丁
gitlab汉化:
1、下载汉化补丁
git clone https://gitlab.com/xhang/gitlab.git 2、查看全部分支版本
git branch ‐a
3、对比版本、生成补丁包
git diff remotes/origin/10‐2‐stable remotes/origin/10‐2‐stable‐zh > ../10.2.2‐zh.diff
4、停止服务器
gitlab‐ctl stop
5、打补丁
patch ‐d /opt/gitlab/embedded/service/gitlab‐rails ‐p1 < /tmp/10.2.2‐zh.diff
6、启动和重新配置
gitlab‐ctl start
gitlab‐ctl reconfigure
相关目录和命令
gitlab命令/opt/gitlab/ #gitlab的程序安装目录
/var/opt/gitlab # gitlab目录数据目录
/var/opt/gitlab/git‐dfata # 存放仓库数据
gitlab‐ctl status # 查看目前gitlab所有服务运维状态
gitlab‐ctl stop # 停止gitlab服务
gitlab‐ctl stop nginx # 单独停止某个服务 ,比如nginx
gitlab‐ctl tail # 查看所有服务的日志
登录测试
登录网址为之前设置的EXTERNAL_URL:http://10.0.0.200/

第一次登录需要修改密码

修改后即可登录,默认用户名为root

使用测试:
修改登录展示页面

修改后展示

创建组‐>创建用户‐>创建项目
创建组

创建项目


ssh认证

cat ~/.ssh/id_rsa.pub
将服务器的公钥复制过来

删除之前建立的github测试库
[root@localhost.localdomain ~/git_data/git_data]$ git remote
origin
[root@localhost.localdomain ~/git_data/git_data]$ git remote remove origin
[root@localhost.localdomain ~/git_data/git_data]$ git remote
添加gitlab的测试库并推送代码
git remote add origin git@10.0.0.200:test/git_data.git
git push -u origin master
touch test.txt
git add .
git commit -m "newfile test.txt"
git push -u origin master
推送成功

注册用户有2种方式
1.用户自主申请

2.管理员开通

用户创建后需要给用户初始设置密码

为注册的dev用户开通项目权限
需将用户(dev)加入到对应的组(test)

DEV用户测试:
DEV用户采用另一台服务器
dev用户仍需要添加ssh key才可以下载代码

添加ssh‐keys到gitlab 注:一个服务器的key只能添加到一个gitlab服务器上,一个用户可以添加多个key 生产key
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub

git init
git config --global user.name "dev"
git config --global user.email "dev@example.com"
克隆代码

git clone git@10.0.0.200:test/git_data.git
dev测试推送代码
cd git_data
touch dev
git add .
git commit -m "add dev"
git push -u origin master
dev测试推送分支
git branch dev
git checkout dev
git push -u origin dev
开启分支保护

为了使dev无法直接合并代码到master分支
gitlab新版本默认开启
旧版本需手动开启
开启分支保护后,push 到master会报错:You are not allowed to push code to protected branches on this project

开启分支保护后,如何上传?
git branch -d dev
git checkout -b dev
touch 456.txt
git add .
git commit -m "newfile 456.txt"
git push -u origin dev
dev用户登录,创建merge申请

root用户登录,审批merge

关闭自主申请注册
当前的设置,是可以自主注册用户的,关闭方法如下:


取消勾选即可
