简体   繁体   English

如何判断你的头是否在 git 中分离?

[英]How to tell if your head is detached in git?

How do you tell if your head has been detached in git?你如何判断你的头是否在 git 中被分离了? I have a feeling that might be the root of my problem.我有一种感觉,这可能是我问题的根源。

An alternative useful for scripts, without having to deal with $GITDIR is一种对脚本有用的替代方法,无需处理$GITDIR

git rev-parse --abbrev-ref --symbolic-full-name HEAD

This gives me HEAD for a detached head, or the branch name on the other case.这给了我一个分离头的HEAD ,或者另一种情况下的分支名称。

git branch

would show会显示

* (no branch)
  master
  develop

If you are in detached state.如果您处于分离状态。

git status is supposed to tell you everything relevant. git status应该告诉你所有相关的事情。

manually you can look content of .git/HEAD file.您可以手动查看 .git/HEAD 文件的内容。 If it has a hash, you're detached.如果它有一个散列,你就分离了。 if there is a ref like ref: refs/heads/master you're not.如果有像ref: refs/heads/master这样的ref: refs/heads/master你就不是。

For git version 2.22 and above, the command git branch --show-current can be used.对于 git 2.22 及以上版本,可以使用命令git branch --show-current In case of detached head state, the output will no nothing.在分离头状态的情况下,输出将 no nothing。

If you want to use it in a script, we can incorporate it with wc as follows:如果你想在脚本中使用它,我们可以将它与wc合并,如下所示:

git branch --show-current | wc -l

This command will return 0 in case of detached head, and 1 in case of a valid branch.此命令将在分离头的情况下返回0在有效分支的情况下返回1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM