Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

like image 636
Koraktor Avatar asked May 19 '10 08:05

Koraktor


People also ask

How do I find my current tag?

Find Latest Git Tag Available In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.

How do I pull a tag in git?

In order to checkout a Git tag, use the “git checkout” command and specify the tagname as well as the branch to be checked out. Note that you will have to make sure that you have the latest tag list from your remote repository.

How do you find the branch of a commit?

It is based on "Find merge commit which include a specific commit". Find when a commit was merged into one or more branches. Find the merge commit that brought COMMIT into the specified BRANCH(es). Specifically, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.


2 Answers

  1. 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/or awk 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.

  2. 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

  3. 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

I think playing around with git log, possibly in conjunction with text processing tools/methods will get you what you want.

like image 93
Jeet Avatar answered Sep 20 '22 04:09

Jeet


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.

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

like image 21
Peter Farmer Avatar answered Sep 20 '22 04:09

Peter Farmer