是否有单个 Git 命令来获取当前标签、分支和提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2863756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a single Git command to get the current tag, branch and commit?
提问by Koraktor
I'm currently using a collection of three commands to get the current tag, branch and the date and SHA1 of the most recent commit.
我目前正在使用三个命令的集合来获取最近提交的当前标签、分支以及日期和 SHA1。
git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD
Which will output something like:
这将输出如下内容:
1.2.3-5-gdeadbeef
deadbeef3b8d90071c24f51ac8f26ce97a72727b
Wed, 19 May 2010 09:12:34 +0200
master
To be honest, I'm totally fine with this. But I'm using these commands from Maven and anyone who'd used Maven before, knows how much things like external commands bloat the POM. I just want to slim down my pom.xml and maybe reduce execution time a bit.
老实说,我对此完全没问题。但是我正在使用来自 Maven 的这些命令,并且任何以前使用过 Maven 的人都知道像外部命令这样的东西会使 POM 膨胀多少。我只是想精简我的 pom.xml 并可能减少一点执行时间。
采纳答案by Koraktor
I created a Maven plugin for exactly this purpose, which really fits my needs (in fact it exceeds them by now).
我正是为此目的创建了一个 Maven 插件,它确实符合我的需求(实际上它现在已经超过了它们)。
It's called Mavanagaiata, it's open-source and available from Maven Central.
它被称为Mavanagaiata,它是开源的,可从 Maven Central 获得。
回答by Jeet
git log
is extremely flexible, with lots and lots of options. You might not be able to reproduce the exact output of the three commands above, but you might come close enough to achieve the effect you need.For example:
git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1
produces the date, SHA-1 and symbolic references (including tags) of the latest (HEAD) commit:
2010-05-20 45bd5e7 (HEAD, origin/master)
After which, presumably,
sed
and/orawk
or maybe Maven-native methods can do the fine-tuning/polishing. Note that a particular tag is associated with a particular commit, so if it was three commits prior to HEAD that was tagged with, for example, "v1.0.0", you are not going to see "v1.0.0" showing up with the above.A simpler single command to provide a succint description of a commit is:
git describe
which writes out the latest applicable tag, the number of commits since the tagged commit, and the SHA1:
v3.3.0-46-g71a77dc
I am not at all familiar with Maven, and have no idea how easy/difficult it is to run external processes, so am unsure whether any of the following help in any way, but I thought I might mention it just in case.
For the exact purpose that you describe, i.e. tagging builds, in an autoconf/automake framework, I actually use something like:
BUILDTAG="`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"
which produces something suitable for tacking onto the end of a program path:
master-c5282ff
A more extended description, suitable for including as a comment or a printed identifier:
BUILDDESC="$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:'%h, %ad' -1)"
produces something like:
master-c5282ff, Fri Mar 12 22:19:51 2010 -0600
git log
非常灵活,有很多很多选择。您可能无法重现上述三个命令的确切输出,但您可能已经足够接近以达到所需的效果。例如:
git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1
生成最新 (HEAD) 提交的日期、SHA-1 和符号引用(包括标签):
2010-05-20 45bd5e7 (HEAD, origin/master)
之后,大概
sed
和/或awk
Maven 本地方法可以进行微调/抛光。请注意,特定标签与特定提交相关联,因此如果在 HEAD 之前的三个提交被标记为“v1.0.0”,则您将不会看到“v1.0.0”与以上。提供对提交的简洁描述的更简单的单个命令是:
git describe
它写出最新的适用标签、自标记提交以来的提交次数以及 SHA1:
v3.3.0-46-g71a77dc
我对 Maven 一点都不熟悉,也不知道运行外部进程有多么容易/困难,所以我不确定以下是否有任何帮助,但我想我可能会提到它以防万一。
对于您描述的确切目的,即在 autoconf/automake 框架中标记构建,我实际上使用了类似的东西:
BUILDTAG="`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`"
这会产生一些适合添加到程序路径末尾的东西:
master-c5282ff
更扩展的描述,适合作为注释或打印的标识符包括在内:
BUILDDESC="$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:'%h, %ad' -1)"
产生类似的东西:
master-c5282ff,2010 年 3 月 12 日星期五 22:19:51 -0600
I think playing around with git log
, possibly in conjunction with text processing tools/methods will get you what you want.
我认为玩弄git log
, 可能与文本处理工具/方法结合使用会得到你想要的。
回答by Peter Farmer
I don't use Maven, so I don't know how you are calling these commands, but adding custom commands to git is fairly trivial.
我不使用 Maven,所以我不知道你是如何调用这些命令的,但是向 git 添加自定义命令是相当简单的。
Create a script called git-tbc that looks like this:
创建一个名为 git-tbc 的脚本,如下所示:
#!/bin/bash
git describe --always --tag
git log -1 --format="%H%n%aD"
git rev-parse --abbrev-ref HEAD
Make sure git-tbc is in your PATH, you can now call "git tbc". Is this what you were looking for?
确保 git-tbc 在您的 PATH 中,您现在可以调用“git tbc”。这就是你要找的吗?
回答by albfan
My "repo" for things like that is always bash_completion. Ok, "tab tab" is the way bash becomes a productive tool, so, where all that magic stuff come from?
我对此类事情的“回购”始终是bash_completion。好的,“tab tab”是 bash 成为高效工具的方式,那么,所有这些神奇的东西来自哪里?
there is a /etc/bash_completion.d/directory where extensions for bash completion are left. there must be a git file executable, open it and seek for something like get_refs(). If you give it a check you will find that git describeand git for-each-refare your friends, let's try some examples:
有一个/etc/bash_completion.d/目录,其中保留了 bash 完成的扩展。必须有一个 git 文件可执行文件,打开它并寻找类似 get_refs() 的东西。如果你检查一下,你会发现git describe和git for-each-ref是你的朋友,让我们尝试一些例子:
A common repo:
一个常见的回购:
$ cd /your/git/repo; git branch -a
master
blaster
* brunch
lunch
remotes/origin/master
remotes/origin/develop
remotes/github/master
Which is my checked branch?
哪个是我检查的分支?
$ git describe --contains --all HEAD
brunch
What are my remotes?
我的遥控器是什么?
$ git remote
origin
github
What are the branches on remotes?
遥控器上的分支是什么?
$ git for-each-ref --format="%(refname:short)" refs/remotes
origin/master
origin/develop
github/master
What are my local branches?
我的本地分支机构是什么?
$ git branch
master
blaster
* brunch
lunch
...a more parseable branches output?
...更可解析的分支输出?
$ git for-each-ref --format="%(refname:short)" refs/heads
master
blaster
brunch
lunch
What about tags?
标签呢?
$ git for-each-ref --format="%(refname:short)" refs/heads refs/remotes refs/tags
master
blaster
brunch
lunch
origin/master
origin/develop
github/master
release-0_1
release-0_2
release-1_0
check the "man pages"for this commands, there's much more inside!.
检查此命令的“手册页”,里面还有更多!
回答by edrabc
I have found this Maven Plugin: https://github.com/alx3apps/jgit-buildnumber, which seems a good replacementof buildnumber-maven-pluginfor git projects. Furthermore it is available from Maven Central Repositories.
我发现这个Maven插件:https://github.com/alx3apps/jgit-buildnumber,这似乎是一个很好的替代的buildnumber - Maven的插件为git的项目。此外,它可以从 Maven 中央存储库获得。
It works nicely in Maven 3. For Maven 2 multi-module projects, simply add the following line in the properties section of your parent pom:
它在Maven 3 中运行良好。对于 Maven 2 多模块项目,只需在父 pom 的属性部分添加以下行:
<session.executionRootDirectory>${basedir}</session.executionRootDirectory>
回答by webmat
This displays the commit id of HEAD, as well as any branches or any tags that also happen to be exactly at HEAD.
这将显示 HEAD 的提交 ID,以及恰好位于 HEAD 的任何分支或任何标签。
git reflog --decorate -1
Sample output:
示例输出:
484c27b (HEAD, tag: deployment-2014-07-30-2359, master, origin/master) HEAD@{0}: 484c27b878ca5ab45185267f4a6b56f8f8d39892: updating HEAD