当前位置:   article > 正文

Git命令常规操作

Git命令常规操作

目录

常用操作示意图

文件的状态变化周期

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

 环境准备

  1. [root@tty01 ~]# yum install -y git
  2. [root@tty01 ~]# git config --global user.name "username" #配置git使用用户,需要自己改个用户名,自己学习测试环境下用户名邮箱可以随便写
  3. [root@tty01 ~]# git config --global user.email "email@mail.com" #配置git使用邮箱,需要自己改个邮箱
  4. [root@tty01 ~]# git config --global color.ui true #语法高亮
  5. [root@tty01 ~]# useradd lisi
  6. [root@tty01 ~]# echo "1" |passwd --stdin lisi
  7. [root@tty01 ~]# mkdir /opt/git
  8. [root@tty01 ~]# cd /opt/git
  9. [root@tty01 git]# git init --bare tty.git
  10. [root@tty01 git]# chown -R lisi:lisi tty.git

1. 创建文件

使用第二台虚拟机

  1. [root@tty02 ~]# yum install -y git
  2. [root@tty02 ~]# git clone lisi@192.168.226.20:/opt/git/tty.git
  3. [root@tty02 ~]# ll
  4. total 4
  5. -rw-------. 1 root root 815 Jun 6 14:00 anaconda-ks.cfg
  6. drwxr-xr-x 3 root root 18 Jun 26 14:39 tty

创建文件 

  1. [root@tty02 ~]# cd tty
  2. [root@tty02 tty]# touch README
  3. [root@tty02 tty]# git status
  4. On branch master
  5. No commits yet
  6. Untracked files:
  7. (use "git add <file>..." to include in what will be committed)
  8. README
  9. nothing added to commit but untracked files present (use "git add" to track)

 添加文件跟踪,即添加到暂存区

  1. [root@tty02 tty]# git add ./*
  2. [root@tty02 tty]# git status
  3. On branch master
  4. No commits yet
  5. Untracked files:
  6. (use "git add <file>..." to include in what will be committed)
  7. README
  8. nothing added to commit but untracked files present (use "git add" to track)
  9. [root@tty02 tty]# git add ./*
  10. [root@tty02 tty]# git status
  11. On branch master
  12. No commits yet
  13. Changes to be committed:
  14. (use "git rm --cached <file>..." to unstage)
  15. new file: README

 文件会添加到.git的隐藏目录

执行 git add . 命令会将当前目录下所有修改过的和新创建的文件添加到 Git 的暂存区,这些文件在暂存区中等待被提交。Git 会将这些变更记录在 .git 目录中,以便后续提交操作可以将它们保存到仓库的历史记录中。 

  1. [root@tty02 tty]# ll -a
  2. total 0
  3. drwxr-xr-x 3 root root 32 Jun 26 14:40 .
  4. dr-xr-x---. 4 root root 190 Jun 26 14:40 ..
  5. drwxr-xr-x 7 root root 132 Jun 26 14:43 .git
  6. -rw-r--r-- 1 root root 0 Jun 26 14:40 README
  7. [root@tty02 tty]# tree .git/
  8. .git/
  9. ├── HEAD
  10. ├── branches
  11. ├── config
  12. ├── description
  13. ├── hooks
  14. │   ├── applypatch-msg.sample
  15. │   ├── commit-msg.sample
  16. │   ├── fsmonitor-watchman.sample
  17. │   ├── post-update.sample
  18. │   ├── pre-applypatch.sample
  19. │   ├── pre-commit.sample
  20. │   ├── pre-merge-commit.sample
  21. │   ├── pre-push.sample
  22. │   ├── pre-rebase.sample
  23. │   ├── pre-receive.sample
  24. │   ├── prepare-commit-msg.sample
  25. │   ├── push-to-checkout.sample
  26. │   ├── sendemail-validate.sample
  27. │   └── update.sample
  28. ├── index
  29. ├── info
  30. │   └── exclude
  31. ├── objects
  32. │   ├── e6
  33. │   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
  34. │   ├── info
  35. │   └── pack
  36. └── refs
  37. ├── heads
  38. └── tags
  39. 暂存区(Staging Area)是 Git 内部的数据结构,它并不是一个可以直接查看的文件夹。
  40. 暂存区的内容实际上存储在 .git/index 文件中,而不是以常规文件的形式存在于 .git 目录下。
  41. 虽然可以查看 .git 目录中的文件和结构,但无法直接看到具体哪些文件在暂存区。
  42. 要查看暂存区中哪些文件已被添加,可以使用 git status 或 git ls-files --stage 命令。
  43. 前者会显示哪些文件已被暂存,后者会显示暂存区中的所有文件及其状态信息。

由工作区提交到本地仓库

  1. [root@tty02 tty]# git commit -m 'first commit'
  2. [master (root-commit) c53f4b2] first commit
  3. 1 file changed, 0 insertions(+), 0 deletions(-)
  4. create mode 100644 README

查看git的状态

  1. [root@tty02 tty]# git status
  2. On branch master
  3. Your branch is based on 'origin/master', but the upstream is gone.
  4. (use "git branch --unset-upstream" to fixup)
  5. nothing to commit, working tree clean

提交后的git目录状态

  1. [root@tty02 tty]# tree .git/
  2. .git/
  3. ├── COMMIT_EDITMSG
  4. ├── HEAD
  5. ├── branches
  6. ├── config
  7. ├── description
  8. ├── hooks
  9. │   ├── applypatch-msg.sample
  10. │   ├── commit-msg.sample
  11. │   ├── fsmonitor-watchman.sample
  12. │   ├── post-update.sample
  13. │   ├── pre-applypatch.sample
  14. │   ├── pre-commit.sample
  15. │   ├── pre-merge-commit.sample
  16. │   ├── pre-push.sample
  17. │   ├── pre-rebase.sample
  18. │   ├── pre-receive.sample
  19. │   ├── prepare-commit-msg.sample
  20. │   ├── push-to-checkout.sample
  21. │   ├── sendemail-validate.sample
  22. │   └── update.sample
  23. ├── index
  24. ├── info
  25. │   └── exclude
  26. ├── logs
  27. │   ├── HEAD
  28. │   └── refs
  29. │   └── heads
  30. │   └── master
  31. ├── objects
  32. │   ├── 54
  33. │   │   └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd
  34. │   ├── c5
  35. │   │   └── 3f4b2fd175f7b8587f1269aa490acfa593bf30
  36. │   ├── e6
  37. │   │   └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
  38. │   ├── info
  39. │   └── pack
  40. └── refs
  41. ├── heads
  42. │   └── master
  43. └── tags

2. 修改原有文件

  1. [root@tty02 tty]# ll
  2. total 0
  3. -rw-r--r-- 1 root root 0 Jun 26 14:40 README
  4. [root@tty02 tty]# echo 123456 >> README
  5. [root@tty02 tty]# git add *
  6. [root@tty02 tty]# git commit -a -m "修改" #-a 表示直接提交
  7. [master 8ed6660] 修改
  8. 1 file changed, 1 insertion(+)

3. 删除原有文件 

  • 没有添加到暂存区的数据直接 rm 删除即可:

 没有使用git add命令创建的文件或目录可以直接rm删除

  1. [root@tty02 tty]# ll
  2. total 4
  3. -rw-r--r-- 1 root root 7 Jun 26 15:07 README
  4. [root@tty02 tty]# mkdir eryr
  5. [root@tty02 tty]# touch dfgeg
  6. [root@tty02 tty]# ll
  7. total 4
  8. -rw-r--r-- 1 root root 7 Jun 26 15:07 README
  9. -rw-r--r-- 1 root root 0 Jun 26 15:11 dfgeg
  10. drwxr-xr-x 2 root root 6 Jun 26 15:11 eryr
  11. [root@tty02 tty]# rm -rf eryr dfgeg
  12. [root@tty02 tty]# ll
  13. total 4
  14. -rw-r--r-- 1 root root 7 Jun 26 15:07 README

  • 对于添加到暂存区的数据 文件或目录:

对于已经使用git add命令的文件,先要从git暂存区域的追踪列表移除并不会删除当前工作目录内的数据文件使用 git rm --cached命令

  1. [root@tty02 tty]# git rm --cached README
  2. rm 'README'
  3. [root@tty02 tty]# ll
  4. total 4
  5. -rw-r--r-- 1 root root 7 Jun 26 15:07 README
  6. [root@tty02 tty]# git status
  7. On branch master
  8. Your branch is based on 'origin/master', but the upstream is gone.
  9. (use "git branch --unset-upstream" to fixup)
  10. Changes to be committed:
  11. (use "git restore --staged <file>..." to unstage)
  12. deleted: README
  13. Untracked files:
  14. (use "git add <file>..." to include in what will be committed)
  15. README
  16. 这条命令 git rm --cached README 成功地将 README 文件从 Git 的暂存区中移除了,并且没有删除工作目录中的实际文件。这意味着 Git 不再追踪 README 文件的变更,但该文件仍然存在于项目目录中。
  17. 之后执行了 git status 命令,将看到 Git 不再列出 README 文件作为需要被提交的文件。

对于已经使用git add命令的文件,先要从git暂存区域的追踪列表移除并和工作目录一起删除使用git rm -f命令

  1. [root@tty02 tty]# touch fff
  2. [root@tty02 tty]# ll
  3. total 4
  4. -rw-r--r-- 1 root root 7 Jun 26 15:07 README
  5. -rw-r--r-- 1 root root 0 Jun 26 15:26 fff
  6. [root@tty02 tty]# git add .
  7. [root@tty02 tty]# git status
  8. On branch master
  9. Your branch is based on 'origin/master', but the upstream is gone.
  10. (use "git branch --unset-upstream" to fixup)
  11. Changes to be committed:
  12. (use "git restore --staged <file>..." to unstage)
  13. new file: fff
  14. [root@tty02 tty]# git rm -f fff
  15. rm 'fff'
  16. [root@tty02 tty]# ll
  17. total 4
  18. -rw-r--r-- 1 root root 7 Jun 26 15:07 README

4. 重命名暂存区数据

  1. [root@tty02 tty]# git mv README notice
  2. [root@tty02 tty]# git status
  3. On branch master
  4. Your branch is based on 'origin/master', but the upstream is gone.
  5. (use "git branch --unset-upstream" to fixup)
  6. Changes to be committed:
  7. (use "git restore --staged <file>..." to unstage)
  8. renamed: README -> notice
  9. [root@tty02 tty]# ll
  10. total 4
  11. -rw-r--r-- 1 root root 7 Jun 26 15:07 notice

5. 查看历史记录

  1. • git log #→查看提交历史记录
  2. • git log -2 #→查看最近几条记录
  3. • git log -p -1 #→-p显示每次提交的内容差异,例如仅查看最近一次差异
  4. • git log --stat -2 #→--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息
  5. • git log --pretty=oneline #→--pretty根据不同的格式展示提交的历史信息
  6. • git log --pretty=fuller -2 #→以更详细的模式输出提交的历史记录
  7. • 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作者的修订时间。

命令实践

  1. [root@tty02 tty]# git log -2
  2. commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
  3. Author: Your Name <you@example.com>
  4. Date: Wed Jun 26 15:07:57 2024 +0800
  5. 修改
  6. commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
  7. Author: Your Name <you@example.com>
  8. Date: Wed Jun 26 15:01:17 2024 +0800
  9. first commit
  10. [root@tty02 tty]# git log -p -1
  11. commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
  12. Author: Your Name <you@example.com>
  13. Date: Wed Jun 26 15:07:57 2024 +0800
  14. 修改
  15. diff --git a/README b/README
  16. index e69de29..9f358a4 100644
  17. --- a/README
  18. +++ b/README
  19. @@ -0,0 +1 @@
  20. +123456
  21. [root@tty02 tty]# git log
  22. commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
  23. Author: Your Name <you@example.com>
  24. Date: Wed Jun 26 15:07:57 2024 +0800
  25. 修改
  26. commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
  27. Author: Your Name <you@example.com>
  28. Date: Wed Jun 26 15:01:17 2024 +0800
  29. first commit

6. 还原历史数据

恢复过程的原理

在 Git 中进行恢复数据或者查看历史版本的操作时,实际上涉及到了 Git 的分支、提交和指针等核心概念。以下是详细总结恢复过程的原理:

1. 提交与版本控制
  • Git 是一个分布式版本控制系统,它以提交(commit)为基本单位来管理和追踪项目的变更历史。
  • 每次提交都会生成一个唯一的 SHA-1 哈希值,用来标识这次提交的内容,包括提交的作者、时间戳、提交信息等。

2. HEAD 指针
  • 在 Git 中,HEAD 是一个指针,指向当前所在分支的最新一次提交。它指示了当前工作目录的状态,也就是你正在进行工作的版本。
  • 当你切换分支或者进行提交时,HEAD 会自动更新到最新的提交版本。

3. 相对引用:
  • Git 提供了一些相对引用来快速定位到其他提交版本:
  1. HEAD^: 表示当前提交的父提交,即上一个提交版本。
  2. HEAD^^: 表示上上一个提交版本,依此类推。
  3. HEAD~<n>: 表示往上数第 n 个提交版本,比如 HEAD~5 表示往上数第五个提交版本。

4. 分支和标签
  • Git 中的分支和标签是指向特定提交的指针。分支(branch)允许你在同一个项目中同时进行多个不同的开发线,而标签(tag)则是对某个特定提交的静态引用。

5. 恢复数据的过程
  • 当你需要恢复到之前的某个提交版本时,实际上是将 HEAD 指针移动到特定的提交上。
  • 使用 git checkout 命令或者 git switch 命令加上相应的引用(如分支名、标签名或相对引用),可以使 HEAD 指向指定的提交版本。
  • 例如,git checkout HEAD^ 将 HEAD 指针指向上一个提交版本,从而回退到上一个版本的状态。
  • 这种操作不会删除历史记录,而是在当前分支上切换到目标提交,可以随时返回到之前的状态。

恢复数据命令 

1. 还原到指定的提交版本 
  1. 如果希望将当前工作目录和暂存区完全还原到某个特定的提交版本,可以使用 git reset --hard 命令。
  2. 命令格式:
  3. git reset --hard <commit-hash>
  4. 其中 <commit-hash> 是你想要恢复到的提交版本的哈希值。
2. 撤销最近的提交并保留更改
  1. 如果只想撤销最近的一次提交(例如回退到上一个提交),但保留当前工作目录和暂存区的状态,可以使用 git reset --soft 命令。
  2. git reset --soft HEAD^
  3. 或者简写为:
  4. git reset --soft HEAD~
  5. 这两者效果相同,都会将 HEAD 指针移动到上一个提交版本(即 HEAD^ 或 HEAD~1),但不会改变工作目录和暂存区的内容。
3. 撤销单个文件的修改 
  1. 如果只是想撤销单个文件的修改,可以使用 git checkout 命令
  2. 命令格式:
  3. git checkout -- <file>
  4. 其中 <file> 是你想要还原的文件路径。
  5. 示例:
  6. 假设修改了文件 index.html,现在想撤销这些修改并恢复到最新提交的状态,可以执行:
  7. git checkout -- index.html
  8. 这会将 index.html 文件恢复到最新提交版本的状态,丢弃掉在工作目录中的未提交的更改。

注意事项:
  • 使用 git reset --hard 命令会丢弃当前工作目录和暂存区中未提交的修改,所以在执行之前请确保你不需要保存这些修改。
  • 在进行版本回退操作时,建议先使用 git log 命令查看提交历史,确认要回退到的具体提交版本。

测试命令

  1. [root@tty02 tty]# git log
  2. commit 8ed666015a697227eaf77539e481307055f03324 (HEAD -> master)
  3. Author: Your Name <you@example.com>
  4. Date: Wed Jun 26 15:07:57 2024 +0800
  5. 修改
  6. commit c53f4b2fd175f7b8587f1269aa490acfa593bf30
  7. Author: Your Name <you@example.com>
  8. Date: Wed Jun 26 15:01:17 2024 +0800
  9. first commit
  10. [root@tty02 tty]# ll
  11. total 4
  12. -rw-r--r-- 1 root root 7 Jun 26 15:07 notice

还原数据

  1. [root@tty02 tty]# git reset --hard c53f4b2fd175f7b8587f1269aa490acfa593bf30
  2. HEAD is now at c53f4b2 first commit
  3. [root@tty02 tty]# ll
  4. total 0
  5. -rw-r--r-- 1 root root 0 Jun 26 16:07 README
  6. [root@tty02 tty]# git log
  7. commit c53f4b2fd175f7b8587f1269aa490acfa593bf30 (HEAD -> master)
  8. Author: Your Name <you@example.com>
  9. Date: Wed Jun 26 15:01:17 2024 +0800
  10. first commit

7. 还原未来数据

未来数据" 在 Git 中指的是通过回退或重置操作后,由于 Git 的垃圾回收机制,可能导致一些之前的提交版本或者分支被清理掉,使得这些提交看起来已经不再存在于项目历史中,即使在 git log 中也找不到它们了。这种情况下,如果你后悔了并希望撤销这些更改。

1. 使用 git reflog 查找历史记录

git reflog 可以显示所有的 HEAD 指针移动记录,即使是过去被删除的提交也会显示出来。你可以通过 git reflog 找回之前的提交版本的哈希值,然后重新指向或者恢复到这个版本。

 会列出类似如下的输出,显示了 HEAD 的移动记录: 

  1. [root@tty02 tty]# git reflog
  2. 9191886 (HEAD -> master) HEAD@{1}: reset: moving to 919188685683917b4c3025aa240e3437921ba733
  3. d2470f1 HEAD@{2}: commit: commit
  4. e4b1113 HEAD@{3}: commit: commit
  5. 9191886 (HEAD -> master) HEAD@{4}: commit (initial): Initial commit

 测试:

  1. [root@tty02 tty]# ll #初始状态
  2. total 0
  3. -rw-r--r-- 1 root root 0 Jul 9 22:34 tty
  4. [root@tty02 tty]# git reset --hard 919188685683917b4c3025aa240e3437921ba733 #进行了一次回滚
  5. HEAD is now at 9191886 Initial commit
  6. [root@tty02 tty]# ll
  7. total 0
  8. -rw-r--r-- 1 root root 0 Jul 9 22:36 readme
  9. [root@tty02 tty]# git reflog #后悔回滚了,现在查找历史记录
  10. 9191886 (HEAD -> master) HEAD@{1}: reset: moving to 919188685683917b4c3025aa240e3437921ba733
  11. d2470f1 HEAD@{2}: commit: commit
  12. e4b1113 HEAD@{3}: commit: commit
  13. 9191886 (HEAD -> master) HEAD@{4}: commit (initial): Initial commit
  14. [root@tty02 tty]# git reset --hard d2470f1 #移动回到原版本
  15. HEAD is now at d2470f1 commit
  16. [root@tty02 tty]# ll
  17. total 0
  18. -rw-r--r-- 1 root root 0 Jul 9 22:37 tty

d2470f1e4b1113 是之前的提交版本的哈希值。你可以找到你需要的提交版本,并使用 git reset --hard <commit-hash> 将 HEAD 指针移动回到这个版本。 

在 Git 中,每个提交都有一个唯一的 SHA-1 哈希值来标识它。通常情况下,每次提交都会生成一个新的唯一哈希值,因为哈希值是根据提交内容计算的,如果提交内容有所不同,哈希值就会不同。

然而,在某些情况下可能会出现重复的哈希值,这通常是极其罕见且不太可能发生的情况,可能是由于以下几种原因:

  1. 碰撞概率:SHA-1 算法本身存在碰撞概率,即不同的数据可以计算出相同的哈希值。虽然在理论上存在碰撞,但在实践中,SHA-1 算法的安全性足以满足大多数应用的需求,尤其是在版本控制系统中。

  2. 手动操作或意外情况:在极少数情况下,可能会发生手动操作或意外情况导致两个提交的内容完全相同,从而生成相同的哈希值。这种情况通常是非常罕见的,并且通常会发生在极少数的特定场景下。

  3. 历史数据恢复:在使用 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)是用来标记特定提交版本的静态引用。它们通常用于标记发布版本或者重要的里程碑。标签可以帮助你方便地回溯和引用特定的版本,而不用记住复杂的提交哈希值。

类型

  1. 轻量标签(Lightweight tags)

    • 轻量标签就是一个指向提交对象(commit)的引用,类似于一个分支,但不会随着新的提交而移动。
    • 创建轻量标签:git tag <tag-name>
  2. 附注标签(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)。

轻量标签:仅是一个指向特定提交的引用,类似于一个分支指针,不包含额外的信息。

  1. [root@tty02 tty]# git tag biaoqian
  2. [root@tty02 tty]# git tag v1.0

 附注标签:存储在 Git 数据库中作为完整的对象,包含标签的名字、电子邮件地址、日期时间以及标签消息。

  1. [root@tty02 tty]# git tag -a ugo -m "tag message"
  2. [root@tty02 tty]# git tag -a v2.0 -m "Version 1.0 released"
2. 查看标签

使用 git tag 命令可以列出所有的标签。

  1. [root@tty02 tty]# git tag
  2. biaoqian
  3. ugo
  4. v1.0
  5. v2.0
3. 推送标签到远程仓库

推送单个标签:

[root@tty02 tty]# git push origin ugo

推送所有标签:

[root@tty02 tty]# git push --tags
4. 删除标签

删除本地标签:

  1. [root@tty02 tty]# git tag #删除前
  2. biaoqian
  3. ugo
  4. v1.0
  5. v2.0
  6. [root@tty02 tty]# git tag -d biaoqian #删除名为biaoqian的标签
  7. Deleted tag 'biaoqian' (was d2470f1)
  8. [root@tty02 tty]# git tag #删除后
  9. ugo
  10. v1.0
  11. v2.0

删除远程标签(需要删除权限):

  1. [root@tty02 tty]# git push origin --delete v1.0
  2. lisi@192.168.226.20's password:
  3. To 192.168.226.20:/opt/git/tty.git
  4. - [deleted] v1.0
5. 检出标签

可以通过将工作目录切换到标签所指向的提交来检出标签。

  1. [root@tty02 tty]# touch fhdhn #新增一个文件
  2. [root@tty02 tty]# git add .
  3. [root@tty02 tty]# git commit -m "Initial commit"
  4. [detached HEAD 4eea29c] Initial commit
  5. 1 file changed, 0 insertions(+), 0 deletions(-)
  6. create mode 100644 fhdhn
  7. [root@tty02 tty]# git tag
  8. ugo
  9. v1.0
  10. v2.0
  11. [root@tty02 tty]# ll
  12. total 0
  13. -rw-r--r-- 1 root root 0 Jul 9 22:56 fhdhn
  14. -rw-r--r-- 1 root root 0 Jul 9 22:37 tty
  15. [root@tty02 tty]# git checkout ugo #切换到标签 ugo
  16. Warning: you are leaving 1 commit behind, not connected to
  17. any of your branches:
  18. 4eea29c Initial commit
  19. If you want to keep it by creating a new branch, this may be a good time
  20. to do so with:
  21. git branch <new-branch-name> 4eea29c
  22. HEAD is now at d2470f1 commit
  23. [root@tty02 tty]# ll
  24. total 0
  25. -rw-r--r-- 1 root root 0 Jul 9 22:37 tty

Git 中的标签(Tag)通常与提交(Commit)相关联

标签和提交的关系

  • 标签本质上是指向一个特定提交的指针或引用。
  • 当你在特定的提交上创建标签时,这个标签将会指向该提交,并且可以通过标签名引用这个提交。

 如果你在一个分支上有多个标签,但是在最后一个标签所指向的提交状态下进行了提交,那么回滚操作通常会针对最后一个标签所指向的提交,用其他标签无效。

9. 对比数据

 在版本控制系统(如Git)中,对比数据通常指的是比较不同提交或分支之间的差异。这种比较可以帮助你了解代码或文件在不同时间点的变化,以及找出修改的具体内容。

1. 比较工具

git diff:Git 提供了 git diff 命令,用于比较工作目录中的当前状态和暂存区域的差异,或者比较暂存区域和最近提交的差异。例如:

  1. git diff # 比较工作目录和暂存区域的差异
  2. git diff --cached # 比较暂存区域和最近提交的差异
  3. git diff <commit> # 比较工作目录和指定提交的差异
  4. 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

这会比较 branch1branch2 分支最新提交之间的差异。

4. 文件内容的比较

如果只需要比较特定文件或目录的内容,可以在 git diff 命令后面加上文件或目录的路径。例如:

  1. git diff file.txt # 比较工作目录中 file.txt 的修改
  2. git diff --cached file.txt # 比较暂存区域中 file.txt 的修改
  3. git diff HEAD^ file.txt # 比较工作目录中 file.txt 与上一次提交的差异

测试

  1. [root@tty02 tty]# git log
  2. commit d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 (HEAD, tag: v2.0, tag: v1.0, tag: ugo, master)
  3. Author: Your Name <you@example.com>
  4. Date: Tue Jul 9 22:35:17 2024 +0800
  5. commit
  6. commit e4b111308a5f888c0fde8efd968f75fbdda0cd61
  7. Author: Your Name <you@example.com>
  8. Date: Tue Jul 9 22:34:41 2024 +0800
  9. commit
  10. commit 919188685683917b4c3025aa240e3437921ba733
  11. Author: Your Name <you@example.com>
  12. Date: Tue Jul 9 22:33:55 2024 +0800
  13. Initial commit
  14. #比较两个提交的不同
  15. [root@tty02 tty]# git diff d2470f142c7721ebbff8a3b8f5f7752ecbae67c8 e4b111308a5f888c0fde8efd968f75fbdda0cd61
  16. diff --git a/readme b/readme
  17. new file mode 100644
  18. index 0000000..e69de29
  19. #Git Diff 结果:输出表明在 d2470f1 和 e4b1113 之间的差异是 readme 文件的新增。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/831152
推荐阅读
相关标签
  

闽ICP备14008679号