我想通过仅读取.git文件夹中的内容来获取当前分支或标签。
我看了很多的解决方案,它们都依赖于执行git status,git branch,git describe,或类似的东西,然后解析输出。但是如果我们不能确定有一个git二进制文件可以调用怎么办?我们不能依赖那个。
对于分支,它看起来几乎非常简单:cat .git/HEAD,但对于标签,它变得有点复杂。我git-flow用来创建我的特征分支和我的标签。当我切换到标签时,我得到:
$ git checkout tags/v0.11.2
Note: checking out 'tags/v0.11.2'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you …Run Code Online (Sandbox Code Playgroud)