简体   繁体   English

Git:获取HEAD指向的分支的最简单方法

[英]Git: Easiest way to get the branch to which HEAD points

I need a git command that returns the name of the branch to which HEAD points. 我需要一个git命令来返回HEAD指向的分支的名称。 Ie, when I am on master , it should return master . 也就是说,当我在master ,它应该归还master In detached HEAD state, it can return either nothing or something that I will not confuse with a branch name. 在分离的HEAD状态下,它可以返回任何内容或者不会与分支名称混淆的内容。 What is the easiest command to achieve this? 实现这一目标的最简单命令是什么?

(Sorry, the question seems to be rather trivial and was probably asked a gazillion of times, but searching for things like "get branch to which HEAD points" didn't give me a satisfying result.) (对不起,这个问题似乎相当微不足道,并且可能被问了很多次,但是搜索“得到分支到哪个HEAD点”这样的事情并没有给我一个令人满意的结果。)

You are probaply looking for: 您正在寻找:

  • git symbolic-ref --short HEAD
  • git rev-parse --abbrev-ref HEAD

But it's also possible to manually read .git/HEAD 但也可以手动读取.git/HEAD

Run this command to add an alias to git config. 运行此命令为git config添加别名。

git config --global alias.current "!git branch|grep '*'"

git current # should return the name of current branch.

Note: You can use git symbolic-ref --short HEAD too instead of grep to find the current branch as @MrTux suggested. 注意:您也可以使用git symbolic-ref --short HEAD而不是grep来查找当前分支,如@MrTux建议的那样。

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

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