使用 git symbolic-ref <alias-branch> <targetbranch>
命令:
git symbolic-ref refs/heads/trunk refs/heads/master
git symbolic-ref refs/heads/master refs/heads/main
但 "当前分支" (git branch --show-current
) 会是 "错误的" (仍然显示目标分支而不是您的别名分支):
在 Git 2.39(2022年第4季度)之前,检出一个指向另一个分支的符号引用的“分支”后,“git symbolic-ref HEAD
”(手册)报告底层分支,而不是用户作为参数传递的符号引用。
该命令学习了"--no-recurse
"选项,以在解除引用symbolic-ref
一次后停止。
请参见commit b77e3bd(2022年10月7日),作者为Junio C Hamano(gitster
)。
(由Junio C Hamano -- gitster
--于commit 4a48c7d合并,2022年10月21日)
Suppose you are managing many maintenance tracks in your project, and some of the more recent ones are maint-2.36
and maint-2.37
.
Further imagine that your project recently tagged the official 2.38 release, which means you would need to start maint-2.38
track soon, by doing:
$ git checkout -b maint-2.38 v2.38.0^0
$ git branch --list 'maint-2.3[6-9]'
* maint-2.38
maint-2.36
maint-2.37
So far, so good.
But it also is reasonable to want not to have to worry about which maintenance track is the latest, by pointing a more generic-sounding 'maint' branch at it, by doing:
$ git symbolic-ref refs/heads/maint refs/heads/maint-2.38
which would allow you to say "whichever it is, check out the latest maintenance track", by doing:
$ git checkout maint
$ git branch --show-current
maint-2.38
It is arguably better to say that we are on 'maint-2.38' rather than on 'maint', and git merge/pull would record into maint-2.38 and not into maint, so I think what we have is a good behaviour.
One thing that is slightly irritating, however, is that I do not think there is a good way (other than "cat .git/HEAD") to learn that you checked out 'maint' to get into that state.
Just like the output of "git branch --show-current
"(man) shows above, "git symbolic-ref
"(man) HEAD would report 'refs/heads/maint-2.38', bypassing the intermediate symbolic ref at 'refs/heads/maint' that is pointed at by HEAD.
The internal resolve_ref()
API already has the necessary support for stopping after resolving a single level of a symbolic-ref, and we can expose it by adding a "--[no-]recurse" option to the command.
git symbolic-ref
现在在其手册页面中包含:
'git symbolic-ref' [-q] [
--recurse
--no-recurse
When showing the value of as a symbolic ref, if
refers to another symbolic ref, follow such a chain
of symbolic refs until the result no longer points at a
symbolic ref (--recurse
, which is the default).
--no-recurse
stops after dereferencing only a single level
of symbolic ref.
ln -s
不一样,如果你把它们放错了位置,git 会欣然(且悄无声息地)用一个指向不存在东西的符号引用覆盖真实分支的 HEAD 引用(此时你最好希望能轻松恢复该分支的正确提交哈希值)。 - philsgit branch -d
来删除符号引用。即使进行这种操作,它也会被取消引用,所以你实际上会删除源分支,但留下引用(如果你当前正在使用该分支,则甚至允许你这样做)。你必须使用git symbolic-ref --delete
(如果你使用的是旧版本,则必须手动删除文件)。 - philsgit symbolic-ref refs/heads/master refs/heads/main
— 如果您在现代存储库中使用老派的习惯感到不舒服 :) - kolypto