Git-命令-使用 Tag 使用方式

本文深入解析Git中的标签(tag)概念,对比分支(branch),阐述其在版本发布中的应用,并提供实用的tag创建、查看、删除、重命名及检出的命令指南。

Git-命令行-使用 Tag 标记你的代码

前言

文章转自
作者:DRPrincess 来源:优快云 原文:https://blog.youkuaiyun.com/qq_32452623/article/details/73949509?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!


正文开始之前,我想我们需要弄明白几个问题:

1.tag 是什么? 
2.使用tag 的好处? 
3.tag 和 branch 的区别以及使用场景?

  • tag 是什么? 
    tag , 翻译过来是标签的意思,顾名思义,标签是为了标记某种事物。 
    tag 是 Git 版本库的一个快照,指向某个 commit 的指针。

  • 使用tag 的好处? 
    tag 的存在,是因为我们需要这种标记的功能。目前的项目开发中,当发布版本时 tag 就派上用场了。例如 v1.0.1,v1.0.2… 
    另外,git 提供了 tag 的增删改查一系列操作,在 tag 的使用上,可谓非常之方便。

  • tag 和 branch 的区别以及使用场景? 
    想到这里,你可能觉得 tag 和 branch 有点相似。没错,的确是有点像,但是它们的职责分工和本质都是不同的。 
    tag 对应某次 commit, 是一个点,是不可移动的。 
    branch 对应一系列 commit,是很多点连成的一根线,有一个HEAD 指针,是可以依靠 HEAD 指针移动的。

    所以,两者的区别决定了使用方式,改动代码用 branch ,不改动只查看用 tag。

    tag 和 branch 的相互配合使用,有时候起到非常方便的效果,例如 已经发布了 v1.0 v2.0 v3.0 三个版本,这个时候,我突然想不改现有代码的前提下,在 v2.0 的基础上加个新功能,作为 v4.0 发布。就可以 检出 v2.0 的代码作为一个 branch ,然后作为开发分支。

tag 的简单使用

以下命令都是我使用 tag 过程中一般会使用到的,可以说都是常用命令。

1.创建标签

需要说明的是,创建 tag 是基于本地分支的 commit,而且与分支的推送是两回事,就是说分支已经推送到远程了,但是你的 tag 并没有,如果把 tag 推送到远程分支上,需要另外执行 tag 的推送命令。

git tag <tagName> //创建本地tag
git push origin <tagName> //推送到远程仓库
  • 1
  • 2

若存在很多未推送的本地标签,你想一次全部推送的话,可以使用一下的命令:

git push origin --tags  
  • 1
  • 2

以上是基于本地当前分支的最后的一个 commit 创建的 tag ,但是如果不想以最后一个,只想以某一个特定的提交为 tag ,也是可以的,只要你知道 commit 的 id。

git log --pretty=oneline //查看当前分支的提交历史,里面包含 commit id
git tag -a <tagName> <commitId>
  • 1
  • 2

2.查看标签

查看本地某个 tag 的详细信息:

git show <tagName>
  • 1

查看本地所有 tag :

//下面两个命令都可以
git tag 
git tag -l
  • 1
  • 2
  • 3

查看远程所有 tag:

git ls-remote --tags origin
  • 1
  • 2

3.删除标签

本地 tag 的删除:

git tag -d <tagName>
  • 1

远程 tag 的删除:

git push origin :<tagName>
  • 1

4.重命名标签

这个本质上是删除掉旧名字 tag ,然后再新建新名字 tag ,然后实现重命名的作用。

如果 tag 只存在本地,那么只需要删除本地的旧名字 tag ,然后新建新名字 tag:

git tag -d <oldTagName>
git tag <newTagName>
git push origin <newTagName> //推送到远程仓库
  • 1
  • 2
  • 3

若已经推送到远程了,那么不仅要删除本地的,还要删除远程的,再重新创建和推送:

git tag -d <oldTagName>
git push origin :<oldTagName>
git tag <newTagName>
git push origin <newTagName> //推送到远程仓库
  • 1
  • 2
  • 3
  • 4
  • 5

5.检出标签

命令如下:

git checkout -b <branchName> <tagName>
  • 1

因为 tag 本身指向的就是一个 commit,所以和根据 commit id 检出分支是一个道理。 
但是需要特别说明的是,如果我们想要修改 tag 检出代码分支,那么虽然分支中的代码改变了,但是 tag 标记的 commit 还是同一个,标记的代码是不会变的,这个要格外的注意。

文章转自
作者:DRPrincess 来源:优快云 原文:https://blog.youkuaiyun.com/qq_32452623/article/details/73949509?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!
 

SUBDIR git-gui SUBDIR gitk-git SUBDIR templates install -d -m 755 '/usr/local/bin' install -d -m 755 '/usr/local/libexec/git-core' install git-daemon git-http-backend git-imap-send git-sh-i18n--envsubst git-shell git-http-fetch git-http-push git-remote-http git-remote-https git-remote-ftp git-remote-ftps git-bisect git-difftool--helper git-filter-branch git-merge-octopus git-merge-one-file git-merge-resolve git-mergetool git-quiltimport git-request-pull git-submodule git-web--browse git-add--interactive git-archimport git-cvsexportcommit git-cvsimport git-cvsserver git-send-email git-svn git-p4 git-instaweb '/usr/local/libexec/git-core' install -m 644 git-mergetool--lib git-rebase--preserve-merges git-sh-i18n git-sh-setup '/usr/local/libexec/git-core' install git git-receive-pack git-shell git-upload-archive git-upload-pack git-cvsserver '/usr/local/bin' make -C templates DESTDIR='' install make[1]: Entering directory `/usr/local/src/git-2.30.0/templates' install -d -m 755 '/usr/local/share/git-core/templates' (cd blt && tar cf - .) | \ (cd '/usr/local/share/git-core/templates' && umask 022 && tar xof -) make[1]: Leaving directory `/usr/local/src/git-2.30.0/templates' install -d -m 755 '/usr/local/libexec/git-core/mergetools' install -m 644 mergetools/* '/usr/local/libexec/git-core/mergetools' install -d -m 755 '/usr/local/share/locale' (cd po/build/locale && tar cf - .) | \ (cd '/usr/local/share/locale' && umask 022 && tar xof -) install -d -m 755 '/usr/local/share/perl5' (cd perl/build/lib && tar cf - .) | \ (cd '/usr/local/share/perl5' && umask 022 && tar xof -) make -C gitweb install make[1]: Entering directory `/usr/local/src/git-2.30.0/gitweb' make[2]: Entering directory `/usr/local/src/git-2.30.0' make[2]: `GIT-VERSION-FILE' is up to date. make[2]: Leaving directory `/usr/local/src/git-2.30.0' install -d -m 755 '/usr/local/share/gitweb' install -m 755 gitweb.cgi '/usr/local/share/gitweb' install -d -m 755 '/usr/local/share/gitweb/static' install -m 644 static/gitweb.js static/gitweb.css static/git-logo.png static/git-favicon.png '/usr/local/share/gitweb/static' make[1]: Leaving directory `/usr/local/src/git-2.30.0/gitweb' make -C gitk-git install make[1]: Entering directory `/usr/local/src/git-2.30.0/gitk-git' install -d -m 755 '/usr/local/bin' install -m 755 gitk-wish '/usr/local/bin'/gitk install -d -m 755 '/usr/local/share/gitk/lib/msgs' install -m 644 po/pt_br.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/bg.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/zh_cn.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ja.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ca.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/sv.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/it.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/de.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/pt_pt.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/fr.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/ru.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/vi.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/hu.msg '/usr/local/share/gitk/lib/msgs' && install -m 644 po/es.msg '/usr/local/share/gitk/lib/msgs' && true make[1]: Leaving directory `/usr/local/src/git-2.30.0/gitk-git' make -C git-gui gitexecdir='/usr/local/libexec/git-core' install make[1]: Entering directory `/usr/local/src/git-2.30.0/git-gui' DEST /usr/local/libexec/git-core INSTALL 755 git-gui INSTALL 755 git-gui--askpass LINK git-citool -> git-gui DEST /usr/local/share/git-gui/lib INSTALL 644 tclIndex INSTALL 644 themed.tcl INSTALL 644 spellcheck.tcl INSTALL 644 branch_create.tcl INSTALL 644 line.tcl INSTALL 644 console.tcl INSTALL 644 checkout_op.tcl INSTALL 644 remote_add.tcl INSTALL 644 browser.tcl INSTALL 644 option.tcl INSTALL 644 merge.tcl INSTALL 644 index.tcl INSTALL 644 branch_checkout.tcl INSTALL 644 branch.tcl INSTALL 644 chord.tcl INSTALL 644 diff.tcl INSTALL 644 remote.tcl INSTALL 644 sshkey.tcl INSTALL 644 logo.tcl INSTALL 644 choose_font.tcl INSTALL 644 transport.tcl INSTALL 644 encoding.tcl INSTALL 644 mergetool.tcl INSTALL 644 tools.tcl INSTALL 644 tools_dlg.tcl INSTALL 644 status_bar.tcl INSTALL 644 search.tcl INSTALL 644 shortcut.tcl INSTALL 644 branch_rename.tcl INSTALL 644 class.tcl INSTALL 644 remote_branch_delete.tcl INSTALL 644 choose_repository.tcl INSTALL 644 about.tcl INSTALL 644 blame.tcl INSTALL 644 win32.tcl INSTALL 644 choose_rev.tcl INSTALL 644 commit.tcl INSTALL 644 branch_delete.tcl INSTALL 644 date.tcl INSTALL 644 database.tcl INSTALL 644 error.tcl INSTALL 644 git-gui.ico INSTALL 644 win32_shortcut.js DEST /usr/local/share/git-gui/lib/msgs INSTALL 644 nb.msg INSTALL 644 pt_br.msg INSTALL 644 bg.msg INSTALL 644 zh_cn.msg INSTALL 644 ja.msg INSTALL 644 it.msg INSTALL 644 de.msg INSTALL 644 pt_pt.msg INSTALL 644 fr.msg INSTALL 644 ru.msg INSTALL 644 el.msg INSTALL 644 hu.msg INSTALL 644 vi.msg INSTALL 644 sv.msg make[1]: Leaving directory `/usr/local/src/git-2.30.0/git-gui' bindir=$(cd '/usr/local/bin' && pwd) && \ execdir=$(cd '/usr/local/libexec/git-core' && pwd) && \ destdir_from_execdir_SQ=$(echo 'libexec/git-core' | sed -e 's|[^/][^/]*|..|g') && \ { test "$bindir/" = "$execdir/" || \ for p in git git-shell git-cvsserver; do \ rm -f "$execdir/$p" && \ test -n "" && \ ln -s "$destdir_from_execdir_SQ/bin/$p" "$execdir/$p" || \ { test -z "" && \ ln "$bindir/$p" "$execdir/$p" 2>/dev/null || \ cp "$bindir/$p" "$execdir/$p" || exit; } \ done; \ } && \ for p in git-receive-pack git-upload-archive git-upload-pack; do \ rm -f "$bindir/$p" && \ test -n "" && \ ln -s "git" "$bindir/$p" || \ { test -z "" && \ ln "$bindir/git" "$bindir/$p" 2>/dev/null || \ ln -s "git" "$bindir/$p" 2>/dev/null || \ cp "$bindir/git" "$bindir/$p" || exit; }; \ done && \ for p in git-add git-am git-annotate git-apply git-archive git-bisect--helper git-blame git-branch git-bugreport git-bundle git-cat-file git-check-attr git-check-ignore git-check-mailmap git-check-ref-format git-checkout-index git-checkout git-clean git-clone git-column git-commit-graph git-commit-tree git-commit git-config git-count-objects git-credential-cache--daemon git-credential-cache git-credential-store git-credential git-describe git-diff-files git-diff-index git-diff-tree git-diff git-difftool git-env--helper git-fast-export git-fast-import git-fetch-pack git-fetch git-fmt-merge-msg git-for-each-ref git-for-each-repo git-fsck git-gc git-get-tar-commit-id git-grep git-hash-object git-help git-index-pack git-init-db git-interpret-trailers git-log git-ls-files git-ls-remote git-ls-tree git-mailinfo git-mailsplit git-merge-base git-merge-file git-merge-index git-merge-ours git-merge-recursive git-merge-tree git-merge git-mktag git-mktree git-multi-pack-index git-mv git-name-rev git-notes git-pack-objects git-pack-redundant git-pack-refs git-patch-id git-prune-packed git-prune git-pull git-push git-range-diff git-read-tree git-rebase git-receive-pack git-reflog git-remote-ext git-remote-fd git-remote git-repack git-replace git-rerere git-reset git-rev-list git-rev-parse git-revert git-rm git-send-pack git-shortlog git-show-branch git-show-index git-show-ref git-sparse-checkout git-stash git-stripspace git-submodule--helper git-symbolic-ref git-tag git-unpack-file git-unpack-objects git-update-index git-update-ref git-update-server-info git-upload-archive git-upload-pack git-var git-verify-commit git-verify-pack git-verify-tag git-worktree git-write-tree git-cherry git-cherry-pick git-format-patch git-fsck-objects git-init git-maintenance git-merge-subtree git-restore git-show git-stage git-status git-switch git-whatchanged; do \ rm -f "$execdir/$p" && \ if test -z ""; \ then \ test -n "" && \ ln -s "$destdir_from_execdir_SQ/bin/git" "$execdir/$p" || \ { test -z "" && \ ln "$execdir/git" "$execdir/$p" 2>/dev/null || \ ln -s "git" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git" "$execdir/$p" || exit; }; \ fi \ done && \ remote_curl_aliases="git-remote-https git-remote-ftp git-remote-ftps" && \ for p in $remote_curl_aliases; do \ rm -f "$execdir/$p" && \ test -n "" && \ ln -s "git-remote-http" "$execdir/$p" || \ { test -z "" && \ ln "$execdir/git-remote-http" "$execdir/$p" 2>/dev/null || \ ln -s "git-remote-http" "$execdir/$p" 2>/dev/null || \ cp "$execdir/git-remote-http" "$execdir/$p" || exit; } \ done && \ ./check_bindir "z$bindir" "z$execdir" "$bindir/git-add"
07-15
### Git 编译安装时 `SUBDIR git-gui gitk-git templates` 安装步骤说明 在 Git 源码编译过程中,`SUBDIR git-gui gitk-git templates` 是构建流程中的一个关键步骤,涉及多个子模块的安装与配置。该过程主要用于构建 Git 的图形化界面组件和模板文件。 #### 1. `git-gui` 子目录安装 `git-gui` 是 Git 提供的一个基于 Tcl/Tk 的图形界面工具,用于简化提交、分支管理等常见操作。在执行 `make` 命令时,系统会进入 `git-gui` 目录并调用其内部的构建脚本以生成可执行文件。如果在此阶段出现错误提示如 `MSGFMT po/de.msg make[1]: *** [po/de.msg] Error 127`,则表示缺少本地化支持所需的 `msgfmt` 工具,需先安装 `gettext` 包;此外,若报错提示缺少 Tk 库,则应通过 `yum install tk` 来补全依赖[^1]。 #### 2. `gitk-git` 子目录安装 `gitk` 是 Git 自带的图形化历史查看器,使用 Tcl/Tk 编写,能够展示提交历史、差异对比等功能。在编译期间,该部分负责构建 `gitk` 可执行文件及其相关资源文件。由于它依赖于 Tcl 和 Tk 图形库,因此必须确保这些库已正确安装在系统中。如果环境未满足依赖条件,可能导致编译失败或功能缺失。 #### 3. `templates` 安装 `templates` 目录包含 Git 在初始化仓库(`git init`)时使用的模板文件。这些模板定义了默认的 `.git` 结构,包括 hooks、description、config 等基础文件。在安装过程中,这些模板会被复制到 Git 的共享数据目录中,通常位于 `/usr/local/share/git-core/templates` 或类似路径下。此步骤确保创建的 Git 仓库具备标准化的初始结构。 #### 4. 构建输出信息解读 在编译安装过程中,用户可能会看到大量 `install` 命令的输出,例如: ```bash install -d /usr/local/bin install git /usr/local/bin/ ``` 这些命令表明正在将 Git 的可执行文件、文档及其他资源安装到目标路径。每个 `install` 调用都对应着一个具体的文件或目录的复制操作,最终完成整个 Git 套件的部署。 #### 5. 验证安装结果 为了确认 Git 是否成功安装,可以通过以下命令检查版本信息: ```bash git --version ``` 若输出类似 `git version 2.xx.x`,则表示安装已完成且 Git 可正常运行。同时,可以尝试启动 `git gui` 或 `gitk` 来验证图形化组件是否可用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值