mbadawi23
mbadawi23

Reputation: 1071

Git: how to have log show only tags that match a pattern?

I'm trying to list tags and hashes on a branch when the tag matches a specific pattern.

I have a command that looks like:

git log develop --tags="develop*" --remove-empty --pretty="%h %D" --decorate=short --decorate-refs=tags

I assumed the above will print for me a list of commit hashes with tags that match the pattern "develop*" (e.g. develop_001). But I still get commits with tags that don't match the pattern (e.g. feature/*). I'm including a snapshot from the output I get:

git log output

Additionally, is there a way to get rid of the commits that are not tagged?

Upvotes: 5

Views: 3444

Answers (2)

A.H.
A.H.

Reputation: 66263

If you want to list tags then don'tuse git log which lists commits. Just use something listing tags like this:

git tag --format="%(objectname:short) %(refname:short)" --merged develop  "v18*"

Here --formatgives you the hash+tag output, -merged develop restricts the output to tags being ancestors of the develop branch and v18* ist an additional filter for tags matching that pattern.

Upvotes: 5

torek
torek

Reputation: 488163

The job for git log, in general, is to walk the commit graph. There are specific cases where you tell it not to do that, and your case is one of these specific cases.

To tell git log not to walk the commit graph, use --no-walk. (Or use one of the other options that suppresses commit graph walking, but here, --no-walk is the right flag.)

Note that you probably also want not to tell it to start from develop. The general idea (which, again, you will be telling it not to do) is that you give git log some starting commit or set of commits. It locates those commits, then locates each parent commit of each of those commits, then the parents of those parents, and so on. As it works, it prints out commits that it has not yet visited, then visits their parents.

Each of the positive references (as Git calls them) that you name, such as develop or --tags="develop*" acts as a starting point. Git will show that commit, and then do the graph-walking / parent-finding.

Adding --no-walk stops the parent-finding, so that git log shows just the starting commits.

Upvotes: 1

Related Questions