Git版本管理系统初体验
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
目录
一.搭建Golang开发环境
1.下载golang的SDK
[root@prometheus-server31 ~]# wget https://studygolang.com/dl/golang/go1.23.3.linux-amd64.tar.gz
2.解压软件版
[root@prometheus-server31 ~]# tar xf go1.23.3.linux-amd64.tar.gz -C /yinzhengjie/
3.配置环境变量
[root@prometheus-server31 ~]# cat /etc/profile.d/go.env
#!/bin/bash
export GOROOT=/yinzhengjie/go
export PATH=$GOROOT/bin
[root@prometheus-server31 ~]#
[root@prometheus-server31 ~]# source /etc/profile.d/go.env
[root@prometheus-server31 ~]#
4.查看golang的环境变量
[root@prometheus-server31 ~]# go env -w GOPROXY=https://goproxy.cn,direct
[root@prometheus-server31 ~]#
[root@prometheus-server31 ~]# go version
go version go1.23.3 linux/amd64
[root@prometheus-server31 ~]#
温馨提示:
想要查看Golang环境搭建的可以参考我之前录制的视频,本章节仅简单介绍下Go环境搭建。
https://www.bilibili.com/video/BV1bwhve7EPJ
二.git init项目初始化
1.创建项目目录
[root@prometheus-server31 ~]# mkdir -pv /yinzhengjie/devops
mkdir: created directory '/yinzhengjie/devops'
[root@prometheus-server31 ~]#
2.初始化项目
[root@prometheus-server31 ~]# cd /yinzhengjie/devops/
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 8
drwxr-xr-x 2 root root 4096 Nov 17 19:32 ./
drwxr-xr-x 6 root root 4096 Nov 17 19:32 ../
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /yinzhengjie/devops/.git/
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 12
drwxr-xr-x 3 root root 4096 Nov 17 19:33 ./
drwxr-xr-x 6 root root 4096 Nov 17 19:32 ../
drwxr-xr-x 7 root root 4096 Nov 17 19:33 .git/
[root@prometheus-server31 devops]#
3.查看".git"目录结构
[root@prometheus-server31 devops]# tree .git
.git
├── branches # 分支
├── config # git config命令设置的配置信息
├── description # 被git web(Github的原型)用来显式对仓库的描述信息
├── HEAD # 当前分支的标识符号,一般指向refs/heads目录下的分支条件
├── hooks # 触发器规则文件,俗称"钩子文件"
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── fsmonitor-watchman.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-merge-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── pre-receive.sample
│ ├── push-to-checkout.sample
│ └── update.sample
├── info # 一些特殊文件设置目录,比如忽略文件设置,".gitignore是互补的".
│ └── exclude
├── objects # 存放git对象,git核心目录。
│ ├── info
│ └── pack
└── refs # 各个提交对象的标识(SHA-1)文件
├── heads # 分支相关信息的目录
└── tags # 标签相关信息的目录
9 directories, 17 files
[root@prometheus-server31 devops]#
三.git config配置git个人信息
1.查看默认的配置信息
[root@prometheus-server31 devops]# git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[root@prometheus-server31 devops]#
2.配置用户名,邮箱,语法高亮等,只需配置一次即可
[root@prometheus-server31 devops]# git config --global user.name 'jasonyin2020'
[root@prometheus-server31 devops]# git config --global user.email 'y1053419035@qq.com'
[root@prometheus-server31 devops]# git config --global color.ui true
[root@prometheus-server31 devops]#
3.再次查看配置信息列表
[root@prometheus-server31 devops]# git config -l
user.name=jasonyin2020
user.email=y1053419035@qq.com
color.ui=true
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
[root@prometheus-server31 devops]#
四.git status查看工作目录状态
1.查看当前目录的状态
[root@prometheus-server31 devops]# git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
[root@prometheus-server31 devops]#
2.创建go.mod包管理文件
[root@prometheus-server31 devops]# go mod init cmdb
go: creating new go.mod: module cmdb
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 19:59 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 7 root root 4096 Nov 17 19:39 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]#
3.再次查看当前目录状态
[root@prometheus-server31 devops]# git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
go.mod
nothing added to commit but untracked files present (use "git add" to track)
[root@prometheus-server31 devops]#
五.git add将文件从工作区提交到暂存区
1.将未追踪的文件提交到暂存区
[root@prometheus-server31 devops]# git add go.mod
[root@prometheus-server31 devops]#
2.再次查看当前目录状态
[root@prometheus-server31 devops]# git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: go.mod
[root@prometheus-server31 devops]#
六.git commit提交代码到本地仓库
1.查看暂存区文件状态
[root@prometheus-server31 devops]# git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: go.mod
[root@prometheus-server31 devops]#
2.将暂存区的文件提交到本地仓库
[root@prometheus-server31 devops]# git commit -m 'add go.mod'
[master (root-commit) 005b95d] add go.mod
1 file changed, 3 insertions(+)
create mode 100644 go.mod
[root@prometheus-server31 devops]#
3.再次查看暂存区文件状态
[root@prometheus-server31 devops]# git status
On branch master
nothing to commit, working tree clean
[root@prometheus-server31 devops]#
七.git log查看版本记录
1.查看历史提交的版本记录
[root@prometheus-server31 devops]# git log
commit 005b95d954d811ae67a219e8cb377766f44fe968 (HEAD -> master)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:06:50 2024 +0800
add go.mod
[root@prometheus-server31 devops]#
2.查看格式化输出的历史版本记录
[root@prometheus-server31 devops]# git log --graph --all --pretty=format:"%h %s"
* 005b95d add go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git log --graph --all --pretty=format:"%h - %d %s (%an, %cd)"
* 005b95d - (HEAD -> master) add go.mod (jasonyin2020, Sun Nov 17 20:06:50 2024 +0800)
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
八.git reset版本回退
1.再次提交一个新的文件
[root@prometheus-server31 devops]# cat main.go
package main
import (
"fmt"
)
func main(){
fmt.Println("尹正杰博客地址: https://www.cnblogs.com/yinzhengjie")
}
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# go run main.go
尹正杰博客地址: https://www.cnblogs.com/yinzhengjie
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add . ; git commit -m 'golang demo'
[master 6eacc9c] golang demo
1 file changed, 9 insertions(+)
create mode 100644 main.go
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git log # 查看当前版本记录
commit 6eacc9c4e90830e92fc9eadb7e4a6d5b41498556 (HEAD -> master)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:12:31 2024 +0800
golang demo
commit 005b95d954d811ae67a219e8cb377766f44fe968
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:06:50 2024 +0800
add go.mod
[root@prometheus-server31 devops]#
2.回滚到指定版本
[root@prometheus-server31 devops]# git reflog # 查看历史版本提交记录,注意要和"git log"进行区分。
6eacc9c (HEAD -> master) HEAD@{0}: commit: golang demo
005b95d HEAD@{1}: commit (initial): add go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git reset --hard 005b95d # 回滚到指定的提交ID
HEAD is now at 005b95d add go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git reflog # 回滚后,再次查看提交的记录信息
005b95d (HEAD -> master) HEAD@{0}: reset: moving to 005b95d
6eacc9c HEAD@{1}: commit: golang demo
005b95d (HEAD -> master) HEAD@{2}: commit (initial): add go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git log # 很明显,已经回滚成功啦
commit 005b95d954d811ae67a219e8cb377766f44fe968 (HEAD -> master)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:06:50 2024 +0800
add go.mod
[root@prometheus-server31 devops]#
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/12241860.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。