第3章 GitLab小试身手
1. 关闭 Auto DevOps 功能
为什么要关闭
这个功能是 GitLab 自己的流水线功能,我们使用 Jenkins 就不需要开启它,开启后会很不方便,所以我们先关闭它。
2. 创建一个简单项目
3. 添加公钥
SSH 公钥说明
GitLab 使用 SSH 协议来传输代码,所以我们需要将开发者电脑的 SSH 公钥提前配置在项目里,这样开发者才可以将代码上传到 GitLab 上。当我们创建好一个项目,但是没有配置公钥时,GitLab 页面上会提醒我们去添加公钥。
4. 克隆代码
现在我们项目已经创建好了,并且也将开发的公钥上传到了 GitLab 上,接下来可以在开发的电脑上下载代码仓库上的代码了。克隆地址在项目的首页上。
安装并配置 Git
[root@m-61 ~]# yum install git -y
[root@m-61 ~]# git config --global user.email "admin@qq.com"
[root@m-61 ~]# git config --global user.name "zhangya"
[root@m-61 ~]# git config --global color.ui true
[root@m-61 ~]#
克隆代码
[root@m-61 ~]# cd /opt/
[root@m-61 /opt]# git clone git@10.0.0.200:root/game.git
Cloning into 'game'...
The authenticity of host '10.0.0.200 (10.0.0.200)' can't be established.
ECDSA key fingerprint is SHA256:4bnum+vhLl+fHDM+WUxdjAs9Jf48mKwMHvpxkKT+FEY.
ECDSA key fingerprint is MD5:8b:cc:8c:66:84:af:09:b7:5c:67:40:17:69:50:3a:73.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.200' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
查看克隆结果
[root@m-61 /opt]# ll game/
total 8
-rw-r--r-- 1 root root 6144 Jun 26 12:32 README.md
5. Git 本地开发新代码
查看当前状态
[root@m-61 /opt]# cd game/
[root@m-61 /opt/game]# git status
# On branch main
nothing to commit, working directory clean
创建新的代码文件
[root@m-61 /opt/game]# echo "hello Linux" > index.html
[root@m-61 /opt/game]# git status
# On branch main
# 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@m-61 /opt/game]# git add .
[root@m-61 /opt/game]# git commit -m "add v1.0"
[main bd4b03e] add v1.0
1 file changed, 1 insertion(+)
create mode 100644 index.html
查看提交后状态
[root@m-61 /opt/game]# git status
# On branch main
# Your branch is ahead of 'origin/main' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
注意事项
此时我们只是提交到了本地的版本仓库,而 GitLab 远程仓库上的 main 分支还是旧的版本,所以这里我们还需要将本地的代码提交到远程仓库上。
推送代码到远程仓库
[root@m-61 /opt/game]# 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)
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.200:root/game.git
780a1f1..bd4b03e main -> main
命令解释
git push
- Git 推送命令origin
- 远程仓库名称(默认为 origin)main
- 要推送到的远程分支名
工作中的最佳实践
这里我们是直接推送到远程的 main 分支,工作中我们并不会这么操作,因为如果谁都有权限提交到 main 分支这样风险很大。
工作中的正确做法:
- 开发人员在开发分支(如 feature/xxx)上开发
- 提交到远程的开发分支
- 通过 Merge Request 由管理员审核后合并到 main 分支
此时我们查看 GitLab 上的代码库可以发现,已经提交成功了。
更新: 2024-06-30 21:27:49