Git log between tags
Asked Answered
P

6

110

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.

Pyotr answered 15/11, 2011 at 12:15 Comment(0)
B
187

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

Bellina answered 15/11, 2011 at 12:18 Comment(6)
... will give you the symmetric difference (only showing commits not reachable from both tags), I guess you want ..Deel
Note: tags should be in ascending order. newtag..oldtag does not workTameratamerlane
Re: "tags should be in ascending order. newtag..oldtag does not work ": I think git just translates the tags to their respective commits first, then performs the requested operation on those commits. Switching the order of the tags just reverses the before and after diffs, but should still work.Distressed
what about if the tag is the first one?Weinrich
Create tag. Run changelog creation. Delete tag. Commit changelog. Create tagEmotion
Note: git log tag1..tag2 gives changes after tag1, and not including tag1 changes.Shiite
A
23

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.

Adai answered 15/1, 2022 at 6:19 Comment(0)
H
11

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
Harlie answered 23/3, 2020 at 2:47 Comment(0)
A
8

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

Ause answered 10/5, 2021 at 10:0 Comment(0)
R
2

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.

Regardful answered 12/5, 2021 at 9:56 Comment(0)
E
0

I'm using the following shortcut

git log $(git tag |tail -1)..

Elmiraelmo answered 29/11, 2022 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.