有没有一个简单的命令可以将分支转换为标签?
我即将完成将“哑快照”转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给出的答案基本都是正确的。
由于标签和分支只是对象的名称,因此有一种更简单的方法,无需触及当前工作区域:
或者甚至可以在不触及本地存储库的情况下对远程服务器执行此操作:
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:
Or even do it to remote server without touching local repository at all:
这些分支是否有单独的开发? (您链接到的帖子似乎在这些分支上没有开发)
如果没有开发,您可以:
git checkoutbranchName
。git checkout master
。如果分支上有开发,也可以这样做,但您需要使用
-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:
git checkout branchName
.git tag tagName
.git checkout master
.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.根据安迪的回答,我创建了一个别名,也可以用于同样的事情:
用法
如果你想将分支 bug-2483 转换为标签(当你的主分支是 master 时),请写入:
更新 1
进行了更改以反映 kauppi 提出的解决方案。
Per Andy's answer, I've made an alias that can also be used for the same thing:
Usage
If you want to convert branch bug-2483 to a tag (while your main branch is master) write:
UPDATE 1
Changed to reflect the solution proposed by kauppi.