Git log between tags

Git

Git Problem Overview


I'm trying to output the log between two tagged commits.

mbell@cheetah [12:07:22] [/var/www/html/brone] [dev]
-> % git tag 
6.x-0.1
6.x-1.0-beta1
6.x-1.0-beta2
6.x-1.0-beta3
6.x-1.0-beta4
6.x-1.0-beta5
6.x-1.0-beta6
6.x-1.0-beta7
6.x-1.0-beta8
6.x-1.0-beta9

If I then do:

git log 6.x-1.0-beta8 6.x-1.0-beta9 > ~/gitlogbrone.txt

It outputs all the commits since the start of the repo which isn't what I want. I've read through the git log manual but it's not helping much.

Git Solutions


Solution 1 - Git

You need an ellipsis to indicate a range. Try git log tag1..tag2.

Solution 2 - Git

I use this to get the commits between the last 2 tags:

git log --pretty=format:%s `git tag --sort=-committerdate | head -1`...`git tag --sort=-committerdate | head -2 | awk '{split($0, tags, "\n")} END {print tags[1]}'` > change_log.txt

Solution 3 - Git

Thanks to @Noufal Ibrahim for his answer.

I was committing a file and creating a new tag. But before doing that, my need was to list and format all of the commits after the last tag created. Here is what I did that time:

$ git log <last_tag>..

> Notice double dot (..) at the end

Solution 4 - Git

An expansion on the answers from @Yurii and @wilmol for those interested in generating a release notes file and wanting a script that is readable and easily modified.

export VERSION=$(git tag --sort=-committerdate | head -1)
export PREVIOUS_VERSION=$(git tag --sort=-committerdate | head -2 | awk '{split($0, tags, "\n")} END {print tags[1]}')
export CHANGES=$(git log --pretty="- %s" $VERSION...$PREVIOUS_VERSION)
printf "# 🎁 Release notes (\`$VERSION\`)\n\n## Changes\n$CHANGES\n\n## Metadata\n\`\`\`\nThis version -------- $VERSION\nPrevious version ---- $PREVIOUS_VERSION\nTotal commits ------- $(echo "$CHANGES" | wc -l)\n\`\`\`\n" > release_notes.md

The above script generates a markdown file at release_notes.md that looks like this:


 Release notes (14.2)

Changes

  • ABCDEFGHIJKLMNOP
  • ABCDEFGHIJKLMNOP
  • ABCDEFGHIJKLMNOP
  • ABCDEFGHIJKLMNOP
  • ABCDEFGHIJKLMNOP

Metadata

This version -------- 14.2
Previous version ---- 14.1
Total commits ------- 5

I like this approach for a few reasons:

  • If there is a tag between the two tags I'm interested in, I can manually set $VERSION and $PREVIOUS_VERSION before running the last two lines.

  • With a few tweaks, I can sort, filter, and modify $CHANGES to meet my specific needs.

Solution 5 - Git

A bit optimised solution from @wilmol

git log --pretty=oneline `git tag --sort=-committerdate | head -1`...`git tag --sort=-committerdate | head -2 | tail -1`

I prefer to use in scripts for the release notes the following code:

git log --pretty=oneline `git tag --sort=-committerdate | head -1`...`git tag --sort=-committerdate | head -2 | tail -1` |cut -d " " -f 2- |grep -v "Merge pull request"

This one give a clear commits history between two last tags without git has and merge lines.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondigitalView Question on Stackoverflow
Solution 1 - GitNoufal IbrahimView Answer on Stackoverflow
Solution 2 - GitwilmolView Answer on Stackoverflow
Solution 3 - GitShudipta SharmaView Answer on Stackoverflow
Solution 4 - GitMathildaView Answer on Stackoverflow
Solution 5 - GitYuriiView Answer on Stackoverflow