183

When I'm going to tag a commit, I need to know what changed since the last tagged commit. Eg:

a87a6sdf87a6d4 Some new feature
a87a6sdf87a6d3 Some bug fix
a87a6sdf87a6d2 Some comments added
a87a6sdf87a6d1 Some merge <- v1.4.0

In this example I would like to know about the 3 newest commits, or be able to print a log like above, that shows both commits their tags if any. And when I see there has been a new feature added, I would tag it v1.5.0.

How do you deal with this? Is this how I'm supposed to use tags? What should I write in the tag message? I always leave it blank: git tag -a v1.2.3 -m ''

eis
  • 51,991
  • 13
  • 150
  • 199
ChocoDeveloper
  • 14,160
  • 26
  • 79
  • 117
  • I know this was an old question, but what operating system was you interested about? Somebody replied mentionig Windows about first answer, for example. Are you using Linux? Maybe nice to clarify in the question that you want a system-independent solution or something like that – Valerio Bozz Jun 29 '23 at 13:55

4 Answers4

313
git log <yourlasttag>..HEAD

If you want them like in your example, on the one line with commit id + message, then

git log <yourlasttag>..HEAD --oneline

and in case you don't know your latest tag or want this to be dynamic, on Linux / git bash / Windows bash you could do:

git log $(git describe --tags --abbrev=0)..HEAD --oneline

and on Windows:

for /f "delims=" %a in ('git describe --tags --abbrev^=0') do @set latesttag=%a
git log %latesttag%..HEAD --oneline

Also, if you have a case where you know a tag in history and want to print everything from that tag up to current situation, you might want to add also --decorate so it would print out any tags in between.

eis
  • 51,991
  • 13
  • 150
  • 199
  • `--oneline` should be before `..HEAD`. Also, `@` is short for `HEAD`. Using `$()` may be preferable to using backticks. – Asclepius Feb 22 '18 at 17:32
  • @A-B-B there is no difference if `--oneline` is before or after. I would also prefer HEAD even if there is a shorthand, as it is easier to understand and more widely known. – eis Feb 23 '18 at 03:14
  • and I see no benefit in using `$()` in this context. Maybe you have thought of some. – eis Feb 24 '18 at 13:13
  • ok, it seems resources such as [this](http://mywiki.wooledge.org/BashFAQ/082) find several reasons in using braces instead of backticks, so could change to use those – eis Sep 12 '18 at 04:47
  • 1
    Is there a concise one-liner which can also handle cases where there may be no prior tag on the branch? It should just default to outputting the entire commit history for the branch. – Jonathan Ellis Mar 04 '19 at 15:24
  • The best I have currently is `last_tag=$(git describe --tags --abbrev=0); git log $(if [[ -z $last_tag ]]; then echo HEAD; else echo $last_tag..HEAD; fi) --pretty=format:"- %s"` but I'm sure this can be improved upon! – Jonathan Ellis Mar 04 '19 at 15:43
  • @JonathanEllis maybe you could raise that as a new question? – eis Mar 04 '19 at 16:13
  • 4
    In Powershell, wrap the git log argument in quotes: `git log "$(git describe --tags --abbrev=0)..HEAD" --oneline` – Joe Maffei Mar 16 '21 at 21:50
  • Since the Linux version also works on Git for Windows Bash, I tried to mention that as first option, also since it's simpler. Also I flagged as outdated some comments that were saying "I don't know" or other things already integrated. Then feel free to nuke my comment too. – Valerio Bozz Jun 29 '23 at 13:57
67

If your current commit is also a tag and you want to dynamically get the changes since the previous tag, without knowing the latest tag nor previous tag name, you can do:

git log --oneline $(git describe --tags --abbrev=0 @^)..@

Note that @ is short for HEAD.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
mediafreakch
  • 1,336
  • 1
  • 12
  • 19
  • 2
    nice!, if just want the commits text change --oneline to --pretty=format:"%s": git log --pretty=format:"%s" $(git describe --tags --abbrev=0 @^)..@ – JBarbosa Dec 07 '19 at 00:12
1

You can easily omit Merge commits with sed

git log $(git describe --tags --abbrev=0)..@ --oneline | sed '/Merge/d'
1

As an addition to this answer, to omit merge commits, one can use

git log <tag>..HEAD --oneline --no-merges

as mentioned in this other answer

Harish Ganesan
  • 135
  • 1
  • 2
  • 11