一.GIT基础

1.1 git简介

  • linus用C语言编写
  • 2005年诞生
  • 分布式管理系统
  • 速度快、适合大规模、跨地区多人协同开发

1.2 本地管理、集中式、分布式

1.3 git安装

 #CentOS上安装
[root@linux-node1 ~]# yum -y install git
#Ubuntu上安装
[root@linux-node1 ~]# apt-get install git

注:生产环境中,不建议这么安装,yum装的git版本是1.8,推荐使用2.7版本


编译安装:

 #安装依赖包
[root@linux-node1 ~]# yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker
#下载安装包
[root@linux-node1 ~]# wget https://github.com/git/git/archive/v2.7.4.zip
#解压
[root@linux-node1 ~]# unzip git-2.7./
#进入git目录
[root@linux-node1 ~]# cd git-2.7./
#编译
[root@linux-node1 git-2.7.]# make prefix=/usr/local/git all
#安装
[root@linux-node1 git-2.7.]# make prefix=/usr/local/git install
#删除原有git命令
[root@linux-node1 git-2.7.]# rm -rf /usr/bin/git
#软连接编译安装git命令
[root@linux-node1 git-2.7.]# ln -s /usr/local/git/bin/git /usr/bin/git
#查看git版本
[root@linux-node1 git-2.7.]# git --version
git version 2.7.

1.4 git初始化

 #创建gittest目录
[root@linux-node1 ~]# mkdir gittest
#进入目录
[root@linux-node1 ~]# cd gittest
#初始化git仓库
[root@linux-node1 gittest]# git init
Initialized empty Git repository in /root/gittest/.git/
#配置基础用户信息
[root@linux-node1 gittest]# git config --global user.name "goodcook"
#配置基础用户邮件信息
[root@linux-node1 gittest]# git config --global user.email "goodcook@qq.com"

1.5 查看基本信息

 #查看基本信息
[root@linux-node1 gittest]# git config --list
user.name=goodcook
user.email=goodcook@qq.com
core.repositoryformatversion=
core.filemode=true
core.bare=false
core.logallrefupdates=true

1.6 git区域

  • 远程仓库

  • 本地仓库

  • 暂存区域

  • 工作目录

1.7 四种状态

  git如何进行版本管理的呢?它对管理的文件,会打上一个标识,这个标识,就分为四种状态。

  • untracked(未被追踪的文件)

   第一次放到库中的文件,文件与库没有任何的关联,还没有纳入系统管理版本的序列中。使用 git add将该类文件推送到暂存区,状态就变成了staged。

  • unmodified(未被修改的文件)

  未被修改的文件,增加新代码(修改)之后,会重新拉倒工作目录中。

  • modified(修改后的文件)

  修改之后,将该文件git add加入到暂存区,再通过commit放入本地库中。

  • staged(暂存区的文件)

  使用git commit将该类文件提交,变成unmodified(未被修改的文件),从暂存区,将文件放到本地仓库中。

流程:

1.新文件,放到git目录中(untracked 未被追踪的文件)

2.使用git add将未被追踪的文件推送到暂存区(staged 暂存区文件)

3.使用git commit将暂存区文件提交到本地仓库中(unmodified 未被修改的文件)

4.此时如果修改文件(modified 修改的文件),然后再用git add将修改后的文件加入到暂存区(staged)

5.然后再提交git commit到本地仓库变成(unmodified 未被修改的文件)...周而复始

1.8 git常用命令

git add            加入暂存区(索引区)

git status           查看状态

git status -s         状态概览

git diff            尚未暂存的文件

git diff --staged       暂存区文件

git commit           提交更新

git reset           回滚

git rm             从版本库中移除

git rm --cached         从暂存区中移除

git mv             相当于mv 、git rm 、git add三个命令

 #创建一个文件
[root@linux-node1 gittest]# touch index.html
#编辑index.html
[root@linux-node1 gittest]# vim index.html
<h1> welcome to my index </h1>
#使用git status查看该文件状态
[root@linux-node1 gittest]# git status
  On branch master   Initial commit   Untracked files: #此时状态为未被追踪的文件
    (use "git add <file>..." to include in what will be committed)         index.html   nothing added to commit but untracked files present (use "git add" to track)
#加入代码库
[root@linux-node1 gittest]# git add index.html
#再次查看状态
[root@linux-node1 gittest]# git status
  On branch master   Initial commit   Changes to be committed:
    (use "git rm --cached <file>..." to unstage)         new file:   index.html
#提交
[root@linux-node1 gittest]# git commit -m "first commit"
[master (root-commit) 6f3aca3] first commit
file changed, insertion(+)
create mode index.html
#再次查看状态
[root@linux-node1 gittest]# git status
  On branch master
  nothing to commit, working directory clean #此时的状态是工作目录中没有还没有提交的文件了

二.分支管理

2.1 创建一个分支

 #创建一个分支
[root@linux-node1 gittest]# git branch about
#查看状态
[root@linux-node1 gittest]# git status
On branch master #还是在master上
#切换分支
[root@linux-node1 gittest]# git checkout about
Switched to branch 'about'
#查看状态
[root@linux-node1 gittest]# git status
On branch about

2.2 分支命令

git branch            查看分支

git branch -v           详细查看

git branch --merged        查看哪些分支已经被融合

git branch --no-merged      查看哪些分支还没有被融合

git branch -d           删除分支

git checkout           切换分支

git merge             融合分支(将分支代码融合到主干)

git log              查看提交事件

git stash             暂存区

git tag              打标签

三.远程仓库

3.1 将github上的代码拉到本地

 #拉代码
[root@linux-node1 ~]# git clone https://github.com/zzgxgit/saltstack-apache.git
Cloning into 'saltstack-apache'...
remote: Counting objects: , done.
remote: Total (delta ), reused (delta ), pack-reused
Unpacking objects: % (/), done.
Checking connectivity... done.
#进入库目录
[root@linux-node1 ~]# cd saltstack-apache/
#查看状态
[root@linux-node1 saltstack-apache]# git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

3.2 修改文件并push

 #查看目录内容
[root@linux-node1 saltstack-apache]# ll
total
drwxr-xr-x root root May : modules
-rw-r--r-- root root May : README.md
#编辑README.md
[root@linux-node1 saltstack-apache]# vim README.m
#增加一行内容
test git push
#添加到staged
[root@linux-node1 saltstack-apache]# git add .
#提交
[root@linux-node1 saltstack-apache]# git commit -m "edit README.md"
[master 6370f89] edit README.md
file changed, insertion(+)
#远程库
[root@linux-node1 saltstack-apache]# git remote
origin
#查看详细信息
[root@linux-node1 saltstack-apache]# git remote -v
origin https://github.com/zzgxgit/saltstack-apache.git (fetch)
origin https://github.com/zzgxgit/saltstack-apache.git (push)
#推送到远程库
[root@linux-node1 saltstack-apache]# git push origin master
#输入账号
Username for 'https://github.com': zzgxgit
#输入密码
Password for 'https://zzgxgit@github.com':
Counting objects: , done.
Compressing objects: % (/), done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To https://github.com/zzgxgit/saltstack-apache.git
a6443a5..6370f89 master -> master

3.3 给本地库添加远程信息

 #进入之前初始化的本地库
[root@linux-node1 ~]# cd gittest/
#查看远程信息(没有任何信息)
[root@linux-node1 gittest]# git remote
[root@linux-node1 gittest]# git remote -v
#添加远程库信息
[root@linux-node1 gittest]# git remote add origin http://192.168.100.1/goodcook.git
#查看信息
[root@linux-node1 gittest]# git remote
origin
[root@linux-node1 gittest]# git remote -v
origin http://192.168.100.1/goodcook.git (fetch)
origin http://192.168.100.1/goodcook.git (push)
#可以添加多个远程库
[root@linux-node1 gittest]# git remote add gitlab http://192.168.100.2/goodcook.git
#查看信息
[root@linux-node1 gittest]# git remote
gitlab
origin
[root@linux-node1 gittest]# git remote -v
gitlab http://192.168.100.2/goodcook.git (fetch)
gitlab http://192.168.100.2/goodcook.git (push)
origin http://192.168.100.1/goodcook.git (fetch)
origin http://192.168.100.1/goodcook.git (push)

3.4 手动打标签

 #查看标签(为空)
[root@linux-node1 gittest]# git tag
#查看一下提交信息
[root@linux-node1 gittest]# git log
commit 6f3aca32bfe6caeb69be9c8b4caa856eedd495ed
Author: goodcook <goodcook@qq.com>
Date: Thu May :: + first commit
#打标签(添加一个1.0的版本信息)
[root@linux-node1 gittest]# git tag -a v1. -m "version"
#查看标签
[root@linux-node1 gittest]# git tag
v1.

四. gitlab安装配置

4.1 安装gitlalb

 #安装依赖
[root@linux-node1 ~]# yum -y install curl policycoreutils openssh-server openssh-clients
#sshd开机自启
[root@linux-node1 ~]# systemctl enable sshd
#启动sshd
[root@linux-node1 ~]# systemctl start sshd
#安装postfix
[root@linux-node1 ~]# yum install postfix
#设置开机自启
[root@linux-node1 ~]# systemctl enable postfix
#启动
[root@linux-node1 ~]# systemctl start postfix
#安装gitlab8.9.5
[root@linux-node1 ~]# rpm -ivh gitlab-ce-8.9.-ce..el7.x86_64.rpm
  Preparing...                          ################################# [100%]
  Updating / installing...
     1:gitlab-ce-8.9.5-ce.0.el7         ################################# [100%]

4.2 配置文件

 #编辑配置文件
[root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb
#修改URL(没有域名就用IP)
external_url 'http://192.168.56.11'
#自动化配置
[root@linux-node1 ~]# gitlab-ctl reconfigure

4.3 访问gitlab

打开浏览器,输入刚才配置的URL:192.168.56.11

设置一个密码,不能太短:ABCD1234

【登录】

用户名:root

密码:ABCD1234

至此,gitlab就安装完成了。

4.4 gitlab常用命令

gitlab-ctl status            查看各组件状态

gitlab-ctl start             开启服务

gitlab-ctl stop             停止服务

gitlab-ctl restart            重启服务

gitlab-ctl tail <service>        查看某一组件日志  例:gitlab-ctl tail nginx

4.5 gitlab组件介绍

nginx                  静态web服务器

gitlab-shell               用于处理git命令和修改authorized keys列表

logrotate                日志文件管理工具

gitlab-workhorse             轻量级的反向代理服务器

postgresql                数据库

redis                  缓存

sidekiq                 用于在后台执行队列任务(异步执行)

unicorn                 gitlab rails应用是托管在这个服务器上面的

4.6 gitlab常用目录介绍

/var/opt/gitlab/git-data/repositories/root/      库默认存储目录

/opt/gitlab/                      应用代码和相应的依赖程序

/var/opt/gitlab/                    使用gitlab-ctl reconfigure命令编译后的应用数据和配置文件,不需要人为修改配置

/etc/gitlab/                      配置文件目录

/var/log/gitlab/                    存放各个组件的日志目录

/var/opt/gitlab/backups/                备份文件生成的目录

五.gitlab管理操作

5.1创建工程

单击右上角的扳手图标

单击new group 创建一个组

创建组

创建用户

按要求创建用户按照此法 创建 dev1和dev2用户

单机进去,查看用户

回到组的页面

给 pm 用户授权为 master

进入项目页面

创建一个java1的项目

设置权限

配置ssh-key

 #创建一个秘钥对
[root@linux-node1 .ssh]# ssh-keygen
#查看公钥并拷贝
[root@linux-node1 .ssh]# cat id_rsa.pub

将公钥加入后点击Add key

六. gitlab备份和恢复

6.1 配置文件

 #编辑配置文件
[root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb
#备份目录
gitlab_rails['backup_path'] = "/data/backups/gitlab"
#备份保留7天
gitlab_rails['backup_keep_time'] =
#创建备份目录
[root@linux-node1 ~]# mkdir -p /data/backups/gitlab
#改完配置重新自动化生成
[root@linux-node1 ~]# gitlab-ctl reconfigure
#重启服务
[root@linux-node1 ~]# gitlab-ctl restart
#授权
[root@linux-node1 ~]# chown -R git:git /data/backups/gitlab/
#添加定时任务
[root@linux-node1 ~]# crontab -e
#每天2点全备一次
* * * /usr/bin/gitlab-rake gitlab:backup:create &>/dev/null

6.2 备份测试

 #手动执行一次
[root@linux-node1 ~]# gitlab-rake gitlab:backup:create
#查看备份
[root@linux-node1 ~]# ll /data/backups/gitlab/
  total 40
  -rw------- 1 git git 40960 May  5 03:37 1493926679_gitlab_backup.tar

6.3 恢复数据

6.3.1删除项目

6.3.2 恢复操作

 #要停两个服务
[root@linux-node1 ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@linux-node1 ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 1s, normally up
#恢复备份
[root@linux-node1 ~]# gitlab-rake gitlab:backup:restore BACKUP=
#备份完,启动服务
[root@linux-node1 ~]# gitlab-ctl start unicorn
ok: run: unicorn: (pid ) 1s
[root@linux-node1 ~]# gitlab-ctl start sidekiq
ok: run: sidekiq: (pid ) 0s

打开页面:


注:恢复操作中的 “BACKUP=1493926679”  是备份时候生成的备份文件名前面的时间戳。


七. gitlab邮件配置

7.1 配置文件

 #编辑配置文件
[root@linux-node1 ~]# vim /etc/gitlab/gitlab.rb
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'goodcook@126.com'
gitlab_rails['gitlab_email_display_name'] = 'gitlab'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.126.com"
gitlab_rails['smtp_port'] =
gitlab_rails['smtp_user_name'] = "goodcook"
gitlab_rails['smtp_password'] = "your_password"
gitlab_rails['smtp_domain'] = "126.com"
gitlab_rails['smtp_authentication'] = "login"

【开源是一种精神,分享是一种美德】

— By GoodCook

— 笔者QQ:253097001

— 欢迎大家随时来交流

—原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。

【持续集成】GIT+jenkins+sonar——GIT的更多相关文章

  1. 老李分享:持续集成学好jenkins之Git和Maven配置

    老李分享:持续集成学好jenkins之Git和Maven配置   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  2. 老李分享:持续集成学好jenkins之Git和Maven配置 1

    老李分享:持续集成学好jenkins之Git和Maven配置   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  3. 老李分享:持续集成学好jenkins之Git和Maven配置 2

    8.检查代码更新并编译 可以通过Poll SCM来设置定时检查编译功能 比如*/5 * * * * 就是每隔5分钟检查一次,如果git仓库中有更新,则执行build操作. 9.使用Maven私服加快下 ...

  4. 【持续集成】jenkins安装部署从git获取代码

    一:持续集成的概念: 1.1:总体的概括 持续集成Continuous Integration 持续交付Continuous Delivery 持续部署Continuous Deployment 1. ...

  5. 持续集成之jenkins

    代码部署规划 安装jenkins yum -y install java-1.8.0cd /etc/yum.repos.d/wget http://pkg.jenkins.io/redhat/jenk ...

  6. Android持续集成之Jenkins 部署

    Android持续集成之Jenkins 部署 [TOC] 0x00安装 准备工作如下: Tomcat8.5下载地址 Jenkins下载链接 1 将下载的jenkins.war包放至tomcat下的we ...

  7. 持续集成之⑤:jenkins结合脚本实现代码自动化部署及一键回滚至上一版本

    持续集成之⑤:jenkins结合脚本实现代码自动化部署及一键回滚至上一版本 一:本文通过jenkins调用shell脚本的的方式完成从Git服务器获取代码.打包.部署到web服务器.将web服务器从负 ...

  8. 持续集成之Jenkins+Gitlab实现持续集成 [二]

    持续集成之Jenkins+Gitlab实现持续集成 [二] 项目:使用git+jenkins实现持续集成 开始构建  General  源码管理 我们安装的是Git插件,还可以安装svn插件  我们将 ...

  9. 有手就行2——持续集成环境—Jenkins安装、插件、用户权限及凭证管理

    有手就行2--持续集成环境-Jenkins安装.插件.权限及凭证管理 持续集成环境(1)-Jenkins安装 持续集成环境(2)-Jenkins插件管理 持续集成环境(3)-Jenkins用户权限管理 ...

随机推荐

  1. iOS:给标签栏控制器的UITabbarItem添加点击动效

    一.介绍 现在很多app,附带很炫的点击效果,让用户享受到非常棒的体验,例如动画.渐变.音效等. 当然,市面上大多数app的标签栏点击还是挺中规中矩的,只是切换图片而已.然而,这个是可以优化的,附带点 ...

  2. Coursera机器学习+deeplearning.ai+斯坦福CS231n

    日志 20170410 Coursera机器学习 2017.11.28 update deeplearning 台大的机器学习课程:台湾大学林轩田和李宏毅机器学习课程 Coursera机器学习 Wee ...

  3. 关于jmeter命令行执行.jmx文件出现Error in NonGUIDriver java.lang.RuntimeException: Could not find the TestPlan c

     

  4. C# System.IO.FileStream

    为文件提供 Stream,既支持同步读写操作,也支持异步读写操作. using System; using System.IO; using System.Text; class Test { pub ...

  5. 【php】php输出jquery的轮询,5秒跳转指定url

    1.在php中直接输出jquery的轮询,5秒后跳转指定url 2.代码稍微改动,即可在html中使用 3.代码: public function alpha(){ $html = '<!DOC ...

  6. solr+zookeeper集群配置

    将solr配置文件交给zookeeper进行管理 ./zkcli.sh -zkhost node01:2181,node02:2181,node03:2181 -cmd upconfig -confd ...

  7. Ubuntu安装守护进程supervisor

    Supervisor安装与配置(Linux/Unix进程管理工具) asp.net core 负载均衡集群搭建(centos7+nginx+supervisor+kestrel) 为了保证服务能够稳定 ...

  8. 飞思卡尔单片机P&E开发工具硬件及软件

    原文链接: http://blog.sina.com.cn/s/blog_8ebff8d7010121tm.html 1.HC(S)08系列 开发机硬件:USB-ML-12 CYCLONE PRO U ...

  9. springboot 项目中读取资源文件内容 如图片、文档文件

    1 问题描述:在 springboot 项目中有时候会需要读取一些资源文件,例如 office的 docx 文档或者 png.jpg的图片.在多模块项目中资源文件需要放到启动项目的 Resources ...

  10. Atitit s2018.5 s5 doc list on com pc.docx  Acc 112237553.docx Acc baidu netdisk.docx Acc csdn 18821766710 attilax main num.docx Atiitt put post 工具 开发工具dev tool test.docx Atiitt 腾讯图像分类相册管家.docx

    Atitit s2018.5 s5  doc list on com pc.docx  Acc  112237553.docx Acc baidu netdisk.docx Acc csdn 1882 ...