1.代码自动上线—git

2021-08-02 分类:git-jenkins 阅读(445) 评论(0)

孙富阳, 江湖人称没人称。多年互联网运维工作经验,曾负责过孙布斯大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型博客网站运维工作。

一、git的基本使用

名字含义
git init初始化本地仓库目录
git config --global邮箱,用户名,颜色
git add提交数据到缓冲区(暂存区)
git add. (所有文件) 或 git add 文件
git commit把暂存区的数据提交到本地仓库 git commit -m "标记/说明"
git status显示工作空间的状态
git reset回滚
git reset --soft cid(版本号)把指定的版本数据内容下载到暂存区
git reset HEAD暂存区--->工作空间(被修改的状态)
git checkout文件下载到工作空间并可以使用 git checkout . 或 git checkout 文件
git reset --mix 版本号回退到工作区,颜色变红
git reset --hard版本号 把本地仓库指定版本信息数据下载到工作目录中
1.安装git
[root@git ~]# yum -y install git
[root@git ~]# git --version 
git version 1.8.3.1
2.创建git工作目录
[root@git ~]# mkdir  /opt/tast
[root@git ~]#cd /opt/tast
3.init初始化工作目录
[root@git /opt/tast]# git init 
  itialized empty Git repository in /opt/tast/.git/
[root@git /opt/tast]# ls
[root@git /opt/tast]# ls -a
.  ..  .git
4.开始开发代码
[root@git /opt/tast]# cat index.html
 这是我的第一个app: 孙某某的运维之路:v1
5.add 和commit提交代码##此处报错,要求提供用户名和邮箱,为了区分代码是谁写的
[root@git /opt/tast]# git add index.htnl
[root@git /opt/tast]# git commit -m '这是第一个app:66%'
*** Please tell me who you are.
Run
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@git.(none)')
6.按要求提交邮箱、用户名信息,并修改git提示颜色
[root@git /opt/tast]# git config --global user.email "2195802440@qq.com"
[root@git /opt/tast]# git config --global user.name  "孙富阳"
[root@git /opt/tast]# git config --global color.ui true
上图可以看到颜色发生变化
#红色代表工作区,指的是代码发生变化
#绿色代码暂存区,用户执行git add后进入暂存区
#本地版本代码仓库,status查看不到颜色,用户执行了commit -m
7.log查看提交的日志信息
[root@git /opt/tast]# git log 
commit 23f02c336312ca2c884f9f45f2d88cedf25a69e6
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:37:57 2021 +0800

    这是第一个app:66%
8.继续开发代码
[root@git /opt/tast]# vim index.html 
这是我的第一个app: 孙某某的运维之路:v1
这是我的第一个app: 孙某某的运维之路:v1.1
这是我的第一个app: 孙某某的运维之路:v2 
9.继续提交代码
 [root@git /opt/tast]# git add index.html
[root@git /opt/tast]# git commit -m '开发完成100%'
[master 8dccd73] 开发完成100%
 1 file changed, 1 insertion(+)
10.查看当前版本信息
[root@git /opt/tast]# git log
commit 8dccd73606f103f73c10b2eb4645c8c1b6785fae
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:45:30 2021 +0800
    开发完成100%
commit 23f02c336312ca2c884f9f45f2d88cedf25a69e6
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:37:57 2021 +0800
    这是第一个app:66%
11.reset代码回退#--hard后面跟log里的前几位即可
[root@git /opt/tast]# git reset --hard 23f02c3363
HEAD is now at 23f02c3 这是第一个app:66%
12.查看一下代码
[root@git /opt/tast]# cat index.html 
这是我的第一个app: 孙某某的运维之路:v1
13.查看全部的日志
##git log 发现代码版本都没了
[root@git /opt/tast]# git log
commit 23f02c336312ca2c884f9f45f2d88cedf25a69e6
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:37:57 2021 +0800
这是第一个app:66%
##可以通过reflog查看完整日志
[root@git /opt/tast]# git reflog 
23f02c3 HEAD@{0}: reset: moving to 23f02c3363
8dccd73 HEAD@{1}: commit: 开发完成100%
344e148 HEAD@{2}: commit: 开发完成70%
23f02c3 HEAD@{3}: commit (initial): 这是第一个app:66%
14.再次回到开发100%
[root@git /opt/tast]# git reset --hard 8dccd73
HEAD is now at 8dccd73 开发完成100%
[root@git /opt/tast]# cat index.html 
这是我的第一个app: 孙某某的运维之路:v1
这是我的第一个app: 孙某某的运维之路:v1.1
这是我的第一个app: 孙某某的运维之路:v2

二、git分支

git 分支相关命令 
git branch查看分支
git branch name创建分支
git branch -d name删除分支
git checkout分支名字 切换分支
git merge分支名字 合并(吸收)分支(把指定的分支合并到当前分支中)
git checkout -b name创建分支并切换到这个分支
1.创建和查看分支branch
[root@git /opt/tast]# git branch shopping
[root@git /opt/tast]# git branch 
* master
  shopping
2.切换分支checkout
[root@git /opt/tast]# git checkout shopping 
Switched to branch 'shopping'
[root@git /opt/tast]# git branch 
  master
* shopping
3.查看一下日志发现和主分支一致
[root@git /opt/tast]# git log
commit 344e148294e34bb2d47dcc78878d010f36b8f605
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:42:30 2021 +0800
    开发完成70%
commit 23f02c336312ca2c884f9f45f2d88cedf25a69e6
Author: 孙富阳 <2195802440@qq.com>
Date:   Wed Jul 28 17:37:57 2021 +0800
    这是第一个app:66%
4.在分支上编写代码
[root@git /opt/tast]# cat index.html 
1. 这是我的第一个app: 孙某某的运维之路:v1
1. 这是我的第一个app: 孙某某的运维之路:v1.1
[root@git /opt/tast]# vim index.html 
1. 这是我的第一个app: 孙某某的运维之路:v1
1. 这是我的第一个app: 孙某某的运维之路:v1.1
开发了第三个功能: shop 开发66%
5.分支上提交代码至代码仓库
[root@git /opt/tast]# git add index.html
[root@git /opt/tast]# git commit -m "shop 功能开发60%"
[shopping 125b5a6] shop 功能开发60%
 1 file changed, 4 insertions(+), 2 deletions(-)
6.此时发现代码有bug,切换回主分支创建一个修复bug的分支
[root@git /opt/tast]# git branch bugfix
[root@git /opt/tast]# git branch 
  bugfix
* master
  shopping
[root@git /opt/tast]# git checkout bugfix 
Switched to branch 'bugfix'
7.修复代码并提交
[root@git /opt/tast]# cat index.html 
1. 这是我的第一个app: 孙某某的运维之路:v1
1. 这是我的第一个app: 孙某某的运维之路:v1.1 bug修复100%
[root@git /opt/tast]# git add index.html
[root@git /opt/tast]# git commit -m "bug已经修复-c7"
[bugfix cd66223] bug已经修复-c7
 1 file changed, 1 insertion(+), 1 deletion(-)
8.切回主分支,并将修改好的代码合并merge
[root@git /opt/tast]# git checkout master 
Switched to branch 'master'
[root@git /opt/tast]# git merge bugfix 
Updating 344e148..cd66223
Fast-forward
 index.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@git /opt/tast]# cat index.html 
这是我的第一个app: 孙某某的运维之路:v1
这是我的第一个app: 孙某某的运维之路:v1.1 bug修复100%
9.这时候bug修复的分支没用了,删掉
[root@git /opt/tast]# git branch -d bugfix 
Deleted branch bugfix (was e47105e).
[root@git /opt/tast]# git branch
* master
  shopping

三、git远程仓库gitee(码云)

登录https://gitee.com/注册码云账号
点击仓库-右上角+号点击创建仓库
填写相关信息后点击创建即可
根据创建完成返回的的页面依次执行如下命令
1.添加远程仓库地址至本地remote add origin
[root@git /opt/tast]# git remote add origin git@gitee.com:sun-fuyang/gits.git
2.推送本地仓库代码至远程仓库发现报错
因为ssh连接,所以需要将本机公钥上传至码云
点击右上角个人头像-点击设置-点击ssh公钥,将公钥复制进去
再次上传,执行成功
[root@git /opt/tast]# git push -u origin master
Counting objects: 12, done.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 1.04 KiB | 0 bytes/s, done.
Total 12 (delta 3), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.0]
To git@gitee.com:sun-fuyang/gits.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
3.git从远程仓库下载代码到本地仓库
生成公钥并将公钥上传至码云
##克隆(拉取)代码至本地
$ git clone git@gitee.com:sun-fuyang/gits.git
#会发现桌面多了个目录进到目录里
$ cd gits/
$ ls -a
./  ../  .git/  index.html
#查看当前分支发现shopping分支不见了,切换shopping分支报错
86151@LAPTOP-P2J23O9Q MINGW64 ~/Desktop/gits (master)
$ git branch
* master

86151@LAPTOP-P2J23O9Q MINGW64 ~/Desktop/gits (master)
$ git checkout shopping
Switched to a new branch 'shopping'
Branch 'shopping' set up to track remote branch 'shopping' from 'origin'.
但是再次查看发现已经切换至shopping分支
86151@LAPTOP-P2J23O9Q MINGW64 ~/Desktop/gits (shopping)
$ git branch
  master
* shopping
4.git标签tag
#指定某个commit打标签
[root@git /opt/tast]# git tag -a "v0.1" -m "0.1版本开发60%" 125b5a6b819a
#默认给最新提交的commit打标签
[root@git /opt/tast]# git tag -a "v0.1" -m "0.1版本开发60%" 
#查看打的版本标签
[root@git /opt/tast]# git tag -l
1.0
v0.1
5.git忽略不需要提交至代码仓库的文件
编辑隐藏文件.gitignore
文件内容支持通配符,例如忽略以jpg结尾的文件

评论已关闭

登录

忘记密码 ?

切换登录

注册

鲁ICP备2021019243号-1