有没有一个简单的命令可以将分支转换为标签?

发布于 2024-11-23 15:42:35 字数 1410 浏览 4 评论 0原文

我即将完成将“哑快照”转换为 git 的繁琐过程。这个过程进展顺利(感谢 这个重命名过程),但现在我意识到我创建的一些分支不值得一个分支,而是一个标签

由于一切仍然是本地的(从未推送到存储库),我发现这个问题(和相关的答案)比我喜欢的更麻烦,所以我想知道是否可以通过一些简单的“从分支转换到标签”命令走捷径?

有没有这么简单的命令将分支转换为标签?

(我知道我可以保持原样,但我真的很喜欢 gitk 突出显示标签的方式,帮助我轻松识别它们)。

更新:感谢@Andy 下面的回答,我设法想出了一个 shell 脚本,可以方便、轻松地完成这一切。我分享这个脚本是为了所有人的利益,并特别感谢这个伟大的社区,他们使我能够从 CVS 迁移到 git:

#!/bin/sh

BRANCHNAME=$1
TAGNAME=$2

echo "Request to convert the branch ${BRANCHNAME} to a tag with the same name accepted."
echo "Processing..."
echo " "

git show-ref --verify --quiet refs/heads/${BRANCHNAME}
# $? == 0 means local branch with <branch-name> exists. 

if [ $? == 0 ]; then
   git checkout ${BRANCHNAME}
   git tag ${BRANCHNAME}
   git checkout master
   git branch ${BRANCHNAME} -d
   echo " "
   echo "Updated list branches, sorted chronologically: "
   echo "---------------------------------------------- "
   git log --no-walk --date-order --oneline --decorate $(git rev-list --branches --no-walk) | cut -d "(" -f 2 | cut -d ")" -f 1
else
   echo "Sorry. The branch ${BRANCHNAME} does NOT seem to exist. Exiting."
fi

I am about to complete a tedious process of converting "dumb snapshots" to git. This process has been going very well (thanks to this rename process), but now I realized that some of the branches that I created, do not merit a branch but rather a tag.

Since everything is still local (never pushed to a repository), I found this question (and associated answer) somewhat more cumbersome than I prefer, so I was wondering whether I can take a shortcut via some simple "convert-from-branch-to-tag" command?

Is there such a simple command to convert a branch to a tag?

(I know I can just leave it as is, but I really like the way gitk highlights tags, helping me easily identify them).

UPDATE: Thanks to @Andy's answer below, I managed to come up with a shell script that does it all conveniently and painlessly. I am sharing this script for the benefit of all and as special thanks to this great community who made moving from CVS to git possible for me:

#!/bin/sh

BRANCHNAME=$1
TAGNAME=$2

echo "Request to convert the branch ${BRANCHNAME} to a tag with the same name accepted."
echo "Processing..."
echo " "

git show-ref --verify --quiet refs/heads/${BRANCHNAME}
# $? == 0 means local branch with <branch-name> exists. 

if [ $? == 0 ]; then
   git checkout ${BRANCHNAME}
   git tag ${BRANCHNAME}
   git checkout master
   git branch ${BRANCHNAME} -d
   echo " "
   echo "Updated list branches, sorted chronologically: "
   echo "---------------------------------------------- "
   git log --no-walk --date-order --oneline --decorate $(git rev-list --branches --no-walk) | cut -d "(" -f 2 | cut -d ")" -f 1
else
   echo "Sorry. The branch ${BRANCHNAME} does NOT seem to exist. Exiting."
fi

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

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

发布评论

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

评论(3

☆獨立☆ 2024-11-30 15:42:35

给出的答案基本都是正确的。

由于标签和分支只是对象的名称,因此有一种更简单的方法,无需触及当前工作区域:

git tag <name_for_tag> refs/heads/<branch_name> # or just git tag <name_for_tag> <branch_name>
git branch -d <branch_name>

或者甚至可以在不触及本地存储库的情况下对远程服务器执行此操作:

git push origin origin/<branch_name>:refs/tags/<tag_name>
git push origin :refs/heads/<branch_name>

The answers given are basically correct.

As tags and branches are just names for objects, there is a simpler way without touching current work area:

git tag <name_for_tag> refs/heads/<branch_name> # or just git tag <name_for_tag> <branch_name>
git branch -d <branch_name>

Or even do it to remote server without touching local repository at all:

git push origin origin/<branch_name>:refs/tags/<tag_name>
git push origin :refs/heads/<branch_name>
成熟稳重的好男人 2024-11-30 15:42:35

这些分支是否有单独的开发? (您链接到的帖子似乎在这些分支上没有开发)
如果没有开发,您可以:

  1. 签出分支git checkoutbranchName
  2. 使用 git tag tagName 对其进行标记。
  3. 切换回 master git checkout master
  4. 最后,使用 gitbranchbranchName -d 删除原始分支。

如果分支上有开发,也可以这样做,但您需要使用 -D 而不是 -d。不过,我不是 git 专业人士,所以不确定这是否是离开分支的“可接受”方式。

Was there separate development on these branches? (the post you linked to, doesn't appear to have development on those branches)
If there was no development, you could:

  1. Checkout the branch git checkout branchName.
  2. Tag it with git tag tagName.
  3. Switch back to master git checkout master.
  4. Finally, delete original branch with git branch branchName -d.

This can also be done if there was development on the branch, but you will need to use -D instead of -d. I'm not a git pro though, so not sure if that is an "acceptable" way to leave a branch.

草莓味的萝莉 2024-11-30 15:42:35

根据安迪的回答,我创建了一个别名,也可以用于同样的事情:

[alias]
branch2tag = "!sh -c 'set -e;git tag $1 refs/heads/$1;git branch -D $1' -"

用法

如果你想将分支 bug-2483 转换为标签(当你的主分支是 master 时),请写入:

git branch2tag bug-2483 master

更新 1

进行了更改以反映 kauppi 提出的解决方案。

Per Andy's answer, I've made an alias that can also be used for the same thing:

[alias]
branch2tag = "!sh -c 'set -e;git tag $1 refs/heads/$1;git branch -D $1' -"

Usage

If you want to convert branch bug-2483 to a tag (while your main branch is master) write:

git branch2tag bug-2483 master

UPDATE 1

Changed to reflect the solution proposed by kauppi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文