CI/CD-分布式版本控制系统-Git使用的场景

1.本地已经写好代码,需要用git去管理

先有文件,再去这个代码目录下git init

[root@localhost git-learn]# mkdir git_test 
[root@localhost git-learn]# cd git_test/
[root@localhost git_test]# echo  "hello git"  >> hello.sh
[root@localhost git_test]# ls
hello.sh
[root@localhost git_test]# git status
fatal: Not a git repository (or any of the parent directories): .git
[root@localhost git_test]# git init  .
初始化空的 Git 版本库于 /root/git-learn/git_test/.git/
[root@localhost git_test]# ls -a
.  ..  .git  hello.sh
[root@localhost git_test]# cd .git/
[root@localhost .git]# ls
branches  config  description  HEAD  hooks  info  objects  refs
[root@localhost .git]# cd ..
[root@localhost git_test]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       hello.sh
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost git_test]# git add hello.sh 
[root@localhost git_test]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#       新文件:    hello.sh
#
[root@localhost git_test]# git commit -m  "v1版本 第一次提交"
[master(根提交) 039901c] v1版本 第一次提交
 1 file changed, 1 insertion(+)
 create mode 100644 hello.sh
 
[root@localhost git_test]# git log
commit 039901c5013945969a0c819e6abf4e8fe5afa68e
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:10:24 2024 -0400

    v1版本 第一次提交
[root@localhost git_test]# echo  "33333333333333"   >> v1.sh   #修改v1.sh文件
[root@localhost git_test]# cat v1.sh 
22222222
33333333333333
[root@localhost git_test]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#       修改:      v1.sh
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")


[root@localhost git_test]# git add v1.sh 
[root@localhost git_test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       修改:      v1.sh
#

[root@localhost git_test]# git commit -m "v3 第三次提交"
[master 19b88a3] v3 第三次提交
 1 file changed, 1 insertion(+)
[root@localhost git_test]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost git_test]# git log
commit 19b88a3e7514fb1a553700a6a41f0148fa016f82
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:52:48 2024 -0400

    v3 第三次提交

commit 163853800ccd9dd170a2bc068eede9668ac287c3
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:45:38 2024 -0400

    v2  第二次提交

commit 039901c5013945969a0c819e6abf4e8fe5afa68e
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:10:24 2024 -0400

    v1版本 第一次提交

[root@localhost git_test]# git reset 163853800ccd9dd170a2bc068eede9668ac287c3 v1.sh
重置后撤出暂存区的变更:
M       v1.sh
[root@localhost git_test]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       修改:      v1.sh
#
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#       修改:      v1.sh
#
[root@localhost git_test]# git log
commit 19b88a3e7514fb1a553700a6a41f0148fa016f82
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:52:48 2024 -0400

    v3 第三次提交

commit 163853800ccd9dd170a2bc068eede9668ac287c3
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:45:38 2024 -0400

    v2  第二次提交

commit 039901c5013945969a0c819e6abf4e8fe5afa68e
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 09:10:24 2024 -0400

    v1版本 第一次提交

2.从零新建git本地仓库,编写代码

先 git init local_ registry_name ,再写代码

  • 初始化一个本地仓库
[root@localhost git-learn]# git init my_wensite   ##初始化一个本地仓库
初始化空的 Git 版本库于 /root/git-learn/my_wensite/.git/
[root@localhost git-learn]# cd my_wensite/
[root@localhost my_wensite]# ls -a
.  ..  .git

  • 向工作区添加代码,并提交v1版本
[root@localhost my_wensite]# echo    "hello git"  >> v1.txt
[root@localhost my_wensite]# git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       v1.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost my_wensite]# git add v1.txt 
[root@localhost my_wensite]# git commit -m  "v1 一次提交" 
[master(根提交) 12fc6a1] v1 一次提交
 1 file changed, 1 insertion(+)
 create mode 100644 v1.txt
[root@localhost my_wensite]# git status 
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost my_wensite]# git log
commit 12fc6a12cda612cc1380e2966a8e0fa71fd79859
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 10:07:32 2024 -0400

    v1 一次提交

  • 向工作区添加代码,并提交v2版本
[root@localhost my_wensite]# touch linux024{a..z}.log 
[root@localhost my_wensite]# ls
linux024a.log  linux024e.log  linux024i.log  linux024m.log  linux024q.log  linux024u.log  linux024y.log
....
[root@localhost my_wensite]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#       linux024a.log
#       linux024b.log
#       linux024c.log
#       linux024d.log
#       linux024e.log
.....
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost my_wensite]# git add .
[root@localhost my_wensite]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#       新文件:    linux024a.log
#       新文件:    linux024b.log
#       新文件:    linux024c.log
#       新文件:    linux024d.log
#       新文件:    linux024e.log
...
[root@localhost my_wensite]# git commit -m "v2 第二次提交包含*.log文件"
[master 0030265] v2 第二次提交包含*.log文件
 26 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 linux024a.log
 create mode 100644 linux024b.log
 create mode 100644 linux024c.log
 create mode 100644 linux024d.log
 create mode 100644 linux024e.log
...
[root@localhost my_wensite]# git status 
# 位于分支 master
无文件要提交,干净的工作区
[root@localhost my_wensite]# git log
commit 00302653fd874d66695102512bfa30d427ee1bf3
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 10:14:09 2024 -0400

    v2 第二次提交包含*.log文件

commit 12fc6a12cda612cc1380e2966a8e0fa71fd79859
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 10:07:32 2024 -0400

    v1 一次提交

  • 如何从v2版本回退到v1版本
[root@localhost my_wensite]# git reset --hard HEAD^  #回退到v1版本
HEAD 现在位于 12fc6a1 v1 一次提交
[root@localhost my_wensite]# ls  #以*.log结尾的文件不存在了
v1.txt

如何从v1版本撤回reset到v2版本的操作

操作: 只需要修改git的文件指针(HEAD),指向另一个commit_id版本记录即可

[root@localhost my_wensite]# #如下命令,可以看见你所有的git log操作, 也就是可以看得到所有的commit_id
[root@localhost my_wensite]# git reflog
12fc6a1 HEAD@{0}: reset: moving to HEAD^    #v2版本回退到v1版本操作
0030265 HEAD@{1}: commit: v2 第二次提交包含*.log文件   #提交v2版本操作  0030265第二次提交记录id
12fc6a1 HEAD@{2}: commit (initial): v1 一次提交    # 提交v1版本操作  12fc6a1第一次提交的记录id

[root@localhost my_wensite]# git reset --hard 0030265  #回到指定的提交id记录中,也就是v2版本
HEAD 现在位于 0030265 v2 第二次提交包含*.log文件
[root@localhost my_wensite]# ls
linux024a.log  linux024e.log  linux024i.log  linux024m.log  linux024q.log  linux024u.log  linux024y.log
...
[root@localhost my_wensite]# git log
commit 00302653fd874d66695102512bfa30d427ee1bf3
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 10:14:09 2024 -0400

    v2 第二次提交包含*.log文件

commit 12fc6a12cda612cc1380e2966a8e0fa71fd79859
Author: wenjie <wenjie01@163.com>
Date:   Thu Aug 1 10:07:32 2024 -0400

    v1 一次提交
[root@localhost my_wensite]# git reset --hard HEAD^  # 回到上次提交的版本id中,也就是v1
HEAD 现在位于 12fc6a1 v1 一次提交
[root@localhost my_wensite]# ls
v1.txt


[root@localhost my_wensite]# git reflog
12fc6a1 HEAD@{0}: reset: moving to HEAD^
0030265 HEAD@{1}: reset: moving to 0030265
12fc6a1 HEAD@{2}: reset: moving to HEAD^
0030265 HEAD@{3}: commit: v2 第二次提交包含*.log文件
12fc6a1 HEAD@{4}: commit (initial): v1 一次提交
[root@localhost my_wensite]# git reset --hard 0030265
HEAD 现在位于 0030265 v2 第二次提交包含*.log文件

3.克隆远程仓库中的代码

  • 克隆jumpserver源代码到本地
[root@localhost tmp]# git clone  https://github.com/jumpserver/jumpserver.git
正克隆到 'jumpserver'...
fatal: unable to access 'https://github.com/jumpserver/jumpserver.git/': Encountered end of file
[root@localhost tmp]# git clone  https://gitee.com/jumpserver/jumpserver.git
正克隆到 'jumpserver'...
remote: Enumerating objects: 105019, done.
remote: Total 105019 (delta 0), reused 0 (delta 0), pack-reused 105019
接收对象中: 100% (105019/105019), 67.91 MiB | 494.00 KiB/s, done.
处理 delta 中: 100% (75229/75229), done.

[root@localhost jumpserver]# git log --oneline  #一行显示commit id和注释
92b6286 fix: ldap更换OU后无法登录 (#13172)
bce776b fix: 修复 v2 升级到 v3 授权的手动登录系统用户显示空字符串的问题
dc39cbf fix: ldap定时任务未执行
38175d6 fix: Fixed csv file export for `0` chars is not appear
7408ed0 perf: add XPACKModelFieldsMixin
5135186 perf: 社区版去掉一些东西


[root@localhost jumpserver]# git log --oneline -4   # 查看最新的几条数据
92b6286 fix: ldap更换OU后无法登录 (#13172)
bce776b fix: 修复 v2 升级到 v3 授权的手动登录系统用户显示空字符串的问题
dc39cbf fix: ldap定时任务未执行
38175d6 fix: Fixed csv file export for `0` chars is not appear

posted @   钱超多  阅读(5)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示