git操作指南

发布于:2025-06-05 ⋅ 阅读:(27) ⋅ 点赞:(0)

三种状态

已提交(committed)已修改(modified)已暂存(staged)

  • 已修改表示修改了文件,但还没保存到数据库中。
  • 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
  • 已提交表示数据已经安全地保存在本地数据库中。

安装 Git

初次运行 Git 前的配置

san@sandeMacBook-Pro /etc % git config --list --show-origin
file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig   credential.helper=osxkeychain
file:/Library/Developer/CommandLineTools/usr/share/git-core/gitconfig   init.defaultbranch=main
file:/Users/san/.gitconfig      url.git.shimo.im:.insteadof=http://git.shimo.im/
file:/Users/san/.gitconfig      url.git@git.shimo.im:.insteadof=https://git.shimo.im/
file:/Users/san/.gitconfig      user.name=zws
file:/Users/san/.gitconfig      user.email=nicker@aliyun.com
file:/Users/san/.gitconfig      core.excludesfile=/Users/san/.gitignore_global
file:/Users/san/.gitconfig      difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
file:/Users/san/.gitconfig      difftool.sourcetree.path=
file:/Users/san/.gitconfig      mergetool.sourcetree.cmd=/Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
file:/Users/san/.gitconfig      mergetool.sourcetree.trustexitcode=true
file:/Users/san/.gitconfig      commit.template=/Users/san/.stCommitMsg

用户信息

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

检查配置信息

如果想要检查你的配置,可以使用 git config --list 命令来列出所有 Git 当时能找到的配置:

san@sandeMacBook-Pro /etc % git config --list
credential.helper=osxkeychain
init.defaultbranch=main
url.git.shimo.im:.insteadof=http://git.shimo.im/
url.git@git.shimo.im:.insteadof=https://git.shimo.im/
user.name=zws
user.email=nicker@aliyun.com
core.excludesfile=/Users/san/.gitignore_global
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
commit.template=/Users/san/.stCommitMsg
san@sandeMacBook-Pro /etc % git config user.name
zws

help

san@sandeMacBook-Pro /etc % git help

用户信息

 $ git config --global user.name "John Doe"
 $ git config --global user.email johndoe@example.com

创建项目

san@sandeMacBook-Pro ~ % cd mycode
san@sandeMacBook-Pro mycode % ls
bookstore		comments		consumer		go			go_2026			go_debug_data		gotest			publisher		simple-http-server
san@sandeMacBook-Pro mycode % mkdir gitTest
san@sandeMacBook-Pro mycode % cd gitTest
san@sandeMacBook-Pro gitTest % git init
Initialized empty Git repository in /Users/san/mycode/gitTest/.git/
san@sandeMacBook-Pro gitTest % go mod init gitTest
go: creating new go.mod: module gitTest

获取 Git 仓库

通常有两种获取 Git 项目仓库的方式:

  1. 将尚未进行版本控制的本地目录转换为 Git 仓库;
  2. 从其它服务器 克隆 一个已存在的 Git 仓库。

两种方式都会在你的本地机器上得到一个工作就绪的 Git 仓库。

在已存在目录中初始化仓库

git init

git add *

git commit -m “”

$ cd /Users/user/my_project

an@sandeMacBook-Pro gitTest % pwd
/Users/san/mycode/gitTest

san@sandeMacBook-Pro gitTest % git init
Reinitialized existing Git repository in /Users/san/mycode/gitTest/.git/

san@sandeMacBook-Pro gitTest % ls -a
.		..		.git		.idea		01-test1	go.mod


san@sandeMacBook-Pro gitTest % git add  .idea/
san@sandeMacBook-Pro gitTest % git commit -m 'initial project version'
[main f4f1b7e] initial project version
 4 files changed, 31 insertions(+)
 create mode 100644 .idea/.gitignore
 create mode 100644 .idea/gitTest.iml
 create mode 100644 .idea/modules.xml
 create mode 100644 .idea/vcs.xml
san@sandeMacBook-Pro gitTest %

克隆现有的仓库

$ git clone https://github.com/libgit2/libgit2 mylibgit

检查当前文件状态

san@sandeMacBook-Pro gitTest % git status
On branch main
nothing to commit, working tree clean

跟踪新文件

git add README

暂存已修改的文件

新增一个文件

san@sandeMacBook-Pro gitTest % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   01-test1/main.go

no changes added to commit (use "git add" and/or "git commit -a")

Git add

san@sandeMacBook-Pro gitTest % git add *
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   01-test1/main.go

san@sandeMacBook-Pro gitTest %

再次修改

san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   01-test1/main.go

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   01-test1/main.go

文件同时出现在暂存区和非暂存区,再次提交到暂存区

san@sandeMacBook-Pro gitTest % git add *
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   01-test1/main.go

状态简览

san@sandeMacBook-Pro gitTest % git status -s
M  01-test1/main.go
san@sandeMacBook-Pro gitTest % git status --short
M  01-test1/main.go

状态解读

 $ git status -s
   M README
  MM Rakefile
  A  lib/git.rb
  M  lib/simplegit.rb
  ?? LICENSE.txt
  1. ?? 新添加的未跟踪文件前面有 ?? 标记,
  2. A 新添加到暂存区中的文件前面有 A 标记,
  3. M 修改过的文件前面有 M 标记

忽略文件

创建文件.gitignore

*.[oa]
*~

第一行告诉 Git 忽略所有以 .o 或 .a 结尾的文件。一般这类对象文件和存档文件都是编译过程中出现的。 第二 行告诉 Git 忽略所有名字以波浪符(~)结尾的文件,许多文本编辑软件(比如 Emacs)都用这样的文件名保存 副本。 此外,你可能还需要忽略 log,tmp 或者 pid 目录,以及自动生成的文档等等

文件 .gitignore 的格式规范如下:
• 所有空行或者以 # 开头的行都会被 Git 忽略。
• 可以使用标准的 glob 模式匹配,它会递归地应用在整个工作区中。
• 匹配模式可以以(/)开头防止递归。
• 匹配模式可以以(/)结尾指定目录。
• 要忽略指定模式以外的文件或目录,可以在模式前加上叹号(!)取反。

所谓的 glob 模式是指 shell 所使用的简化了的正则表达式。 星号(*)匹配零个或多个任意字符;[abc] 匹配
任何一个列在方括号中的字符 (这个例子要么匹配一个 a,要么匹配一个 b,要么匹配一个 c); 问号(?)只
匹配一个任意字符;如果在方括号中使用短划线分隔两个字符, 表示所有在这两个字符范围内的都可以匹配
(比如 [0-9] 表示匹配所有 0 到 9 的数字)。 使用两个星号()表示匹配任意中间目录,比如 a//z 可以
匹配 a/z 、 a/b/z 或 a/b/c/z 等。
我们再看一个 .gitignore 文件的例子:

# 忽略所有的 .a 文件
*.a
# 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
!lib.a
# 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
/TODO
# 忽略任何目录下名为 build 的文件夹
build/
# 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
doc/*.txt
# 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
doc/**/*.pdf

GitHub 有一个十分详细的针对数十种项目及语言的 .gitignore 文件列表, 你可以在 https://github.com/github/gitignore 找到它。

查看已暂存和未暂存的修改

修改已经暂存的文件

san@sandeMacBook-Pro gitTest % git status -s
MM 01-test1/main.go
?? .gitignore
san@sandeMacBook-Pro gitTest % git diff
diff --git a/01-test1/main.go b/01-test1/main.go
index 987e7e4..fef5379 100644
--- a/01-test1/main.go
+++ b/01-test1/main.go
@@ -5,5 +5,5 @@ import "fmt"
 func main() {
        // git add
        fmt.Println("Hello World")
-
+       // git diff
 }

此命令比较的是工作目录中当前文件和暂存区域快照之间的差异。 也就是修改之后还没有暂存起来的变化内 容。若要查看已暂存的将要添加到下次提交里的内容,可以用 git diff --staged 命令。 这条命令将比对已暂存

san@sandeMacBook-Pro gitTest % git diff --staged
diff --git a/01-test1/main.go b/01-test1/main.go
index 91e7378..987e7e4 100644
--- a/01-test1/main.go
+++ b/01-test1/main.go
@@ -3,5 +3,7 @@ package main
 import "fmt"

 func main() {
+       // git add
        fmt.Println("Hello World")
+
 }

然后用 git diff --cached 查看已经暂存起来的变化( --staged 和 --cached 是同义词):

san@sandeMacBook-Pro gitTest % git diff --cached
diff --git a/01-test1/main.go b/01-test1/main.go
index 91e7378..987e7e4 100644
--- a/01-test1/main.go
+++ b/01-test1/main.go
@@ -3,5 +3,7 @@ package main
 import "fmt"

 func main() {
+       // git add
        fmt.Println("Hello World")
+
 }

提交更新

san@sandeMacBook-Pro gitTest % git add *
san@sandeMacBook-Pro gitTest % git commit -m "learn"
[main 887ea32] learn
 1 file changed, 2 insertions(+)
san@sandeMacBook-Pro gitTest %

跳过使用暂存区域

san@sandeMacBook-Pro gitTest % git diff
diff --git a/01-test1/main.go b/01-test1/main.go
index fef5379..e701b6c 100644
--- a/01-test1/main.go
+++ b/01-test1/main.go
@@ -6,4 +6,5 @@ func main() {
        // git add
        fmt.Println("Hello World")
        // git diff
+       // git commit -a
 }

Git 提供了一个跳过使用暂 存区域的方式, 只要在提交的时候,给 git commit 加上 -a 选项,Git 就会自动把所有已经跟踪过的文件暂存 起来一并提交,从而跳过 git add 步骤:

san@sandeMacBook-Pro gitTest % git commit -a -m "-a"
[main 584868e] -a
 1 file changed, 1 insertion(+)
san@sandeMacBook-Pro gitTest % git status
On branch main
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore

nothing added to commit but untracked files present (use "git add" to track)
san@sandeMacBook-Pro gitTest %

解决没带上的文件

san@sandeMacBook-Pro gitTest % git add  .gitignore
san@sandeMacBook-Pro gitTest % git commit -a -m "-a"
[main 1720081] -a
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore

移除文件

san@sandeMacBook-Pro gitTest % git add *
san@sandeMacBook-Pro gitTest % git commit  -m "PROJECTS.md"
[main cb023ae] PROJECTS.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 PROJECTS.md

san@sandeMacBook-Pro gitTest % git status
On branch main
nothing to commit, working tree clean

移除

san@sandeMacBook-Pro gitTest % rm  "PROJECTS.md"
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	deleted:    PROJECTS.md

no changes added to commit (use "git add" and/or "git commit -a")
san@sandeMacBook-Pro gitTest % git rm  "PROJECTS.md"
rm 'PROJECTS.md'
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    PROJECTS.md

下一次提交时,该文件就不再纳入版本管理了。 如果要删除之前修改过或已经放到暂存区的文件,则必须使用 强制删除选项 -f(译注:即 force 的首字

另外一种情况是,我们想把文件从 Git 仓库中删除(亦即从暂存区域移除),但仍然希望保留在当前工作目录 中。 换句话说,你想让文件保留在磁盘,但是并不想让 Git 继续跟踪

$ git rm --cached README

git rm 命令后面可以列出文件或者目录的名字,也可以使用 glob 模式。比如:
$ git rm log/\*.log
注意到星号 * 之前的反斜杠 \, 因为 Git 有它自己的文件模式扩展匹配方式,所以我们不用 shell 来帮忙展开。
此命令删除 log/ 目录下扩展名为 .log 的所有文件。 类似的比如:
$ git rm \*~
该命令会删除所有名字以 ~ 结尾的文件。

移动文件

san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    PROJECTS.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	README.md

san@sandeMacBook-Pro gitTest % git add *
san@sandeMacBook-Pro gitTest % git commit -m "README.md"
[main da98bc5] README.md
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename PROJECTS.md => README.md (100%)
san@sandeMacBook-Pro gitTest % git status
On branch main
nothing to commit, working tree clean

执行

san@sandeMacBook-Pro gitTest %  git mv README.md README
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    README.md -> README

本质

$ mv README.md README
$ git rm README.md
$ git add README

查看提交历史

san@sandeMacBook-Pro gitTest % git log
commit da98bc5917d59ba359ae891407744db4f2ee37bb (HEAD -> main)
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 11:01:48 2025 +0800

    README.md

commit cb023ae4594aa0940033bce622f43edab67e0327
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 10:52:34 2025 +0800

    PROJECTS.md

commit 1720081fa9d4052dd8812e9dc6267bae18044aee
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 10:50:41 2025 +0800

    -a

不传入任何参数的默认情况下,git log 会按时间先后顺序列出所有的提交,最近的更新排在最上面。 正如你 所看到的,这个命令会列出每个提交的 SHA-1 校验和、作者的名字和电子邮件地址、提交时间以及提交说明。

它会显示每次提交所引入的差异(按 补丁 的格式输出)

san@sandeMacBook-Pro gitTest %  git log -p -2
commit da98bc5917d59ba359ae891407744db4f2ee37bb (HEAD -> main)
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 11:01:48 2025 +0800

    README.md

diff --git a/PROJECTS.md b/README.md
similarity index 100%
rename from PROJECTS.md
rename to README.md

commit cb023ae4594aa0940033bce622f43edab67e0327
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 10:52:34 2025 +0800

    PROJECTS.md

diff --git a/PROJECTS.md b/PROJECTS.md
new file mode 100644
index 0000000..e69de29

每 次提交的简略统计信息

san@sandeMacBook-Pro gitTest %  git log --stat -2
commit da98bc5917d59ba359ae891407744db4f2ee37bb (HEAD -> main)
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 11:01:48 2025 +0800

    README.md

 PROJECTS.md => README.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

commit cb023ae4594aa0940033bce622f43edab67e0327
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 10:52:34 2025 +0800

    PROJECTS.md

 PROJECTS.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

比如 oneline 会将每个提交放在一行显示,在浏览大量的提交时非常有用

san@sandeMacBook-Pro gitTest %  git log --pretty=oneline
da98bc5917d59ba359ae891407744db4f2ee37bb (HEAD -> main) README.md
cb023ae4594aa0940033bce622f43edab67e0327 PROJECTS.md
1720081fa9d4052dd8812e9dc6267bae18044aee -a
584868e6afc83592aab25ef210f30e534a0244df -a
887ea32c106ab22e8883c727b2c832edcf6ad44b learn
f4f1b7ed7e6dec64b56f60c40992f468d007dbbd initial project version
0a504cbef2de16cd856a84e1cce7f2c40fa9ad58 init project version
san@sandeMacBook-Pro gitTest %  git log --pretty=format:"%h - %an, %ar : %s"
da98bc5 - zws, 52 minutes ago : README.md
cb023ae - zws, 61 minutes ago : PROJECTS.md
1720081 - zws, 63 minutes ago : -a
584868e - zws, 64 minutes ago : -a
887ea32 - zws, 65 minutes ago : learn
f4f1b7e - zws, 85 minutes ago : initial project version
0a504cb - zws, 5 days ago : init project version

git log 的常用选项

选项 说明
-p 按补丁格式显示每个提交引入的差异。
–stat 显示每次提交的文件修改统计信息。
–shortstat 只显示 --stat 中最后的行数修改添加移除统计。
–name-only 仅在提交信息后显示已修改的文件清单。
–name-status 显示新增、修改、删除的文件清单。
–abbrev-commit 仅显示 SHA-1 校验和所有 40 个字符中的前几个字符。
–relative-date 使用较短的相对时间而不是完整格式显示日期(比如“2 weeks ago”)。
–graph 在日志旁以 ASCII 图形显示分支与合并历史。
–pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline、short、full、fuller 和
format(用来定义自己的格式)。
–oneline --pretty=oneline --abbrev-commit 合用的简写。

限制输出长度

 git log --since=2.weeks

撤消操作

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了。 此时,可以运行带有 --amend 选 项的提交命令来重新提交: git commit --amend

这个命令会将暂存区中的文件提交。 如果自上次提交以来你还未做任何修改(例如,在上次提交后马上执行了 此命令), 那么快照会保持不变,而你所修改的只是提交信息。

当你在修补最后的提交时,与其说是修复旧提交,倒不如说是完全用一个 新的提交 替换旧的 提交, 理解这一点非常重要。从效果上来说,就像是旧有的提交从未存在过一样,它并不会 出现在仓库的历史中。 修补提交最明显的价值是可以稍微改进你最后的提交,而不会让“啊,忘了添加一个文

没搞懂

san@sandeMacBook-Pro gitTest % git commit -m 'initial commit'
[main 9ec3281] initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename README.md => README (100%)
san@sandeMacBook-Pro gitTest % git add forgotten_file
san@sandeMacBook-Pro gitTest %  git commit --amend
[main 33e2838] initial commit
 Date: Wed Jun 4 11:58:07 2025 +0800
 2 files changed, 0 insertions(+), 0 deletions(-)
 rename README.md => README (100%)
 create mode 100644 forgotten_file
san@sandeMacBook-Pro gitTest % git status
On branch main
nothing to commit, working tree clean

取消暂存的文件

san@sandeMacBook-Pro gitTest % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   forgotten_file

no changes added to commit (use "git add" and/or "git commit -a")
san@sandeMacBook-Pro gitTest % git diff
diff --git a/forgotten_file b/forgotten_file
index 8dd8eaa..991c8b8 100644
--- a/forgotten_file
+++ b/forgotten_file
@@ -1 +1,3 @@
-撤消对文件的修改
\ No newline at end of file
+撤消对文件的修改
+
+新增无效修改
\ No newline at end of file

请务必记得 git checkout — 是一个危险的命令。 你对那个文件在本地的任何修 改都会消失——Git 会用最近提交的版本覆盖掉它。 除非你确实清楚不想要对那个文件的本地 修改了,否则请不要使用这个命令。

san@sandeMacBook-Pro gitTest % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   forgotten_file

no changes added to commit (use "git add" and/or "git commit -a")
san@sandeMacBook-Pro gitTest % git diff
diff --git a/forgotten_file b/forgotten_file
index 8dd8eaa..991c8b8 100644
--- a/forgotten_file
+++ b/forgotten_file
@@ -1 +1,3 @@
-撤消对文件的修改
\ No newline at end of file
+撤消对文件的修改
+
+新增无效修改
\ No newline at end of file
san@sandeMacBook-Pro gitTest % git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   forgotten_file

no changes added to commit (use "git add" and/or "git commit -a")
san@sandeMacBook-Pro gitTest %  git checkout -- forgotten_file
san@sandeMacBook-Pro gitTest % git status
On branch main
nothing to commit, working tree clean
san@sandeMacBook-Pro gitTest %

远程仓库的使用

查看远程仓库

san@sandeMacBook-Pro gitTest % git remote add origin https://gitee.com/san_7/git.git
san@sandeMacBook-Pro gitTest %  git remote
origin

你也可以指定选项 -v,会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL。

san@sandeMacBook-Pro gitTest % git remote -v
origin	https://gitee.com/san_7/git.git (fetch)
origin	https://gitee.com/san_7/git.git (push)

如果你的远程仓库不止一个,该命令会将它们全部列出。

添加远程仓库

运行 git remote add 添加一个新的远程 Git 仓库

git remote add <shortname> <url> 

现在你可以在命令行中使用字符串 pb 来代替整个 URL

san@sandeMacBook-Pro gitTest %  git fetch origin

从远程仓库中抓取与拉取

git fetch origin 会抓取克隆(或上一次抓取)后新推送的所有工作。

推送到远程仓库

当你想分享你的项目时,必须将其推送到上游。 这个命令很简单:git push 。 当你 想要将 master 分支推送到 origin 服务器时(再次说明,

san@sandeMacBook-Pro gitTest % git push origin master
error: src refspec master does not match any
error: failed to push some refs to 'https://gitee.com/san_7/git.git'

查看某个远程仓库

如果想要查看某一个远程仓库的更多信息,可以使用 git remote show 命令。 如果想以一个特定的缩写名运行这个命令,例如 origin,会得到像下面类似的信息:

san@sandeMacBook-Pro gitTest %  git remote show origin
* remote origin
  Fetch URL: https://gitee.com/san_7/git.git
  Push  URL: https://gitee.com/san_7/git.git
  HEAD branch: (unknown)

远程仓库的重命名与移除

你可以运行 git remote rename 来修改一个远程仓库的简写名

san@sandeMacBook-Pro gitTest % git remote rename origin main
san@sandeMacBook-Pro gitTest % git remote
main
san@sandeMacBook-Pro gitTest %

值得注意的是这同样也会修改你所有远程跟踪的分支名字。 那些过去引用 pb/master 的现在会引用 paul/master。

打标签

列出标签

san@sandeMacBook-Pro gitTest % git tag
san@sandeMacBook-Pro gitTest %  git tag -l "v1.8.5*"

创建标签

轻量标签很像一个不会改变的分支——它只是某个特定提交的引用。 而附注标签是存储在 Git 数据库中的一个完整对象, 它们是可以被校验的,其中包含打标签者的名字、电子邮件 地址、日期时间, 此外还有一个标签信息,并且可以使用 GNU Privacy Guard (GPG)签名并验证。 通常会建 议创建附注标签,这样你可以拥有以上所有信息。但是如果你只是想用一个临时的标签, 或者因为某些原因不 想要保存这些信息,那么也可以用轻量标签。

附注标签

san@sandeMacBook-Pro gitTest % git tag
san@sandeMacBook-Pro gitTest %  git tag -l "v1.8.5*"
san@sandeMacBook-Pro gitTest %  git tag -a v1.4 -m "my version 1.4"
san@sandeMacBook-Pro gitTest % git tag
v1.4
san@sandeMacBook-Pro gitTest %  git tag -a v1 -m "my version 1"
san@sandeMacBook-Pro gitTest % git tag
v1
v1.4

-m 选项指定了一条将会存储在标签中的信息。 如果没有为附注标签指定一条信息,Git 会启动编辑器要求你输 入信息。 通过使用 git show 命令可以看到标签信息和与之对应的提交信息:

san@sandeMacBook-Pro gitTest % git show v1
tag v1
Tagger: zws <nicker@aliyun.com>
Date:   Wed Jun 4 18:51:42 2025 +0800

my version 1

commit 8a5b9a54979c10d8b6847f858e0457d11ad94281 (HEAD -> main, tag: v1.4, tag: v1)
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 12:00:53 2025 +0800

    撤消对文件的修改

diff --git a/forgotten_file b/forgotten_file
index e69de29..8dd8eaa 100644
--- a/forgotten_file
+++ b/forgotten_file
@@ -0,0 +1 @@
+撤消对文件的修改
\ No newline at end of file

轻量标签

创建轻量标签,不需要使用 -a、-s 或 -m 选项,只需要提供标签名字:

san@sandeMacBook-Pro gitTest % git tag v1.1

后期打标签

san@sandeMacBook-Pro gitTest %  git log --pretty=oneline
8a5b9a54979c10d8b6847f858e0457d11ad94281 (HEAD -> main, tag: v1.4, tag: v1.1, tag: v1, tag: v0.1, main/main) 撤消对文件的修改
33e28386cb8b688b3d5aad7b483e5aa3c83ba170 initial commit
da98bc5917d59ba359ae891407744db4f2ee37bb README.md
cb023ae4594aa0940033bce622f43edab67e0327 PROJECTS.md
1720081fa9d4052dd8812e9dc6267bae18044aee -a
584868e6afc83592aab25ef210f30e534a0244df -a
887ea32c106ab22e8883c727b2c832edcf6ad44b learn
f4f1b7ed7e6dec64b56f60c40992f468d007dbbd initial project version
0a504cbef2de16cd856a84e1cce7f2c40fa9ad58 init project version

现在,假设在 v1.2 时你忘记给项目打标签,也就是在 “updated rakefile” 提交。 你可以在之后补上标签。 要 在那个提交上打标签,你需要在命令的末尾指定提交的校验和(或部分校验和):

san@sandeMacBook-Pro gitTest % git tag -a v1.2 8a5b9a5 -m "v1.2"
san@sandeMacBook-Pro gitTest % git tag
v0.1
v1
v1.1
v1.2
v1.4

查看

san@sandeMacBook-Pro gitTest %  git show v1.2
tag v1.2
Tagger: zws <nicker@aliyun.com>
Date:   Wed Jun 4 18:58:11 2025 +0800

v1.2

commit 8a5b9a54979c10d8b6847f858e0457d11ad94281 (HEAD -> main, tag: v1.4, tag: v1.2, tag: v1.1, tag: v1, tag: v0.1, main/main)
Author: zws <nicker@aliyun.com>
Date:   Wed Jun 4 12:00:53 2025 +0800

    撤消对文件的修改

diff --git a/forgotten_file b/forgotten_file
index e69de29..8dd8eaa 100644
--- a/forgotten_file
+++ b/forgotten_file
@@ -0,0 +1 @@
+撤消对文件的修改
\ No newline at end of file

共享标签

删除标签

检出标签

Git 别名

Git 分支


网站公告

今日签到

点亮在社区的每一天
去签到