目錄
常用操作示意圖
文件的狀態變化周期
1. 創建文件
2. 修改原有文件
3. 刪除原有文件?
沒有添加到暫存區的數據直接 rm 刪除即可:
對于添加到暫存區的數據?文件或目錄:
4. 重命名暫存區數據
5.?查看歷史記錄
6.?還原歷史數據
恢復過程的原理
1. 提交與版本控制:
2. HEAD 指針:
3. 相對引用:
4. 分支和標簽:
5. 恢復數據的過程:
恢復數據命令?
1. 還原到指定的提交版本?
2. 撤銷最近的提交并保留更改
3. 撤銷單個文件的修改?
注意事項:
7. 還原未來數據
1. 使用 git reflog 查找歷史記錄
2. 使用 git fsck --lost-found 查找丟失的對象?
8.?標簽使用
類型
使用方法
測試:?
1. 創建標簽
2. 查看標簽
3. 推送標簽到遠程倉庫
4. 刪除標簽
5. 檢出標簽
9. 對比數據
1. 比較工具
2.?提交之間的比較
3.?分支之間的比較
4.?文件內容的比較
測試
命令 | 作用 | 示例 |
---|---|---|
git init | 初始化一個新的 Git 倉庫 | git init |
git clone [url] | 克隆遠程倉庫 | git clone https://github.com/user/repo.git |
git status | 顯示工作目錄和暫存區的狀態 | git status |
git add [file] | 添加文件到暫存區 | git add README.md |
git commit -m "[message]" | 提交暫存區的文件并添加提交信息 | git commit -m "Initial commit" |
git commit | 提交暫存區的文件,打開文本編輯器添加提交信息 | git commit |
git log | 顯示提交歷史 | git log |
git log --oneline | 簡潔的提交歷史 | git log --oneline |
git diff | 顯示未暫存的文件更改 | git diff |
git diff --staged | 顯示已暫存的文件更改 | git diff --staged |
git branch | 列出所有本地分支 | git branch |
git branch [branch-name] | 創建新分支 | git branch dev |
git checkout [branch-name] | 切換到指定分支 | git checkout dev |
git checkout -b [branch-name] | 創建并切換到新分支 | git checkout -b dev |
git merge [branch-name] | 合并指定分支到當前分支 | git merge dev |
git remote -v | 顯示所有遠程倉庫 | git remote -v |
git remote add [name] [url] | 添加新的遠程倉庫 | git remote add origin https://github.com/user/repo.git |
git push [remote] [branch] | 推送分支到遠程倉庫 | git push origin master |
git push -u [remote] [branch] | 推送分支到遠程倉庫并將其設置為上游分支 | git push -u origin master |
git fetch | 從遠程倉庫獲取更新但不合并 | git fetch |
git pull | 獲取并合并遠程倉庫的更新 | git pull origin master |
git reset [file] | 取消暫存區中的文件更改 | git reset README.md |
git reset --hard [commit] | 將當前分支重置到指定的提交,并丟棄所有更改 | git reset --hard HEAD~1 |
git rm [file] | 從工作目錄和暫存區中刪除文件 | git rm README.md |
git stash | 將未提交的更改保存到棧中 | git stash |
git stash pop | 恢復最后一次保存的更改并從棧中移除 | git stash pop |
常用操作示意圖
文件的狀態變化周期
以下實操為2臺虛擬機,均坐了關閉防火墻和selinux,進行時間同步,系統為Rocky_linux9.4
?環境準備
[root@tty01 ~]# yum install -y git
[root@tty01 ~]# git config --global user.name "username" #配置git使用用戶,需要自己改個用戶名
[root@tty01 ~]# git config --global user.email "email@mail.com" #配置git使用郵箱,需要自己改個郵箱
[root@tty01 ~]# git config --global color.ui true #語法高亮[root@tty01 ~]# useradd lisi
[root@tty01 ~]# echo "1" |passwd --stdin lisi
[root@tty01 ~]# mkdir /opt/git
[root@tty01 ~]# cd /opt/git
[root@tty01 git]# git init --bare tty.git
[root@tty01 git]# chown -R lisi:lisi tty.git
1. 創建文件
使用第二臺虛擬機
[root@tty02 ~]# yum install -y git
[root@tty02 tty]# git config --global user.email "you@example.com" #自己學習測試環境下用戶名郵箱可以隨便寫
[root@tty02 tty]# git config --global user.name "Your Name"[root@tty02 ~]# git clone lisi@192.168.226.20:/opt/git/tty.git
[root@tty02 ~]# ll
total 4
-rw-------. 1 root root 815 Jun 6 14:00 anaconda-ks.cfg
drwxr-xr-x 3 root root 18 Jun 26 14:39 tty
創建文件?
[root@tty02 ~]# cd tty
[root@tty02 tty]# touch README
[root@tty02 tty]# git status
On branch masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)READMEnothing added to commit but untracked files present (use "git add" to track)
?添加文件跟蹤,即添加到暫存區
[root@tty02 tty]# git add ./*
[root@tty02 tty]# git status
On branch masterNo commits yetUntracked files:(use "git add <file>..." to include in what will be committed)READMEnothing added to commit but untracked files present (use "git add" to track)
[root@tty02 tty]# git add ./*
[root@tty02 tty]# git status
On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage)new file: README
?文件會添加到.git的隱藏目錄
執行 git add .
命令會將當前目錄下所有修改過的和新創建的文件添加到 Git 的暫存區,這些文件在暫存區中等待被提交。Git 會將這些變更記錄在 .git
目錄中,以便后續提交操作可以將它們保存到倉庫的歷史記錄中。?
[root@tty02 tty]# ll -a
total 0
drwxr-xr-x 3 root root 32 Jun 26 14:40 .
dr-xr-x---. 4 root root 190 Jun 26 14:40 ..
drwxr-xr-x 7 root root 132 Jun 26 14:43 .git
-rw-r--r-- 1 root root 0 Jun 26 14:40 README[root@tty02 tty]# tree .git/
.git/
├── HEAD
├── branches
├── config
├── description
├── hooks
│?? ├── applypatch-msg.sample
│?? ├── commit-msg.sample
│?? ├── fsmonitor-watchman.sample
│?? ├── post-update.sample
│?? ├── pre-applypatch.sample
│?? ├── pre-commit.sample
│?? ├── pre-merge-commit.sample
│?? ├── pre-push.sample
│?? ├── pre-rebase.sample
│?? ├── pre-receive.sample
│?? ├── prepare-commit-msg.sample
│?? ├── push-to-checkout.sample
│?? ├── sendemail-validate.sample
│?? └── update.sample
├── index
├── info
│?? └── exclude
├── objects
│?? ├── e6
│?? │?? └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│?? ├── info
│?? └── pack
└── refs├── heads└── tags暫存區(Staging Area)是 Git 內部的數據結構,它并不是一個可以直接查看的文件夾。
暫存區的內容實際上存儲在 .git/index 文件中,而不是以常規文件的形式存在于 .git 目錄下。雖然可以查看 .git 目錄中的文件和結構,但無法直接看到具體哪些文件在暫存區。
要查看暫存區中哪些文件已被添加,可以使用 git status 或 git ls-files --stage 命令。
前者會顯示哪些文件已被暫存,后者會顯示暫存區中的所有文件及其狀態信息。
由工作區提交到本地倉庫
[root@tty02 tty]# git commit -m 'first commit'
[master (root-commit) c53f4b2] first commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 README
查看git的狀態
[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)nothing to commit, working tree clean
提交后的git目錄狀態
[root@tty02 tty]# tree .git/
.git/
├── COMMIT_EDITMSG
├── HEAD
├── branches
├── config
├── description
├── hooks
│?? ├── applypatch-msg.sample
│?? ├── commit-msg.sample
│?? ├── fsmonitor-watchman.sample
│?? ├── post-update.sample
│?? ├── pre-applypatch.sample
│?? ├── pre-commit.sample
│?? ├── pre-merge-commit.sample
│?? ├── pre-push.sample
│?? ├── pre-rebase.sample
│?? ├── pre-receive.sample
│?? ├── prepare-commit-msg.sample
│?? ├── push-to-checkout.sample
│?? ├── sendemail-validate.sample
│?? └── update.sample
├── index
├── info
│?? └── exclude
├── logs
│?? ├── HEAD
│?? └── refs
│?? └── heads
│?? └── master
├── objects
│?? ├── 54
│?? │?? └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd
│?? ├── c5
│?? │?? └── 3f4b2fd175f7b8587f1269aa490acfa593bf30
│?? ├── e6
│?? │?? └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│?? ├── info
│?? └── pack
└── refs├── heads│?? └── master└── tags
2. 修改原有文件
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 26 14:40 README
[root@tty02 tty]# echo 123456 >> README
[root@tty02 tty]# git add *
[root@tty02 tty]# git commit -a -m "修改" #-a 表示直接提交
[master 8ed6660] 修改1 file changed, 1 insertion(+)
3. 刪除原有文件?
-
沒有添加到暫存區的數據直接 rm 刪除即可:
?沒有使用git add命令創建的文件或目錄可以直接rm刪除
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README
[root@tty02 tty]# mkdir eryr
[root@tty02 tty]# touch dfgeg
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README
-rw-r--r-- 1 root root 0 Jun 26 15:11 dfgeg
drwxr-xr-x 2 root root 6 Jun 26 15:11 eryr
[root@tty02 tty]# rm -rf eryr dfgeg
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README
-
對于添加到暫存區的數據?文件或目錄:
對于已經使用git add命令的文件,先要從git暫存區域的追蹤列表移除并不會刪除當前工作目錄內的數據文件使用?git rm --cached命令
[root@tty02 tty]# git rm --cached README
rm 'README'
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)Changes to be committed:(use "git restore --staged <file>..." to unstage)deleted: READMEUntracked files:(use "git add <file>..." to include in what will be committed)README這條命令 git rm --cached README 成功地將 README 文件從 Git 的暫存區中移除了,并且沒有刪除工作目錄中的實際文件。這意味著 Git 不再追蹤 README 文件的變更,但該文件仍然存在于項目目錄中。
之后執行了 git status 命令,將看到 Git 不再列出 README 文件作為需要被提交的文件。
對于已經使用git add命令的文件,先要從git暫存區域的追蹤列表移除并和工作目錄一起刪除使用git rm -f命令
[root@tty02 tty]# touch fff
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README
-rw-r--r-- 1 root root 0 Jun 26 15:26 fff
[root@tty02 tty]# git add .
[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)Changes to be committed:(use "git restore --staged <file>..." to unstage)new file: fff[root@tty02 tty]# git rm -f fff
rm 'fff'
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 README
4. 重命名暫存區數據
[root@tty02 tty]# git mv README notice
[root@tty02 tty]# git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.(use "git branch --unset-upstream" to fixup)Changes to be committed:(use "git restore --staged <file>..." to unstage)renamed: README -> notice[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 notice
5.?查看歷史記錄
? git log #→查看提交歷史記錄? git log -2 #→查看最近幾條記錄? git log -p -1 #→-p顯示每次提交的內容差異,例如僅查看最近一次差異? git log --stat -2 #→--stat簡要顯示數據增改行數,這樣能夠看到提交中修改過的內容,對文件添加或移動的行數,并在最后列出所有增減行的概要信息? git log --pretty=oneline #→--pretty根據不同的格式展示提交的歷史信息? git log --pretty=fuller -2 #→以更詳細的模式輸出提交的歷史記錄? git log --pretty=fomat:"%h %cn" #→查看當前所有提交記錄的簡短SHA-1哈希字串與提交著的姓名。
使用format參數來指定具體的輸出格式
格式 | 說明 |
---|---|
%s | 提交說明。 |
%cd | 提交日期。 |
%an | 作者的名字。 |
%cn | 提交者的姓名。 |
%ce | 提交者的電子郵件。 |
%H | 提交對象的完整SHA-1哈希字串。 |
%h | 提交對象的簡短SHA-1哈希字串。 |
%T | 樹對象的完整SHA-1哈希字串。 |
%t | 樹對象的簡短SHA-1哈希字串。 |
%P | 父對象的完整SHA-1哈希字串。 |
%p | 父對象的簡短SHA-1哈希字串。 |
%ad | 作者的修訂時間。 |
命令實踐
[root@tty02 tty]# git log -2
commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:07:57 2024 +0800修改commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:01:17 2024 +0800first commit[root@tty02 tty]# git log -p -1
commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:07:57 2024 +0800修改diff --git a/README b/README
index e69de29..9f358a4 100644
--- a/README
+++ b/README
@@ -0,0 +1 @@
+123456[root@tty02 tty]# git log
commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:07:57 2024 +0800修改commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:01:17 2024 +0800first commit
6.?還原歷史數據
恢復過程的原理
在 Git 中進行恢復數據或者查看歷史版本的操作時,實際上涉及到了 Git 的分支、提交和指針等核心概念。以下是詳細總結恢復過程的原理:
1. 提交與版本控制:
- Git 是一個分布式版本控制系統,它以提交(commit)為基本單位來管理和追蹤項目的變更歷史。
- 每次提交都會生成一個唯一的 SHA-1 哈希值,用來標識這次提交的內容,包括提交的作者、時間戳、提交信息等。
2. HEAD 指針:
- 在 Git 中,HEAD 是一個指針,指向當前所在分支的最新一次提交。它指示了當前工作目錄的狀態,也就是你正在進行工作的版本。
- 當你切換分支或者進行提交時,HEAD 會自動更新到最新的提交版本。
3. 相對引用:
- Git 提供了一些相對引用來快速定位到其他提交版本:
HEAD^: 表示當前提交的父提交,即上一個提交版本。
HEAD^^: 表示上上一個提交版本,依此類推。
HEAD~<n>: 表示往上數第 n 個提交版本,比如 HEAD~5 表示往上數第五個提交版本。
4. 分支和標簽:
- Git 中的分支和標簽是指向特定提交的指針。分支(branch)允許你在同一個項目中同時進行多個不同的開發線,而標簽(tag)則是對某個特定提交的靜態引用。
5. 恢復數據的過程:
- 當你需要恢復到之前的某個提交版本時,實際上是將 HEAD 指針移動到特定的提交上。
- 使用
git checkout
命令或者git switch
命令加上相應的引用(如分支名、標簽名或相對引用),可以使 HEAD 指向指定的提交版本。 - 例如,
git checkout HEAD^
將 HEAD 指針指向上一個提交版本,從而回退到上一個版本的狀態。 - 這種操作不會刪除歷史記錄,而是在當前分支上切換到目標提交,可以隨時返回到之前的狀態。
恢復數據命令?
1. 還原到指定的提交版本?
如果希望將當前工作目錄和暫存區完全還原到某個特定的提交版本,可以使用 git reset --hard 命令。命令格式:
git reset --hard <commit-hash>其中 <commit-hash> 是你想要恢復到的提交版本的哈希值。
2. 撤銷最近的提交并保留更改
如果只想撤銷最近的一次提交(例如回退到上一個提交),但保留當前工作目錄和暫存區的狀態,可以使用 git reset --soft 命令。
git reset --soft HEAD^
或者簡寫為:
git reset --soft HEAD~
這兩者效果相同,都會將 HEAD 指針移動到上一個提交版本(即 HEAD^ 或 HEAD~1),但不會改變工作目錄和暫存區的內容。
3. 撤銷單個文件的修改?
如果只是想撤銷單個文件的修改,可以使用 git checkout 命令
命令格式:
git checkout -- <file>
其中 <file> 是你想要還原的文件路徑。示例:
假設修改了文件 index.html,現在想撤銷這些修改并恢復到最新提交的狀態,可以執行:
git checkout -- index.html
這會將 index.html 文件恢復到最新提交版本的狀態,丟棄掉在工作目錄中的未提交的更改。
注意事項:
- 使用
git reset --hard
命令會丟棄當前工作目錄和暫存區中未提交的修改,所以在執行之前請確保你不需要保存這些修改。 - 在進行版本回退操作時,建議先使用
git log
命令查看提交歷史,確認要回退到的具體提交版本。
測試命令
[root@tty02 tty]# git log
commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:07:57 2024 +0800修改commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:01:17 2024 +0800first commit
[root@tty02 tty]# ll
total 4
-rw-r--r-- 1 root root 7 Jun 26 15:07 notice
還原數據
[root@tty02 tty]# git reset --hard c53f4b2fd175f7b8587f1269aa490acfa593bf30
HEAD is now at c53f4b2 first commit
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 26 16:07 README[root@tty02 tty]# git log
commit c53f4b2fd175f7b8587f1269aa490acfa593bf30 (HEAD -> master)
Author: Your Name <you@example.com>
Date: Wed Jun 26 15:01:17 2024 +0800first commit
7. 還原未來數據
未來數據" 在 Git 中指的是通過回退或重置操作后,由于 Git 的垃圾回收機制,可能導致一些之前的提交版本或者分支被清理掉,使得這些提交看起來已經不再存在于項目歷史中,即使在 git log
中也找不到它們了。這種情況下,如果你后悔了并希望撤銷這些更改。
1. 使用 git reflog
查找歷史記錄
git reflog
可以顯示所有的 HEAD 指針移動記錄,即使是過去被刪除的提交也會顯示出來。你可以通過 git reflog
找回之前的提交版本的哈希值,然后重新指向或者恢復到這個版本。
?會列出類似如下的輸出,顯示了 HEAD 的移動記錄:?
[root@tty02 tty]# git reflog
9191886 (HEAD -> master) HEAD@{1}: reset: moving to 919188685683917b4c3025aa240e3437921ba733
d2470f1 HEAD@{2}: commit: commit
e4b1113 HEAD@{3}: commit: commit
9191886 (HEAD -> master) HEAD@{4}: commit (initial): Initial commit
?測試:
[root@tty02 tty]# ll #初始狀態
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:34 tty
[root@tty02 tty]# git reset --hard 919188685683917b4c3025aa240e3437921ba733 #進行了一次回滾
HEAD is now at 9191886 Initial commit
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:36 readme[root@tty02 tty]# git reflog #后悔回滾了,現在查找歷史記錄
9191886 (HEAD -> master) HEAD@{1}: reset: moving to 919188685683917b4c3025aa240e3437921ba733
d2470f1 HEAD@{2}: commit: commit
e4b1113 HEAD@{3}: commit: commit
9191886 (HEAD -> master) HEAD@{4}: commit (initial): Initial commit
[root@tty02 tty]# git reset --hard d2470f1 #移動回到原版本
HEAD is now at d2470f1 commit
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:37 tty
d2470f1
和 e4b1113
是之前的提交版本的哈希值。你可以找到你需要的提交版本,并使用 git reset --hard <commit-hash>
將 HEAD 指針移動回到這個版本。?
在 Git 中,每個提交都有一個唯一的 SHA-1 哈希值來標識它。通常情況下,每次提交都會生成一個新的唯一哈希值,因為哈希值是根據提交內容計算的,如果提交內容有所不同,哈希值就會不同。
然而,在某些情況下可能會出現重復的哈希值,這通常是極其罕見且不太可能發生的情況,可能是由于以下幾種原因:
-
碰撞概率:SHA-1 算法本身存在碰撞概率,即不同的數據可以計算出相同的哈希值。雖然在理論上存在碰撞,但在實踐中,SHA-1 算法的安全性足以滿足大多數應用的需求,尤其是在版本控制系統中。
-
手動操作或意外情況:在極少數情況下,可能會發生手動操作或意外情況導致兩個提交的內容完全相同,從而生成相同的哈希值。這種情況通常是非常罕見的,并且通常會發生在極少數的特定場景下。
-
歷史數據恢復:在使用 Git 進行歷史數據恢復或修復時,可能會手動編輯提交內容或合并歷史分支,這種情況下可能會導致一些特殊的提交內容重復出現。
2. 使用 git fsck --lost-found
查找丟失的對象?
?如果通過 git reflog
找不到需要的提交版本,可以嘗試使用 git fsck --lost-found
命令來查找被丟棄但未被清理的對象。這個命令會列出所有未被引用的對象(包括提交、樹和 blob),你可能需要手動查找并恢復。
git fsck --lost-found
命令用于檢查 Git 倉庫中的對象(如提交、樹、blob 等)的完整性,并且如果有未被引用(即沒有被任何分支或標簽引用)的對象,它會將這些對象輸出到 .git/lost-found/other
目錄下。
[root@tty02 tty]# git fsck --lost-found
8.?標簽使用
前面回滾使用的是一串字符串,又長又難記。
在 Git 中,標簽(Tag)是用來標記特定提交版本的靜態引用。它們通常用于標記發布版本或者重要的里程碑。標簽可以幫助你方便地回溯和引用特定的版本,而不用記住復雜的提交哈希值。
類型
-
輕量標簽(Lightweight tags):
- 輕量標簽就是一個指向提交對象(commit)的引用,類似于一個分支,但不會隨著新的提交而移動。
- 創建輕量標簽:
git tag <tag-name>
-
附注標簽(Annotated tags):
- 附注標簽存儲在 Git 數據庫中作為完整的對象。它包含標簽的名字、電子郵件地址、日期時間以及標簽消息。
- 創建附注標簽:
git tag -a <tag-name> -m "tag message"
使用方法
- 查看標簽:使用
git tag
命令可以列出所有標簽。 - 創建標簽:如上述所示,使用
-a
或者不帶任何選項來創建不同類型的標簽。 - 刪除標簽:使用
git tag -d <tag-name>
來刪除本地標簽。 - 推送標簽:默認情況下,
git push
不會推送標簽到遠程倉庫,你可以使用git push origin <tag-name>
來推送單個標簽,或者使用git push --tags
推送所有標簽。 - 檢出標簽:可以通過
git checkout <tag-name>
將工作目錄切換到標簽所指向的提交。
測試:?
1. 創建標簽
在 Git 中,有兩種主要的標簽類型:輕量標簽(Lightweight tags)和附注標簽(Annotated tags)。
輕量標簽:僅是一個指向特定提交的引用,類似于一個分支指針,不包含額外的信息。
[root@tty02 tty]# git tag biaoqian
[root@tty02 tty]# git tag v1.0
?附注標簽:存儲在 Git 數據庫中作為完整的對象,包含標簽的名字、電子郵件地址、日期時間以及標簽消息。
[root@tty02 tty]# git tag -a ugo -m "tag message"
[root@tty02 tty]# git tag -a v2.0 -m "Version 1.0 released"
2. 查看標簽
使用 git tag
命令可以列出所有的標簽。
[root@tty02 tty]# git tag
biaoqian
ugo
v1.0
v2.0
3. 推送標簽到遠程倉庫
推送單個標簽:
[root@tty02 tty]# git push origin ugo
推送所有標簽:
[root@tty02 tty]# git push --tags
4. 刪除標簽
刪除本地標簽:
[root@tty02 tty]# git tag #刪除前
biaoqian
ugo
v1.0
v2.0[root@tty02 tty]# git tag -d biaoqian #刪除名為biaoqian的標簽Deleted tag 'biaoqian' (was d2470f1)
[root@tty02 tty]# git tag #刪除后
ugo
v1.0
v2.0
刪除遠程標簽(需要刪除權限):
[root@tty02 tty]# git push origin --delete v1.0
lisi@192.168.226.20's password:
To 192.168.226.20:/opt/git/tty.git- [deleted] v1.0
5. 檢出標簽
可以通過將工作目錄切換到標簽所指向的提交來檢出標簽。
[root@tty02 tty]# touch fhdhn #新增一個文件
[root@tty02 tty]# git add .
[root@tty02 tty]# git commit -m "Initial commit"
[detached HEAD 4eea29c] Initial commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 fhdhn
[root@tty02 tty]# git tag
ugo
v1.0
v2.0
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:56 fhdhn
-rw-r--r-- 1 root root 0 Jul 9 22:37 tty[root@tty02 tty]# git checkout ugo #切換到標簽 ugo
Warning: you are leaving 1 commit behind, not connected to
any of your branches:4eea29c Initial commitIf you want to keep it by creating a new branch, this may be a good time
to do so with:git branch <new-branch-name> 4eea29cHEAD is now at d2470f1 commit
[root@tty02 tty]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 9 22:37 tty
Git 中的標簽(Tag)通常與提交(Commit)相關聯
標簽和提交的關系:
- 標簽本質上是指向一個特定提交的指針或引用。
- 當你在特定的提交上創建標簽時,這個標簽將會指向該提交,并且可以通過標簽名引用這個提交。
?如果你在一個分支上有多個標簽,但是在最后一個標簽所指向的提交狀態下進行了提交,那么回滾操作通常會針對最后一個標簽所指向的提交,用其他標簽無效。
9. 對比數據
?在版本控制系統(如Git)中,對比數據通常指的是比較不同提交或分支之間的差異。這種比較可以幫助你了解代碼或文件在不同時間點的變化,以及找出修改的具體內容。
1. 比較工具
git diff:Git 提供了 git diff
命令,用于比較工作目錄中的當前狀態和暫存區域的差異,或者比較暫存區域和最近提交的差異。例如:
git diff # 比較工作目錄和暫存區域的差異
git diff --cached # 比較暫存區域和最近提交的差異
git diff <commit> # 比較工作目錄和指定提交的差異
git diff <commit1> <commit2> # 比較兩個提交之間的差異
git difftool:如果你配置了比較工具(如Beyond Compare、KDiff3等),可以使用 git difftool
命令打開圖形化工具來進行比較。
2.?提交之間的比較
可以通過指定不同的提交或標簽來比較它們之間的差異。例如:
git diff <commit1> <commit2>
這會顯示 <commit1>
和 <commit2>
之間的差異,包括文件內容的修改、新增或刪除等。
3.?分支之間的比較
如果需要比較不同分支之間的差異,可以使用 git diff
命令來比較它們的最新提交。例如:
git diff branch1..branch2
這會比較 branch1
和 branch2
分支最新提交之間的差異。
4.?文件內容的比較
如果只需要比較特定文件或目錄的內容,可以在 git diff
命令后面加上文件或目錄的路徑。例如:
git diff file.txt # 比較工作目錄中 file.txt 的修改
git diff --cached file.txt # 比較暫存區域中 file.txt 的修改
git diff HEAD^ file.txt # 比較工作目錄中 file.txt 與上一次提交的差異
測試
[root@tty02 tty]# git log
commit d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 (HEAD, tag: v2.0, tag: v1.0, tag: ugo, master)
Author: Your Name <you@example.com>
Date: Tue Jul 9 22:35:17 2024 +0800commitcommit e4b111308a5f888c0fde8efd968f75fbdda0cd61
Author: Your Name <you@example.com>
Date: Tue Jul 9 22:34:41 2024 +0800commitcommit 919188685683917b4c3025aa240e3437921ba733
Author: Your Name <you@example.com>
Date: Tue Jul 9 22:33:55 2024 +0800Initial commit#比較兩個提交的不同
[root@tty02 tty]# git diff d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 e4b111308a5f888c0fde8efd968f75fbdda0cd61
diff --git a/readme b/readme
new file mode 100644
index 0000000..e69de29#Git Diff 結果:輸出表明在 d2470f1 和 e4b1113 之間的差異是 readme 文件的新增。