git:有类似每个分支标签的东西吗?

发布于 2024-11-02 05:22:08 字数 81 浏览 5 评论 0原文

我有一些历史重写要做,为此我想暂时保持我原来的树完好无损。然而,重写的树也应该复制以前使用的标签。是否有比在分支名称前面添加标签名称更少的手动选项?

I have some history rewriting to do for which I'd like to keep my original tree intact for now. The rewritten tree should however copy the tags previously used as well. Is there any less manual option than e.g. prepending tag names with the branch name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯然 2024-11-09 05:22:08

不,git 中没有像每个分支标签那样的东西。所有分支和标签都只是 Git 中的引用; ref 只是指向修订历史记录中特定修订的名称。例如,如果您有 develmaster 分支以及 v1.0v2.0 标记,则引用看起来像这样:

refs/heads/devel ->  *
                    / \
                   *   * <- refs/heads/master
                   |   |
                   *   *
                    \ /
                     * <- refs/tags/v2.0
                     |
                     *
                     |
                     * <- refs/tags/v1.0
                     |
                     *

正如您所看到的,没有任何东西将这些标签绑定到任何分支;事实上,所有这些标签都包含在 masterdevel 分支中。通过查看您的 .git 存储库,您可以发现标签实际上没有比这更多的结构了;它只是一个包含 SHA-1 的文件,引用 .git/refs 中的提交,或者 .git/packed-refs 中的一行(标签通常位于 < code>packed-refs 因为它们不经常更改,而分支通常是 git/refs 中的单独文件。

因此,如果您想重写历史记录并保留旧标签,则必须重写标签名称。正如 sehe 指出的,这是使用 git filter-branch --tag-name-filter

No, there is nothing like a per-branch tag in git. All branches and tags are just kinds of refs in Git; a ref is just a name that points to a particular revision in the revision history. For instance, if you have devel and master branches, and v1.0 and v2.0 tags, the references would look something like this:

refs/heads/devel ->  *
                    / \
                   *   * <- refs/heads/master
                   |   |
                   *   *
                    \ /
                     * <- refs/tags/v2.0
                     |
                     *
                     |
                     * <- refs/tags/v1.0
                     |
                     *

As you can see, there's nothing tying those tags to any branches; in fact, all of those tags are contained in both the master and devel branches. By looking inside your .git repo, you can see that there is really no more structure to a tag than that; it is just a file containing a SHA-1 referencing a commit within .git/refs, or a line in .git/packed-refs (tags will frequently be in packed-refs because they don't change often, while branches will usually be separate files within git/refs).

So if you want to rewrite history, and preserve the old tags, you will have to rewrite your tag names. As sehe points out, this is done using git filter-branch --tag-name-filter.

荆棘i 2024-11-09 05:22:08

您可以将标签命名为以您希望标签(某种)所属的分支命名的目录,并且不必是手动的:

git-thisBranch() { git symbolic-ref --short HEAD; } 
git-localTag() { 
   local name="$1"; shift;
   git tag $(git-thisBranch)/"$name" "$@";
}

You can namespace a tag into a directory named after the branch you want the tag to (kind of) belong to, and it doesn't have to be manual:

git-thisBranch() { git symbolic-ref --short HEAD; } 
git-localTag() { 
   local name="$1"; shift;
   git tag $(git-thisBranch)/"$name" "$@";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文