Is there one Git command to get the current tag, branch and commit?

I am currently using a collection of three commands to retrieve the current tag, branch and date, and SHA1 of the last commit.

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 

Honestly, I'm fine with that. But I use these commands from Maven and everyone who has used Maven before knows how many things like external commands inflate POM. I just want to reduce the size of pom.xml and slightly reduce the runtime.

+43
git command-line maven mavanagaiata
May 19 '10 at 8:00
source share
6 answers

I created the Maven plugin just for this purpose, which really meets my needs (in fact, it now surpasses them).

It is called open source Mavanagaiata and is available from Maven Central.

+6
May 13 '11 at 7:28 a.m.
source share
  • git log extremely flexible with many options. You may not be able to reproduce the exact output of the three above commands, but you can come close enough to achieve the desired effect.

    For example:

     git log --pretty=format:'%ad %h %d' --abbrev-commit --date=short -1 

    returns the date, SHA-1, and symbolic links (including tags) of the last (HEAD) commit:

    2010-05-20 45bd5e7 (HEAD, origin / master)

    After which, presumably, sed and / or awk or, possibly, Maven-native methods can fine tune / polish. Please note that a specific tag is associated with a specific commit, so if it was three months before the HEAD that was tagged, for example, "v1.0.0", you will not see "v1.0.0" above.

  • The simplest single command to provide succint commit descriptions:

     git describe 

    which writes out the last applicable tag, the number of commits since the marked commit and SHA1:

    v3.3.0-46-g71a77dc

  • I am not completely familiar with Maven and don’t know how easy / difficult it is to start external processes, so I’m not sure if any of the following actions is anyway, but I thought I could mention this just in case.

    For the specific purpose that you are describing, i.e. creating tags, in the autoconf / automake system, I actually use something like:

     BUILDTAG="`git symbolic-ref HEAD 2> /dev/null | cut -b 12-`-`git log --pretty=format:\"%h\" -1`" 

    which creates something suitable to snap to the end of the program path:

    master c5282ff

    A more detailed description suitable for inclusion as a comment or print identifier:

     BUILDDESC="$(git symbolic-ref HEAD 2> /dev/null | cut -b 12-)-$(git log --pretty=format:'%h, %ad' -1)" 

    creates something like:

    master-c5282ff, Fri 12 Mar. 22:19:51 2010 -0600

I think playing with git log , perhaps in combination with tools / text processing methods, will give you what you want.

+53
May 20 '10 at a.m.
source share

I don't use Maven, so I don't know how you call these commands, but adding custom commands to git is pretty trivial.

Create a script called git -tbc that looks like this:

 #!/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, now you can call "git tbc". Is this what you were looking for?

+10
May 19 '10 at 8:11
source share

My "repo" for such things is always bash_completion . So, tab tab is the way that bash becomes a productive tool, so where do all magic things come from?

there is a directory /etc/bash_completion.d/ where extensions remain to complete bash. there should be a git executable, open it and look for something like get_refs (). If you give him a check, you will find that git to describe and git for-each-ref are your friends, try some examples:

General repo:

 $ cd /your/git/repo; git branch -a master blaster * brunch lunch remotes/origin/master remotes/origin/develop remotes/github/master 

What is my tagged branch?

 $ git describe --contains --all HEAD brunch 

What are my remotes?

 $ git remote origin github 

What are the branches on the 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 

... more syntax output is 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 these commands, there is much more !!

+4
Sep 16 2018-11-11T00
source share

I found this Maven plugin: https://github.com/alx3apps/jgit-buildnumber , which seems like a good replacement for the buildnumber-maven- plugin for git projects . In addition, it is available from the central Maven repositories.

It works great in Maven 3 . For Maven 2 multi-module projects, simply add the following line to the properties section of your parent pom:

 <session.executionRootDirectory>${basedir}</session.executionRootDirectory> 
+3
Nov 20
source share

The HEAD commit ID is displayed here, as well as any branches or tags that are also in HEAD.

 git reflog --decorate -1 

Output Example:

 484c27b (HEAD, tag: deployment-2014-07-30-2359, master, origin/master) HEAD@{0}: 484c27b878ca5ab45185267f4a6b56f8f8d39892: updating HEAD 
+1
Aug 25 '14 at 13:30
source share



All Articles