简体   繁体   English

git plumbing告诉签出的分支

[英]git plumbing to tell the checked out branch

Scripting git, I need to find out the checked-out branch name. 脚本git,我需要找出签出的分支名称。 So far it seems the only "reliable" way to do that is with git branch | sed -n '/^\\* /s///p' 到目前为止,似乎唯一“可靠”的方法是使用git branch | sed -n '/^\\* /s///p' git branch | sed -n '/^\\* /s///p' . git branch | sed -n '/^\\* /s///p' (Scare quotes because of things like color.branch or column.branch in .gitconfig; it's not reliable at all.) The only other thing I found is git name-rev , but that seems to return the first (sorted by names) branch that points to HEAD: (因为column.branch中的color.branchcolumn.branch类的东西引用了引号;它根本不可靠。)我发现的唯一另一件事是git name-rev ,但这似乎返回第一个(按名称排序)分支这指向HEAD:

> git checkout master
> git checkout -b another
> git checkout master
> git name-rev HEAD
HEAD another

Is there something better than sed -n '\\#^ref: refs/heads/#s###p' .git/HEAD to figure out the checked out branch? 有没有什么比sed -n '\\#^ref: refs/heads/#s###p' .git/HEAD更好地找出签出的分支?

Just output the branch you are on with: 只需输出您所在的分支:

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

There should be no trouble also if you have more than one branches, and if you aren't on any branch it just gives you HEAD 如果你有多个分支,也应该没有问题,如果你不在任何分支上,它只会给你HEAD

Here's a little git invocation I've used in a couple of scripts, that either gives back refs/heads/branchname , or if you're not on a branch, it gives the SHA of your detached HEAD: 这是我在几个脚本中使用的一个小git调用,要么返回refs/heads/branchname ,要么你不在分支上,它给出了你的分离HEAD的SHA:

cur_branch=$(git symbolic-ref HEAD 2>> /dev/null || git rev-parse HEAD)

Removing the refs/heads/ prefix should be pretty simple, if you need it... 删除refs/heads/前缀应该非常简单,如果你需要它...

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

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