
至此第一个代码仓库就创建完成了,不过里面是空的。接下来上传代码
项目创建完成后,需要将游戏代码上传到此项目
服务器和代码仓库连接方式有两种
第一种: 基于用户名和密码的方式连接
第二种: 基于SSH免秘钥方式(和命令行的SSH免秘钥相同)
我们使用基于免秘钥方式: 方便+安全性高
[root@200-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:bLxPOzTxS2TnnMi0cGPUd0L4sUpnQ5VL0QoPx7YWuXs root@200-gitlab The key's randomart image is: +---[RSA 2048]----+ | =.+=| | = @o+| | . O.Xo| | o o O &. | | S % % + | | . .o B = E| | ...o . . | | o... | | o. | +----[SHA256]-----+ ##查看秘钥对 [root@200-gitlab ~]# ll .ssh/ 总用量 8 -rw------- 1 root root 1675 11月 15 22:44 id_rsa #私钥 -rw-r--r-- 1 root root 397 11月 15 22:44 id_rsa.pub #公钥 [root@200-gitlab ~]# cat .ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxGRglMUgFFMXaZ/KXl/ukqHOZcRTCzZZHWD97KusxFrUG3fUIw01IxvEah9uuEbSemTrVXo23FLSOSbZOxbVAeRcZl5wsxn1P070jjliM5ECk0PXe3S/tSPmioxVV2oFTVNhutDGAzMzTNRIpUunLKK/cIw9qbwB4aFnok78h6nqroPl68q854cf0PVD/p9FgrT+1MHGrgl/2V8EfGTfy8vGJFx2ffS9fzLnB7CG+HdqLfaC6WWOpAj8+4DRV2YTAAJ12tcwrs2ZkYn8XzGg8G/ruFs3uG1AZ07aU5ntm7D5hIUzev85Bh6RIE72uLtwbVd8fM7F7gARQeb8UYah3 root@200-gitlab
##注意是用户设置里面的SSH秘钥

至此就完成了服务器和gitlab页面之间的SSH免秘钥,接下来将代码推送到game项目中
推送代码的方式有两种:
进入到项目中找到项目的链接地址
1##克隆game仓库至本地 [root@200-gitlab ~]# git clone git@10.0.0.200:ops/game.git 正克隆到 'game'... remote: Enumerating objects: 7, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (5/5), done. remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 接收对象中: 100% (7/7), done. 2##查看game仓库下载到了本地 [root@200-gitlab ~]# ll -a game/ 总用量 16 drwxr-xr-x 3 root root 48 11月 15 23:54 . dr-xr-x---. 10 root root 4096 11月 15 23:54 .. -rw-r--r-- 1 root root 6 11月 15 23:54 1.txt drwxr-xr-x 8 root root 163 11月 15 23:54 .git -rw-r--r-- 1 root root 6142 11月 15 23:54 README.md 3##进入game目录上传游戏代码 [root@200-gitlab game]# rz -E rz waiting to receive. [root@200-gitlab game]# ll -h 总用量 7.6M -rw-r--r-- 1 root root 6 11月 15 23:54 1.txt -rw-r--r-- 1 root root 6.0K 11月 15 23:54 README.md -rw-r--r-- 1 root root 7.6M 11月 14 00:10 xbw.zip 4##解压代码 [root@200-gitlab game]# unzip xbw.zip 5##将代码提交到本地仓库 [root@200-gitlab game]# git add . [root@200-gitlab game]# git commit -m "game_v1" 6##将代码提交到远程仓库gitlab的game仓库中,因为我们是直接克隆的gitlab的仓库下载到本地的,所以目录中已经默认有了远程仓库game的项目地址 ##查看远程仓库 [root@200-gitlab game]# git remote -v origin git@10.0.0.200:ops/game.git (fetch) origin git@10.0.0.200:ops/game.git (push) ##将本地的master推送到运城仓库origin [root@200-gitlab game]# git push -u origin master error: src refspec master does not match any. error: 无法推送一些引用到 'git@10.0.0.200:ops/game.git' ##错误原因是本地没有master分支 ##查看本地所有分支 [root@200-gitlab game]# git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/main ##查看远程分支 [root@200-gitlab game]# git ls-remote --heads origin 6775582d2d9e6f4712050a33ac4b8014c178b0b5 refs/heads/main ##将本地的main推送到运城仓库origin [root@200-gitlab game]# git push -u origin main Counting objects: 100, done. Delta compression using up to 4 threads. Compressing objects: 100% (97/97), done. Writing objects: 100% (99/99), 14.87 MiB | 10.85 MiB/s, done. Total 99 (delta 3), reused 0 (delta 0) To git@10.0.0.200:ops/game.git 6775582..27538f0 main -> main 分支 main 设置为跟踪来自 origin 的远程分支 main。 [root@200-gitlab game]#
查看game项目,代码上传成功
1##配置一个已存在的本地仓库 [root@200-gitlab ~]# mkdir git_data [root@200-gitlab ~]# cd git_data/ 2##初始化仓库 [root@200-gitlab git_data]# git init 初始化空的 Git 版本库于 /root/git_data/.git/ 3##上传已存在的代码 [root@200-gitlab git_data]# touch 1.txt [root@200-gitlab git_data]# git add . [root@200-gitlab git_data]# git commit -m "test_v1" [master(根提交) 12a3723] test_v1 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 1.txt 4##设置gitlab的game仓库为本地的远程仓库 [root@200-gitlab git_data]# git remote add origin git@10.0.0.200:ops/game.git [root@200-gitlab git_data]# ll -a 总用量 4 drwxr-xr-x 3 root root 31 11月 16 00:13 . dr-xr-x---. 10 root root 4096 11月 16 00:13 .. -rw-r--r-- 1 root root 0 11月 16 00:13 1.txt drwxr-xr-x 8 root root 166 11月 16 00:16 .git 5##将本地的仓库的内容提交到远程仓库master分支 [root@200-gitlab git_data]# git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 196 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: To create a merge request for master, visit: remote: http://10.0.0.200/ops/game/-/merge_requests/new?merge_request%5Bsource_branch%5D=master remote: To git@10.0.0.200:ops/game.git * [new branch] master -> master 分支 master 设置为跟踪来自 origin 的远程分支 master。 [root@200-gitlab git_data]# 6##查看master分支
1.用管理员登录gitlab创建

创建后编辑用户,并输入密码
2.然后回到组页面,邀请test1用户进入ops组

3.完成后使用test1登录gitlab(第一次登录需要修改用户密码)
4.test1的服务器端生成秘钥对
[root@202-nexus ~]# ssh-keygen
5.将生成的公钥复制到test1账户下的ssh-key页面中
6.克隆代码到202 test1
[root@202-nexus ~]# git clone git@10.0.0.200:ops/game.git 正克隆到 'game'... remote: Enumerating objects: 109, done. remote: Counting objects: 100% (109/109), done. remote: Compressing objects: 100% (103/103), done. remote: Total 109 (delta 4), reused 0 (delta 0), pack-reused 0 接收对象中: 100% (109/109), 14.87 MiB | 11.18 MiB/s, done. 处理 delta 中: 100% (4/4), done.
7.修改并上传代码到远程仓库
1##修改文件内容 [root@202-nexus game]# cat 1.txt testr [root@202-nexus game]# vim 1.txt [root@202-nexus game]# cat 1.txt 修改后test1 2##提交修改到暂存区 [root@202-nexus game]# git commit -m "test1_v1" # 位于分支 main # 尚未暂存以备提交的变更: # (使用 "git add <file>..." 更新要提交的内容) # (使用 "git checkout -- <file>..." 丢弃工作区的改动) # # 修改: 1.txt # 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") [root@202-nexus game]# git commit -am "test1_v1" [main e1a2880] test1_v1 1 file changed, 1 insertion(+), 1 deletion(-) 3##推送本地修改到main分支 [root@202-nexus game]# git push -u origin main Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To git@10.0.0.200:ops/game.git 27538f0..e1a2880 main -> main 分支 main 设置为跟踪来自 origin 的远程分支 main。 [root@202-nexus game]#
页面上查看提交是否成功
这里不做描述
在web服务器上安装nginx,把代码克隆部署,这里不多做描述。
[root@204-web ~]# cd /home/deploy/nginx/html/ [root@204-web html]# git clone git@10.0.0.200:ops/game.git
部署成功后截图:

| 备注 | 修改日期 | 修改人 |
| 修改标题 | 2025-11-23 00:25:56[当前版本] | 文艺范儿 |
| 格式调整 | 2025-11-17 22:20:35 | 文艺范儿 |
| 格式调整 | 2025-11-17 22:18:41 | 文艺范儿 |
| 创建版本 | 2025-11-17 22:09:28 | 文艺范儿 |
| 附件类型 |
|
|
|
|
||