当前位置: 首页 > news >正文

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

 环境准备

[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

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)。

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

[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

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

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 文件的新增。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • C++ //练习 14.45 编写类型转换运算符将一个Sales_data对象分别转换成string和double,你认为这些运算符的返回值应该是什么?
  • 电脑多开卡顿的所有原因汇总
  • IEC62056标准体系简介-2.IEC62056标准体系及对象标识系统(OBIS)
  • 嵌入式底层开发 入门学习路线
  • Python深度理解系列之【排序算法——冒泡排序】
  • 使用OpenCV的absdiff函数报错
  • 深圳唯创知音革新健康监测!语音播报,蓝牙传输,电量检测—全能型智能血压计三大方案,让关爱更“声”动人心
  • 智能眼镜火热发展 AI+AR或将成为主流趋势?
  • Django ModelForm用法详解 —— Python
  • Redis 7.x 系列【21】主从复制
  • Elasticsearch详细介绍
  • 数据库第五次作业---多表查询
  • Linux grep技巧 结合awk查询
  • 阶段三:项目开发---搭建项目前后端系统基础架构:任务11:搭建项目后台系统基础架构
  • 利用node连接mongodb实现一个小型后端服务系统demo
  • 2017年终总结、随想
  • Android单元测试 - 几个重要问题
  • Druid 在有赞的实践
  • IndexedDB
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • mongo索引构建
  • Vue ES6 Jade Scss Webpack Gulp
  • 从零开始的无人驾驶 1
  • 大整数乘法-表格法
  • 基于axios的vue插件,让http请求更简单
  • 技术发展面试
  • 坑!为什么View.startAnimation不起作用?
  • 聊聊directory traversal attack
  • 前端相关框架总和
  • 通过git安装npm私有模块
  • 写代码的正确姿势
  • 云大使推广中的常见热门问题
  • ​ssh免密码登录设置及问题总结
  • #{}和${}的区别?
  • #laravel部署安装报错loadFactoriesFrom是undefined method #
  • #vue3 实现前端下载excel文件模板功能
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (delphi11最新学习资料) Object Pascal 学习笔记---第14章泛型第2节(泛型类的类构造函数)
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第2节(共同的基类)
  • (八)Flask之app.route装饰器函数的参数
  • (笔记)M1使用hombrew安装qemu
  • (翻译)Quartz官方教程——第一课:Quartz入门
  • (规划)24届春招和25届暑假实习路线准备规划
  • (四)软件性能测试
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • .bat批处理出现中文乱码的情况
  • .NET Core 成都线下面基会拉开序幕
  • .NET Micro Framework 4.2 beta 源码探析
  • .Net6 Api Swagger配置
  • .net6使用Sejil可视化日志
  • .Net通用分页类(存储过程分页版,可以选择页码的显示样式,且有中英选择)
  • [ HTML + CSS + Javascript ] 复盘尝试制作 2048 小游戏时遇到的问题
  • [ vulhub漏洞复现篇 ] ThinkPHP 5.0.23-Rce
  • [C/C++]数据结构----顺序表的实现(增删查改)