Linux-git

git

一、ci-cd-co基础

1.代码上线流程

代码上线流程:

开发书写代码

把代码上传到代码仓库

通过工具拉取代码到测试环境中,运行代码,测试人员测试.

通过工具拉取代码部署到生产环境.

2. ci/cd

ci: 持续集成: 开发的代码集成到代码仓库。 (Continuous Integratuin)

cd: 持续交付: 从代码仓库拉取代码部署到测试环境。 (Continuous Delivery)

cd: 持续部署: 从代码仓库拉取代码部署到生产环境。(Continuous Deplayment)

3. DevOps

DevOps:

理念让开发人员可以持续集成,运维人员可以持续交付和持续部署.让整个开发过程自动部署,上线 开发写完的代码自动集成,自动交付与部署。本质是自动化。

DevOps是一套快速、频繁、可靠的软件交付实践.

DevSecOps

  • Development 开发
  • Sec安全
  • Operation 运维

DevOPS目标是解决开发书写代码,集成,交付,部署进度,审核缓慢.

DevOPS实现

  • 开发(存放收集代码 CI): git/gitlab/github/gitee(代码平台)
  • 运维(部署代码 CD): jenkins maven ....插件 sonarqube (运维)

image-20230326111249538

二、环境

开发环境

测试环境

预发布环境/准生产环境/预生产环境

生产环境

三、代码部署

代码发布/部署 >开发书写的程序代码---->部署测试/生产环境 web服务器(站点目录中)

四、代码发布方式

发布方式 效率 干扰 安全性
手动发布 干扰因素很多 不安全
自动发布 干扰因素很少 安全

五、git极速入门

1.环境准备

角色 主机名 ip 内存
gitlab私有代码仓库 gitlab 10.0.0.71/172.16.1.71 1C4G
jenkins jenkins 10.0.0.72/172.16.1.72 1C4G
sonarqube代码检查 sonar 10.0.0.73/172.16.1.73 1c1G
nexus私服 nexus 10.0.0.74/172.16.1.74 1c1G

2.git

Git SVN
共同点 存放代码,版本控制 存放代码,版本控制
工作模式 分布式 中心化(权限集中)
使用 入门较难,熟练后容易使用,目前使用率高 入门比较简单,服务端linux,客户端windows
分支 创建和维护分支方便 创建和维护分支繁琐

3.多系统部署

3.1 win

https:git-scm.com/download/

3.2 Linux(CentOS/Fedora/Rocky Linux/RHEL)

yum install -y git

3.3 MacOS

#需要提前安装brew
brew install git

3.4 Ubuntu/Debian

apt install -y git

4.配置git

#配置全局变量
[root@gitlab ~]# git config --global user.name '{用户名}'
[root@gitlab ~]# git config --global user.email '{邮箱}'
[root@gitlab ~]# git config --global color.ui true
[root@gitlab ~]# git config --global --list
user.name={用户名}
user.email={邮箱}
color.ui=true
#创建目录并且初始化
[root@gitlab ~]# mkdir -p /app/code/live-app
[root@gitlab ~]# cd /app/code/live-app/
[root@gitlab /app/code/live-app]# git init
Initialized empty Git repository in /app/code/live-app/.git/
[root@gitlab /app/code/live-app]# ll -a
total 0
drwxr-xr-x 3 root root 18 Mar 26 11:43 .
drwxr-xr-x 3 root root 22 Mar 26 11:42 ..
drwxr-xr-x 7 root root 119 Mar 26 11:43 .git
#书写代码,提交到暂存区
[root@gitlab /app/code/live-app]# echo "项目启动" >index.html
[root@gitlab /app/code/live-app]# 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@gitlab /app/code/live-app]# git add .
[root@gitlab /app/code/live-app]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
#
#提交到本地仓库
[root@gitlab /app/code/live-app]# git commit -m "项目启动"
[master (root-commit) 0b9425e] 项目启动
1 file changed, 1 insertion(+)
create mode 100644 index.html
[root@gitlab /app/code/live-app]# git status
# On branch master
nothing to commit, working directory clean
#回滚
[root@gitlab /app/code/live-app]# rm -f index.html
[root@gitlab /app/code/live-app]# ll
total 0
[root@gitlab /app/code/live-app]# git checkout .
[root@gitlab /app/code/live-app]# ll
total 4
-rw-r--r-- 1 root root 13 Mar 26 11:46 index.html
名字 含义
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 版本号 把本地仓库指定版本信息数据下载到工作目录中

5.git区域与状态

image-20230326114855794

6.git分支 branch

#查看分支
[root@gitlab /app/code/live-app]# git branch
* master
#创建分组
[root@gitlab /app/code/live-app]# git branch shopping
#查看分支
[root@gitlab /app/code/live-app]# git branch
* master
shopping
#切换分支
[root@gitlab /app/code/live-app]# git checkout shopping
Switched to branch 'shopping'
#查看分支,*在哪一行表示当前在哪个分支
[root@gitlab /app/code/live-app]# git branch
master
* shopping
#在shopping分支创建内容
[root@gitlab /app/code/live-app]# echo "shopping 分支" >shopping.html
#提交html到shopping分支
[root@gitlab /app/code/live-app]# git add .
[root@gitlab /app/code/live-app]# git commit -m "shopping 分支"
[shopping b9f70ac] shopping 分支
1 file changed, 1 insertion(+)
create mode 100644 shopping.html
#切换分支到master
[root@gitlab /app/code/live-app]# git checkout master
Switched to branch 'master'
[root@gitlab /app/code/live-app]# ll
total 4
-rw-r--r-- 1 root root 13 Mar 26 11:46 index.html
#合并分支
[root@gitlab /app/code/live-app]# git merge shopping
Updating 0b9425e..b9f70ac
Fast-forward
shopping.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 shopping.html
[root@gitlab /app/code/live-app]# ll
total 8
-rw-r--r-- 1 root root 13 Mar 26 11:46 index.html
-rw-r--r-- 1 root root 16 Mar 26 11:55 shopping.html
git分支
git branch 查看分支
git branch name 创建分支
git branch -d name 删除分支
git checkout 分支名字 切换分支
git merge 分支名字 合并(吸收)分支(把指定的分支合并到当前分支中)
git checkout -b name 创建分支并切换到这个分支

六、创建远程仓库

公共仓库:gitee.com/github.com

私有仓库:gitlab,gogs

1.创建仓库

image-20230326120221471

image-20230326120335015

2.连接远程仓库并上传代码(用户名密码认证方式)

image-20230326120519184

#设置远程仓库的地址
[root@gitlab /app/code/live-app]# git remote add origin https://gitee.com/{git用户名}/live_app.git
#查看仓库
[root@gitlab /app/code/live-app]# git remote -v
origin https://gitee.com/{git用户名}/live_app.git (fetch)
origin https://gitee.com/{git用户名}/live_app.git (push)
#推送本地仓库的数据到远程仓库
[root@gitlab /app/code/live-app]# git push -u origin master
Username for 'https://gitee.com': git用户名
Password for 'https://{git用户名}@gitee.com':
Counting objects: 6, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 495 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/{git用户名}/live_app.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

image-20230326120910577

3.拉取代码

git clone https://gitee.com/{git用户名}/live_app.git

4. 连接远程仓库(密钥认证方式)

#创建秘钥
[root@gitlab ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:L+amDaPhtlOj28a4FksvbvFPwAAGB4GYrF9DDZJTE2U root@gitlab
The key's randomart image is:
+---[RSA 2048]----+
|B==o==E |
|++oo.o. |
|. o. |
|. oo |
| . . .o S |
| . + o. . |
| ..@o.+ . |
| .X+=B.. |
| =BBo++ |
+----[SHA256]-----+
[root@gitlab ~]# cat .ssh/id_rsa.pub
ssh-rsa {秘钥内容} root@gitlab
#将公钥粘贴到gitee里

image-20230326121457738

#移除现有仓库
[root@gitlab /app/code/live-app]# git remote remove origin
[root@gitlab /app/code/live-app]# git remote -v
#添加新的git方式的仓库
[root@gitlab /app/code/live-app]# git remote add origin git@gitee.com:{git用户名}/live_app.git
[root@gitlab /app/code/live-app]# git remote -v
origin git@gitee.com:{git用户名}/live_app.git (fetch)
origin git@gitee.com:{git用户名}/live_app.git (push)
#提交代码到本地仓库
[root@gitlab /app/code/live-app]# echo "秘钥认证" >>index.html
[root@gitlab /app/code/live-app]# git add .
[root@gitlab /app/code/live-app]# git commit -m "秘钥认证"
[master 744ce21] 秘钥认证
1 file changed, 1 insertion(+)
#提交代码到远程仓库
[root@gitlab /app/code/live-app]# git push origin
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
The authenticity of host 'gitee.com (ip)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
ECDSA key fingerprint is MD5:27:e5:d3:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,ip' (ECDSA) to the list of known hosts.
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 310 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:{git用户名}/live_app.git
b9f70ac..744ce21 master -> master
代码仓库访问方法 说明
https 每次连接的时候需要输入账号的用户名和密码
ssh 必备常用. 密钥认证

5.给代码添加标签tag

tag标签:给commit id设置别名,方便我们快速查看与使用,一般tag标签表示软件的版本

[root@gitlab /app/code/live-app]# git tag -a v1.0 -m "v1.0"
[root@gitlab /app/code/live-app]# git push -u origin --tags
Warning: Permanently added the ECDSA host key for IP address 'ip' to the list of known hosts.
Counting objects: 1, done.
Writing objects: 100% (1/1), 147 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To git@gitee.com:{git用户名}/live_app.git
* [new tag] v1.0 -> v1.0

image-20230326122410619

6.gitignore

上传代码的时候,代码中可能包含临时文件cache .swp 排除

在代码根目录中创建文件.gitignore 写上要排除的内容

忽略上传到本地仓库的内容

[root@gitlab /app/code/live-app]# cat .gitignore
#*.jpg
*.tmp
cache

应用场景

  1. 程序运行时产生的垃圾文件(临时文件)
  2. 程序运行时产生的缓存文件(临时文件)
  3. 程序本地开发使用的图片文件
  4. 程序连接数据一类的配置文件(什么时候用,什么时候配置)

本文作者:wh459086748

本文链接:https://www.cnblogs.com/world-of-yuan/p/17270558.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   wh459086748  阅读(165)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起