作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
目录
- 一.git命令初体验
- 1.搭建Golang开发环境
- 2.git init项目初始化
- 3.git status查看工作目录状态
- 4.git add将文件从工作区提交到暂存区
- 5.git config配置git个人信息
- 6.git commit提交代码到本地仓库
- 7.git log查看版本记录
- 8.git reset版本回退
- 二.git branch分支管理
- 1.查看分支和当前所在分支
- 2.创建分支
- 3.切换分支
- 4.主分支合并其他分支
- 5.删除分支
- 三.推送代码到远程仓库gitee
- 1.注册GitHub或者gitee等仓库账号
- 2.将本地仓库同步至远程仓库
- 3.将多个分支推送到远程仓库
- 4.配置本地的ssh公钥到gitee服务器
- 5.使用ssh协议拉取代码
- 6.拉取远程仓库特定的分支
- 四.tag标签管理
- 1.标签管理概述
- 2.为已经提交的版本打tag
- 3.查看现有的tag
- 4.同步所有的标签到远程仓库
- 5.其他节点从远程仓库拉取最新的标签
- 6.删除标签
- 五.git设置忽略文件
- 1.".gitignore"文件说明
- 2.配置忽略指定文件案例
- 六.git revert实现远程仓库回滚
- 1.git revert概述
- 2.git revert用法
- 3.git reset和git revert区别
- 七.可能会遇到的问题
- 1.Already up to date.
- 2.Please make sure you have the correct access rights
一.git命令初体验
1.搭建Golang开发环境
1.下载golang的SDK
[root@prometheus-server31 ~]# wget https://studygolang.com/dl/golang/go1.23.3.linux-amd64.tar.gz2.解压软件版
[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/bashexport 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
2.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]#
3.git status查看工作目录状态
1.查看当前目录的状态
[root@prometheus-server31 devops]# git status
On branch masterNo commits yetnothing 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 masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)go.modnothing added to commit but untracked files present (use "git add" to track)
[root@prometheus-server31 devops]#
4.git add将文件从工作区提交到暂存区
1.将未追踪的文件提交到暂存区
[root@prometheus-server31 devops]# git add go.mod
[root@prometheus-server31 devops]# 2.再次查看当前目录状态
[root@prometheus-server31 devops]# git status
On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: go.mod[root@prometheus-server31 devops]#
5.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]#
6.git commit提交代码到本地仓库
1.查看暂存区文件状态
[root@prometheus-server31 devops]# git status
On branch masterNo commits yetChanges 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.mod1 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]#
7.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 +0800add 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]#
8.git reset版本回退
1.再次提交一个新的文件
[root@prometheus-server31 devops]# cat main.go
package mainimport ("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 demo1 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 +0800golang democommit 005b95d954d811ae67a219e8cb377766f44fe968
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:06:50 2024 +0800add 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 +0800add go.mod
[root@prometheus-server31 devops]#
二.git branch分支管理
1.查看分支和当前所在分支
[root@prometheus-server31 devops]# git branch
* master
[root@prometheus-server31 devops]#
2.创建分支
[root@prometheus-server31 devops]# git branch k8s
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branchk8s
* master
[root@prometheus-server31 devops]#
3.切换分支
1.切换到已经存在的分支
[root@prometheus-server31 devops]# git branchk8s
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git checkout k8s
Switched to branch 'k8s'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch
* k8smaster
[root@prometheus-server31 devops]# 2.如果分支不存在,可以创建分支
[root@prometheus-server31 devops]# git checkout -b docker
Switched to a new branch 'docker'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch
* dockerk8smaster
[root@prometheus-server31 devops]#
4.主分支合并其他分支
1.docker分支创建文件
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 20:16 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:05 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch
* dockerk8smaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# echo docs.docker.com > office.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:07 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:05 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 16 Nov 17 22:07 office.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add office.txt ; git commit -m 'add docker info'
[docker 5b1e29f] add docker info1 file changed, 1 insertion(+)create mode 100644 office.txt
[root@prometheus-server31 devops]# 2.k8s分支创建文件
[root@prometheus-server31 devops]# git branch
* dockerk8smaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git checkout k8s
Switched to branch 'k8s'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branchdocker
* k8smaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 22:08 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:08 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# echo https://www.kubernetes.io > k8s.log
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add k8s.log ; git commit -m 'add k8s info'
[k8s 828e023] add k8s info1 file changed, 1 insertion(+)create mode 100644 k8s.log
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:09 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:09 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:09 k8s.log
[root@prometheus-server31 devops]# 3.合并分支
[root@prometheus-server31 devops]# git branchdocker
* k8smaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git checkout master # 先切换到master分支
Switched to branch 'master'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch dockerk8s
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 16
drwxr-xr-x 3 root root 4096 Nov 17 22:10 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:10 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git merge k8s # 先合并K8S分支
Updating 005b95d..828e023
Fast-forwardk8s.log | 1 +1 file changed, 1 insertion(+)create mode 100644 k8s.log
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 20
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:11 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:11 k8s.log # 不难发现,该分支的数据已经到主分支了。
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git merge docker # 接下来合并docker分支
Merge made by the 'ort' strategy.office.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 office.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:12 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root 16 Nov 17 22:11 office.txt # 不难发现,docker分支的内容也来到主分支啦~
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# cat k8s.log
https://www.kubernetes.io
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# cat office.txt
docs.docker.com
[root@prometheus-server31 devops]#
5.删除分支
[root@prometheus-server31 devops]# git branchdockerk8s
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch -d k8s # 删除k8s分支
Deleted branch k8s (was 828e023).
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branchdocker
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch -d docker # 删除docker分支
Deleted branch docker (was 5b1e29f).
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:17 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root 16 Nov 17 22:11 office.txt
[root@prometheus-server31 devops]#
三.推送代码到远程仓库gitee
1.注册GitHub或者gitee等仓库账号
首先,需要注册GitHub或者gitee等仓库账号,然后再执行如下命令,将代码上传到仓库。如上图所示,在新建仓库时,选择不初始化仓库。
2.将本地仓库同步至远程仓库
如上图所示,官方提供了推送到仓库的命令。如下图所示,成功将代码推送到gitee远程仓库。以下是我将本地devops目录的代码推送到远程gitee仓库详细步骤:
[root@prometheus-server31 devops]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:11 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:17 .git/
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root 16 Nov 17 22:11 office.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git remote -v
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git remote add origin https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git remote -v
origin https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git (fetch)
origin https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git (push)
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git push -u origin "master"
Username for 'https://gitee.com': jasonyin2020
Password for 'https://jasonyin2020@gitee.com':
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 2 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 939 bytes | 939.00 KiB/s, done.
Total 11 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 9141c7ae
To https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
[root@prometheus-server31 devops]#
3.将多个分支推送到远程仓库
1.新建flink分支
[root@prometheus-server31 devops]# git branch
* master
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git checkout -b flink
Switched to a new branch 'flink'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branch
* flinkmaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# echo https://flink.apache.org/ > flink.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add flink.txt ; git commit -m 'add flink'
[flink 9e74e39] add flink1 file changed, 1 insertion(+)create mode 100644 flink.txt
[root@prometheus-server31 devops]# 2.新建kafka分支
[root@prometheus-server31 devops]# git checkout -b kafka
Switched to a new branch 'kafka'
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branchflink
* kafkamaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# echo https://kafka.apache.org/ > kafka.log
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add kafka.log ; git commit -m 'add kafka'
[kafka 932ec59] add kafka1 file changed, 1 insertion(+)create mode 100644 kafka.log
[root@prometheus-server31 devops]# 3.推送所有分支到远程仓库
[root@prometheus-server31 devops]# git remote -v
origin https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git (fetch)
origin https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git (push)
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git push -u origin --all # 推送所有分支到远程仓库
Username for 'https://gitee.com': jasonyin2020
Password for 'https://jasonyin2020@gitee.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 598 bytes | 598.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 88410398
To https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git* [new branch] flink -> flink* [new branch] kafka -> kafka
Branch 'master' set up to track remote branch 'master' from 'origin'.
Branch 'flink' set up to track remote branch 'flink' from 'origin'.
Branch 'kafka' set up to track remote branch 'kafka' from 'origin'.
[root@prometheus-server31 devops]#
4.配置本地的ssh公钥到gitee服务器
1.新电脑生成秘钥
[root@prometheus-server31 ~]# ssh-keygen -f ~/.ssh/id_rsa -P '' -q2.查看公钥,将自己本地的公钥拷贝到服务器
[root@prometheus-server31 ~]# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDJRqH9BN9GEPbqvvoRYZqKPcU8V/muF0GrXQtUrh42NMHecu2sdO5Utp7SQnh4lA/+xX1wp7tm1A2v4jRHCFmHM26okw0O0aFW8BzjY+ZW2bTFuWE/zI1P0q1lxdT4gaCtbFohyjmJi26cqTSDHfeN5r6vatyiJCPsDOzMa/SPMeKf4C8uVoAR+FRCO3T4LAzoGqi5OabsrDguzgkQaqclH7AhHIpyRGwhCMJLtpkDHAxni7PRO6smIlc4Rwuelr8chQRqalS6I8pxQ+82XmCEtE7KV4g2wcRIed2EuZQmBbxZZMSnGZdYRZ+DT5pu3CXUeJ8DDx/8K/9bT05+opZ5/cOtD2Yr5rimRjWIGQgZh7MNsYZpqcxecXSMXLFRsMiGyyuzAavT2GcStknz5E30vE5cgw4gn5/VT49y9/UAFHd8Uw85yD7RSSAw3GGO31H67wzTQbt6YY6XmAr6aSD9rotvS2Ml5KMGx2GU/X3Jtip6rGV9fqiG+d1BEBOrMO0= root@prometheus-server31
[root@prometheus-server31 ~]# 3.添加公钥
按照上图所示的步骤操作,操作成功后,会得到如下图所示的提示哟。温馨提示:上面的方式演示了基于ssh的免密登录,其实对于https也是支持免密登录的哟,只不过不建议使用。- 原来的地址:https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git- 修改的地址:https://用户名:密码@gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git对于ssh免密登录使用的是前缀协议是git,对于https免密登录使用的前缀协议是https。
5.使用ssh协议拉取代码
如上图所示,我们可以按照WebUI的方式来克隆代码到本地。如下图所示,将代码成功克隆到本地后,我们可以切换分支。具体操作如下:
[root@prometheus-server31 ~]# git clone git@gitee.com:jasonyin2020/yinzhengjie-devops-k8s.git
Cloning into 'yinzhengjie-devops-k8s'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 17 (delta 4), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (4/4), done.
[root@prometheus-server31 ~]#
[root@prometheus-server31 ~]# cd yinzhengjie-devops-k8s/
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# ll
total 24
drwxr-xr-x 3 root root 4096 Nov 17 22:51 ./
drwx------ 12 root root 4096 Nov 17 22:51 ../
drwxr-xr-x 8 root root 4096 Nov 17 22:51 .git/
-rw-r--r-- 1 root root 23 Nov 17 22:51 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:51 k8s.log
-rw-r--r-- 1 root root 16 Nov 17 22:51 office.txt
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git branch
* master
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git checkout kafka
Branch 'kafka' set up to track remote branch 'kafka' from 'origin'.
Switched to a new branch 'kafka'
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git branch
* kafkamaster
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# ll
total 32
drwxr-xr-x 3 root root 4096 Nov 17 22:51 ./
drwx------ 12 root root 4096 Nov 17 22:52 ../
-rw-r--r-- 1 root root 26 Nov 17 22:51 flink.txt
drwxr-xr-x 8 root root 4096 Nov 17 22:51 .git/
-rw-r--r-- 1 root root 23 Nov 17 22:51 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:51 k8s.log
-rw-r--r-- 1 root root 26 Nov 17 22:51 kafka.log
-rw-r--r-- 1 root root 16 Nov 17 22:51 office.txt
[root@prometheus-server31 yinzhengjie-devops-k8s]#
6.拉取远程仓库特定的分支
如上图所示,我们可以在拉取代码的时候选择一个特定的分支来拉取代码。具体操作如下所示:
[root@prometheus-server31 tmp]# git clone git@gitee.com:jasonyin2020/yinzhengjie-devops-k8s.git -b flink
Cloning into 'yinzhengjie-devops-k8s'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 17 (delta 4), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (4/4), done.
[root@prometheus-server31 tmp]#
[root@prometheus-server31 tmp]# cd yinzhengjie-devops-k8s/
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git branch
* flink
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# ll
total 28
drwxr-xr-x 3 root root 4096 Nov 17 22:55 ./
drwxrwxrwt 14 root root 4096 Nov 17 22:55 ../
-rw-r--r-- 1 root root 26 Nov 17 22:55 flink.txt
drwxr-xr-x 8 root root 4096 Nov 17 22:55 .git/
-rw-r--r-- 1 root root 23 Nov 17 22:55 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:55 k8s.log
-rw-r--r-- 1 root root 16 Nov 17 22:55 office.txt
[root@prometheus-server31 yinzhengjie-devops-k8s]#
四.tag标签管理
1.标签管理概述
虽然每次commit都会生成一个commit id,但此方式无疑是不方便标记和记忆的。git标签就是对指定的commit的一次快照,相当于commit ID起了一个容易记忆的名称,可以作为进度的里程碑标记。git tag便于后续将特定时期的代码快速取出。在代码发布时可以使用标签发布。
2.为已经提交的版本打tag
1.查看现有已经提交的commit ID
[root@prometheus-server31 devops]# git reflog
932ec59 (HEAD -> kafka, origin/kafka) HEAD@{0}: commit: add kafka
9e74e39 (origin/flink, flink) HEAD@{1}: checkout: moving from flink to kafka
9e74e39 (origin/flink, flink) HEAD@{2}: commit: add flink
b69638d (origin/master, master) HEAD@{3}: checkout: moving from master to flink
b69638d (origin/master, master) HEAD@{4}: merge docker: Merge made by the 'ort' strategy.
828e023 HEAD@{5}: merge k8s: Fast-forward
005b95d HEAD@{6}: checkout: moving from k8s to master
828e023 HEAD@{7}: commit: add k8s info
005b95d HEAD@{8}: checkout: moving from docker to k8s
5b1e29f HEAD@{9}: commit: add docker info
005b95d HEAD@{10}: checkout: moving from k8s to docker
005b95d HEAD@{11}: checkout: moving from master to k8s
005b95d HEAD@{12}: reset: moving to 005b95d
6eacc9c HEAD@{13}: commit: golang demo
005b95d HEAD@{14}: commit (initial): add go.mod
[root@prometheus-server31 devops]# 2.为已经提交的commit ID打标签
[root@prometheus-server31 devops]# git tag -a 'Kafka-v3.8.0' -m 'Kafka v3.8.0版本发布' # 若不指定commit ID则默认为最新的commit ID打标签。
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git tag -a 'Flink-v2.0' -m 'Flink v2.0版本发布' 9e74e39 # 为指定的commit ID打标签。
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git tag
Flink-v2.0
Kafka-v3.8.0
[root@prometheus-server31 devops]#
3.查看现有的tag
1.查看现有的tag列表信息
[root@prometheus-server31 devops]# git tag
Flink-v2.0
Kafka-v3.8.0
[root@prometheus-server31 devops]# 2.查看某个tag的详细信息
[root@prometheus-server31 devops]# git show Flink-v2.0
tag Flink-v2.0
Tagger: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 23:04:12 2024 +0800Flink v2.0版本发布commit 9e74e39327f26b9d3d8dc241eb85b26413e37ed1 (tag: Flink-v2.0, origin/flink, flink)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:32:15 2024 +0800add flinkdiff --git a/flink.txt b/flink.txt
new file mode 100644
index 0000000..fa29c21
--- /dev/null
+++ b/flink.txt
@@ -0,0 +1 @@
+https://flink.apache.org/
[root@prometheus-server31 devops]# 3.查看commit ID并显式tag相关信息
[root@prometheus-server31 devops]# git log
commit 932ec59bbff547666f92aff82f691ac030d2fd86 (HEAD -> kafka, tag: Kafka-v3.8.0, origin/kafka)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:37:29 2024 +0800add kafkacommit 9e74e39327f26b9d3d8dc241eb85b26413e37ed1 (tag: Flink-v2.0, origin/flink, flink)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:32:15 2024 +0800add flinkcommit b69638dee63a0fd9e631014c284aff5749d90960 (origin/master, master)
Merge: 828e023 5b1e29f
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:11:58 2024 +0800Merge branch 'docker'commit 828e02325fb9d4a3b162c6743cc21507af290eae
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:09:32 2024 +0800add k8s infocommit 5b1e29faee912c5578040abb914a3e50e3749249
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:07:42 2024 +0800add docker infocommit 005b95d954d811ae67a219e8cb377766f44fe968
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 20:06:50 2024 +0800add go.mod
[root@prometheus-server31 devops]#
4.同步所有的标签到远程仓库
1.将本地仓库的tag信息推送到远程仓库
[root@prometheus-server31 devops]# git push origin --tags
Username for 'https://gitee.com': jasonyin2020
Password for 'https://jasonyin2020@gitee.com':
Enumerating objects: 2, done.
Counting objects: 100% (2/2), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 346 bytes | 346.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 65453081
To https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git* [new tag] Flink-v2.0 -> Flink-v2.0* [new tag] Kafka-v3.8.0 -> Kafka-v3.8.0
[root@prometheus-server31 devops]# 2.在Gitee上查看某个标签的数据
如上图所示,我们把标签推送成功了,当然我们可以下载对应标签的程序包哟。
5.其他节点从远程仓库拉取最新的标签
[root@prometheus-server31 yinzhengjie-devops-k8s]# git branch
* flink
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git tag
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git pull origin --tags # 这里会拉取所有的标签哟
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (2/2), 326 bytes | 326.00 KiB/s, done.
From gitee.com:jasonyin2020/yinzhengjie-devops-k8s* [new tag] Flink-v2.0 -> Flink-v2.0* [new tag] Kafka-v3.8.0 -> Kafka-v3.8.0
Already up to date.
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git tag
Flink-v2.0
Kafka-v3.8.0
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git show Kafka-v3.8.0
tag Kafka-v3.8.0
Tagger: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 23:02:57 2024 +0800Kafka v3.8.0版本发布commit 932ec59bbff547666f92aff82f691ac030d2fd86 (tag: Kafka-v3.8.0, origin/kafka)
Author: jasonyin2020 <y1053419035@qq.com>
Date: Sun Nov 17 22:37:29 2024 +0800add kafkadiff --git a/kafka.log b/kafka.log
new file mode 100644
index 0000000..3ae0ead
--- /dev/null
+++ b/kafka.log
@@ -0,0 +1 @@
+https://kafka.apache.org/
[root@prometheus-server31 yinzhengjie-devops-k8s]#
6.删除标签
如上图所示,如果想要删除远程仓库的标签,则需要在WebUI操作即可,本地的标签基于命令行删除即可。本地删除标签的具体操作如下所示:
[root@prometheus-server31 yinzhengjie-devops-k8s]# git tag
Flink-v2.0
Kafka-v3.8.0
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git tag -d Kafka-v3.8.0
Deleted tag 'Kafka-v3.8.0' (was bd02cc1)
[root@prometheus-server31 yinzhengjie-devops-k8s]#
[root@prometheus-server31 yinzhengjie-devops-k8s]# git tag
Flink-v2.0
[root@prometheus-server31 yinzhengjie-devops-k8s]#
五.git设置忽略文件
1.".gitignore"文件说明
配置".gitignore"文件让Git不在管理当前目录下的某些文件。生产中有如下几类文件可能需要忽略:- 程序运行时产生的临时的文件;- 程序连接数据库这一类的配置文件;- 程序本地开发使用的图片文字;- 其他不想被共享的文件;
2.配置忽略指定文件案例
1.创建忽略文件
[root@prometheus-server31 devops]# cat .gitignore
/bin
/target
.classpath
.project
.settings
*.h
!test.h
*.py[c|x]
*.swp
*.exe
[root@prometheus-server31 devops]# 2.将忽略文件提交打本地仓库
[root@prometheus-server31 devops]# git add .gitignore; git commit -m '配置忽略文件'
[kafka c5002de] 配置忽略文件1 file changed, 10 insertions(+)create mode 100644 .gitignore
[root@prometheus-server31 devops]# 3.将提交的本地仓库推到远程仓库
[root@prometheus-server31 devops]# touch {1..5}.exe
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# ll
total 36
drwxr-xr-x 3 root root 4096 Nov 17 23:24 ./
drwxr-xr-x 7 root root 4096 Nov 17 19:43 ../
-rw-r--r-- 1 root root 0 Nov 17 23:24 1.exe
-rw-r--r-- 1 root root 0 Nov 17 23:24 2.exe
-rw-r--r-- 1 root root 0 Nov 17 23:24 3.exe
-rw-r--r-- 1 root root 0 Nov 17 23:24 4.exe
-rw-r--r-- 1 root root 0 Nov 17 23:24 5.exe
-rw-r--r-- 1 root root 26 Nov 17 22:31 flink.txt
drwxr-xr-x 8 root root 4096 Nov 17 23:23 .git/
-rw-r--r-- 1 root root 77 Nov 17 23:23 .gitignore
-rw-r--r-- 1 root root 23 Nov 17 19:59 go.mod
-rw-r--r-- 1 root root 26 Nov 17 22:11 k8s.log
-rw-r--r-- 1 root root 26 Nov 17 22:34 kafka.log
-rw-r--r-- 1 root root 16 Nov 17 22:11 office.txt
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git add . ;git commit -m 'add *.exe'
On branch kafka
Your branch is ahead of 'origin/kafka' by 1 commit.(use "git push" to publish your local commits)nothing to commit, working tree clean
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git branchflink
* kafkamaster
[root@prometheus-server31 devops]#
[root@prometheus-server31 devops]# git push origin kafka
Username for 'https://gitee.com': jasonyin2020
Password for 'https://jasonyin2020@gitee.com':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 371 bytes | 371.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [1.1.5]
remote: Set trace flag 5146499e
To https://gitee.com/jasonyin2020/yinzhengjie-devops-k8s.git932ec59..c5002de kafka -> kafka
[root@prometheus-server31 devops]#
六.git revert实现远程仓库回滚
1.git revert概述
如果我们修改了某些内容已经提交到本地仓库,并且push到远程仓库了,这种情况下想把本地和远程仓库都回滚到某个版本,该怎么做呢?前面用git reset只是在本地仓库中回退版本,而远程仓库的版本不会变化,这样,即使本地reset了,但如果再次git pull,那么,远程仓库的内容又会和本地之前版本的内容进行merge,造成回退失败。对于已经把代码push到远程仓库,你退回本地代码其实也想同时退回线上代码,回滚到某个特定的版本,线上和线下代码保持一致。git revert用于撤销某次操作,此次操作之前和commit和history都会保留,即用一个新提交来消除一个历史提交所做的任何修改。revert之后你的本地代码会回滚到指定的历史版本,然后再git push。
2.git revert用法
git revert HEAD撤销前一次commit,会交互式打开文本编辑器提示输入提交信息。git revert HEAD --no-edit非交互式撤销前一次提交。git revert HEAD^撤销前一次commit。git revert commit <commit>撤销指定的版本,撤销也会作为一次提交进行保存。
3.git reset和git revert区别
git revert是用一次新的commit来回顾之前的commit,git reset是直接删除指定的commit。git reset是把HEAD向后移动了一下,而git revert是HEAD继续前进,只是新的commit的内容跟要revert的内容正好相反,能够抵消要被revert的内容。
七.可能会遇到的问题
1.Already up to date.
报错信息:
[root@prometheus-server31 devops]# git merge k8s
Already up to date.
[root@prometheus-server31 devops]# 错误原因:已经合并过分支了。可以忽略此报错,因为你可能已经完成分支合并啦。解决方案:检查工作区文件,验证是否已经完成分支合并。
2.Please make sure you have the correct access rights
报错信息:
[root@prometheus-server31 ~]# git clone git@gitee.com:jasonyin2020/yinzhengjie-devops-k8s.git
Cloning into 'yinzhengjie-devops-k8s'...
The authenticity of host 'gitee.com (180.76.198.77)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists.
[root@prometheus-server31 ~]# 错误原因:本地无权限访问gitee的远程仓库,可能是没有将服务器的公钥拷贝到gitee上。解决方案:在gitee中的WebUI检查是否配置了本地服务器生成的公钥信息。