Git版本管理系統快速上手指南

尹正杰發表於2024-11-18

                                              作者:尹正傑

版權宣告:原創作品,謝絕轉載!否則將追究法律責任。

目錄
  • 一.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.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

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 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]# 

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 master

No commits yet

Changes 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 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]# 

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 +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]# 

8.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]# 

二.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 branch
  k8s
* master
[root@prometheus-server31 devops]# 

3.切換分支

	1.切換到已經存在的分支
[root@prometheus-server31 devops]# git branch
  k8s
* 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
* k8s
  master
[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
* docker
  k8s
  master
[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
* docker
  k8s
  master
[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 info
 1 file changed, 1 insertion(+)
 create mode 100644 office.txt
[root@prometheus-server31 devops]# 

	
	2.k8s分支建立檔案
[root@prometheus-server31 devops]# git branch
* docker
  k8s
  master
[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 branch
  docker
* k8s
  master
[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 info
 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: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 branch
  docker
* k8s
  master
[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 
  docker
  k8s
* 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-forward
 k8s.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 branch
  docker
  k8s
* 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 branch
  docker
* 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
* flink
  master
[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 flink
 1 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 branch
  flink
* kafka
  master
[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 kafka
 1 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 '' -q

	2.檢視公鑰,將自己本地的公鑰複製到伺服器
[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
* kafka
  master
[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 +0800

Flink v2.0版本釋出

commit 9e74e39327f26b9d3d8dc241eb85b26413e37ed1 (tag: Flink-v2.0, origin/flink, flink)
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 22:32:15 2024 +0800

    add flink

diff --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 +0800

    add kafka

commit 9e74e39327f26b9d3d8dc241eb85b26413e37ed1 (tag: Flink-v2.0, origin/flink, flink)
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 22:32:15 2024 +0800

    add flink

commit b69638dee63a0fd9e631014c284aff5749d90960 (origin/master, master)
Merge: 828e023 5b1e29f
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 22:11:58 2024 +0800

    Merge branch 'docker'

commit 828e02325fb9d4a3b162c6743cc21507af290eae
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 22:09:32 2024 +0800

    add k8s info

commit 5b1e29faee912c5578040abb914a3e50e3749249
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 22:07:42 2024 +0800

    add docker info

commit 005b95d954d811ae67a219e8cb377766f44fe968
Author: jasonyin2020 <y1053419035@qq.com>
Date:   Sun Nov 17 20:06:50 2024 +0800

    add 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 +0800

Kafka 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 +0800

    add kafka

diff --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 branch
  flink
* kafka
  master
[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.git
   932ec59..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檢查是否配置了本地伺服器生成的公鑰資訊。

相關文章