git控制的文件可以知道它属于哪个分支? [英] Can a git-controlled file be aware of which branch it belongs to?

查看:129
本文介绍了git控制的文件可以知道它属于哪个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Maybe it might be a strange need, but I believe it would be helpful to have something like this:

if($branch$ === "master) {
   // Special code for the master branch
} else {
   // Something special for tests. Like extended logging
}

在SVN中,这没有问题,我知道GIT没有关键字扩展系统,找到解决方案仍然很有趣。

In SVN this was no problem and I'm aware that GIT don't have a keyword expansion system, anyway, it's still interesting to find a solution for this.

推荐答案

不,所有元文件信息存储在 .git ,你可以用下面的bash命令检测当前分支(代码已修改,但最初发现 here)

No, all meta file information is stored under .git in the root directory. You can detect the current branch with the following bash command (code modified, but originally found here):

function parse_git_branch() {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
}

这篇关于git控制的文件可以知道它属于哪个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆