rj93
rj93

Reputation: 573

How to filter git log by tag?

I'm attempting to filter git log by tags. I have tried using --tags="3.5.0", as per the documentation, but it doesn't work as it returns all commits, both before and after the tag.

How am I suppose to use this?

Upvotes: 7

Views: 10066

Answers (3)

328d95
328d95

Reputation: 3

I had a similar problem and I ended up here. My solution was to extend the other answers to get:

git log <commitId>..HEAD --format='%h %D' | grep '<projectRegex>' | awk '{print $1}' | xargs git show --no-patch

which will show you a git log style history for all the commits between and HEAD whose tag (or commit hash if you are unlucky -- you can use a space prefixed regex to get around this as tags cannot contain whitespace -- eg \sprojectRegex.*$) matches .

Explanation pipe by pipe

  1. Get commit hashes and tags between your commitId and HEAD.
  2. Filter by tags as they are now represented as strings.
  3. Extract the matching commit hashes.
  4. Show only the commit messages hash by hash using git show.

Upvotes: 0

Bill Jetzer
Bill Jetzer

Reputation: 1115

I ran into (I think) this same issue with my own project. In my case, I have several tag "categories", each with its own specific versioning, and when I get successful build, I want to find the most recent tag, extract the version, increment it, and tag the current commit with that new version identifier.

I'm not 100% clear on your goal, but here is some code that will spit out the most recent commit id whose tag matches the specified regular expression:

tagRegex='foo'; # fill in your tag pattern here
git log --format='%h %D' | sed -n "/ tag: .*$tagRegex/ {s/ .*//; p;q;}";

My tags are of the form category-v1.2.3, and I'm interested in the semantic version attached to the tag rather than the commit ID, so I use this:

git log --format='%D' | sed -n "/^tag: .*$tagRegex/ {s/.*-v//; p;q;}";

Upvotes: 1

SVSchmidt
SVSchmidt

Reputation: 6527

Filter may include anything. For example, git log 3.5.0 will give you all commits up to that tag. git log 3.4.0..3.5.0 will output all commits between those tags. If you just want to see a commit for a tag, use git show 3.5.0. If you must see all tags and their respective commit, something like git tag -l | xargs git show is thinkable.

Also, don't forget to add --decorate to git log to actually see tags associated with commits.

Upvotes: 6

Related Questions