标记之间的 Git 日志

我试图在两次带标记的提交之间输出日志。

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

如果我这样做:

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

它输出所有的提交,因为开始的回购,这不是我想要的。我已经通读了 git 日志手册,但它没有多大帮助。

76386 次浏览

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

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

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

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.

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.

I'm using the following shortcut

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