Releases and Semantic Versioning

Updated · 10 min read
easyGit 2.53gitreleasessemvergit-describeinterview

A textbook's cover says "3rd edition". Readers know at once that it is newer than the 2nd, and a teacher can say "use the 3rd edition" and everyone has the same book. A small correction is printed as "3rd edition, revised", not as a 4th edition.

Software releases need the same kind of numbering. A release is a version of your project that you give to users: the school office, customers, or other developers. Each release gets a version number and a tag on its commit, so that everyone can name it, find it and build it again. The most common numbering scheme, semantic versioning, tells users in the number itself whether an update is safe to install.

In this lesson, you will choose version numbers, name builds between releases with git describe, write release notes from history, and publish a release on a hosting platform.

Semantic Versioning

A semantic version has three numbers, MAJOR.MINOR.PATCH, such as 2.4.1:

PartIncrease it when youExample
MAJORMake a change that breaks things for existing users1.4.2 → 2.0.0: the fee file format changed
MINORAdd a feature that doesn't break anything1.4.2 → 1.5.0: receipts were added
PATCHFix a bug without adding features1.4.2 → 1.4.3: a wrong total was corrected

When one part goes up, the parts to its right go back to 0. Versions before 1.0.0, such as 0.9.0, mean "still in development: anything may change". A hyphen marks a pre-release, such as 2.0.0-beta.1, which comes before 2.0.0.

Git doesn't enforce any of this. The convention lives in your tag names, usually with a v in front: v1.5.0. Java projects also put the version in their build file, such as <version>1.5.0</version> in Maven's pom.xml, and update it together with the tag.

Which number to raise

The Starting Point

Navika released v1.0.0, and has made two commits since:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
git tag -a v1.0.0 -m "First release"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipts"
git log --oneline
Output
73d7e6f (HEAD -> main) Add receipts
3550432 Add the late fee
6b651a0 (tag: v1.0.0) Add the term fee

Name Any Build: git describe

Between releases, builds still need a name, for bug reports and test results. git describe names the current commit after the newest annotated tag before it:

Terminal
git describe
Output
v1.0.0-2-g73d7e6f
PartMeaning
v1.0.0The newest annotated tag in the history
2The number of commits since that tag
gShort for "git", to mark what follows as a commit ID
73d7e6fThe short ID of the current commit

So this build is "two commits after 1.0.0". Anyone can go back to it exactly. Build tools often put this text into the application's "About" page or log.

Release a New Version

The two commits added a feature (receipts) and changed nothing that breaks, so the next version is a minor release, v1.1.0:

Terminal
git tag -a v1.1.0 -m "Add receipts and the late fee"
git describe
echo "draft" >> receipts.txt
git describe --dirty
git restore receipts.txt
Output
v1.1.0
v1.1.0-dirty

On a tagged commit, git describe prints just the tag. --dirty adds -dirty when the working tree has uncommitted changes, a useful warning in a build's version: that build can't be reproduced from any commit.

Release Notes from History

The commits between two tags are exactly what changed in a release. Two-dot ranges list them:

Terminal
git log --oneline v1.0.0..v1.1.0
git shortlog v1.0.0..v1.1.0
Output
73d7e6f (HEAD -> main, tag: v1.1.0) Add receipts
3550432 Add the late fee
Navika Anamaneni (2):
      Add the late fee
      Add receipts

git log gives the list for the release notes, and git shortlog groups it by author, which is handy for thanking contributors. Good commit messages (see "Writing Good Commit Messages") make this nearly ready to publish. Teams that use Conventional Commits often generate the notes, and even the next version number, automatically from feat: and fix: messages.

Package a Release: git archive

git archive creates a ZIP or tar file of the project exactly as it was at a tag, without the .git folder or any untracked files:

Terminal
git archive --format=tar.gz --prefix=fee-tracker-1.1.0/ -o fee-tracker-1.1.0.tar.gz v1.1.0
tar -tzf fee-tracker-1.1.0.tar.gz
Output
fee-tracker-1.1.0/
fee-tracker-1.1.0/fees.txt
fee-tracker-1.1.0/receipts.txt

--prefix puts the files in a folder inside the archive, and --format=zip makes a ZIP instead. Hosting platforms create the same kind of archive automatically for every tag.

Publish a Release on the Platform

A tag is enough for Git. Hosting platforms add a release page on top: a title, the release notes, and files to download. Push the tag first, then:

text
1. Repository > Releases > Draft a new release.
2. Choose a tag: v1.1.0 (or type a new one to create it).
3. Title: Fee Tracker 1.1.0
4. Write the notes, or click "Generate release notes".
5. Attach files, such as the built .jar.
6. Check "Set as a pre-release" for betas. Click Publish release.

Users then find every release, with its notes and files, in one place.

Syntax

CommandIt is used to
git tag -a v<MAJOR.MINOR.PATCH> -m "<message>"Mark a release
git describeName the current commit after the newest annotated tag
git describe --tagsAlso consider lightweight tags
git describe --dirtyAdd -dirty if there are uncommitted changes
git describe --alwaysFall back to a short commit ID when there is no tag
git log --oneline <old-tag>..<new-tag>List the changes in a release
git shortlog <old-tag>..<new-tag>The same, grouped by author
git archive -o <file> <tag>Package the files of a release

Common Mistakes

Changing the version number without tagging. The number in pom.xml then can't be traced to a commit. Tag every release.

Using a minor release for a breaking change. Users who trust the number update and their setup breaks. Breaking means MAJOR.

Reusing or moving a version tag. Two different builds both called v1.1.0 cause endless confusion. Make v1.1.1.

fatal: No names found, cannot describe anything. There is no annotated tag in the history. Tag a release, or use git describe --always.

Releasing a dirty build. A version ending in -dirty contains changes that exist in no commit. Commit first, then build.

Under the Hood

git describe walks back from the current commit, through its parents, until it reaches a commit that has an annotated tag, counting the commits on the way. If several tags are in reach, it picks the one with the fewest commits in between. The g<id> part is made long enough to be unique in the repository when the name is printed, so the name leads back to exactly one commit, and adding new tags later doesn't change which commit it names.

Interview Corner

What is semantic versioning?

A versioning scheme with three numbers, MAJOR.MINOR.PATCH: increase MAJOR for incompatible changes, MINOR for backward-compatible features, and PATCH for backward-compatible bug fixes. Pre-releases add a suffix such as -beta.1.

What does git describe output, and what is it used for?

The newest reachable annotated tag, the number of commits since it, and the abbreviated commit ID, such as v1.0.0-2-g73d7e6f. It gives every build a readable, traceable version name.

How do you list the changes between two releases?

git log --oneline v1.0.0..v1.1.0 lists the commits in the newer release that aren't in the older one. git shortlog groups them by author.

What is the difference between a tag and a GitHub release?

A tag is a Git ref to a commit. A GitHub release is a page built on a tag, with a title, release notes and downloadable files. GitLab has the same feature, and Bitbucket uses tags and its Downloads page.

Practice

Challenge: name the next build. After v1.1.0, Navika fixes a rounding bug in one commit (add Totals are rounded to the nearest dollar. to fees.txt). What should the next version be? Commit the fix, print git describe, then tag the right version and print git describe again.

What does Git print? Before you open the solution, decide the next version number, and the two git describe results.

Show solution
Terminal
echo "Totals are rounded to the nearest dollar." >> fees.txt
git commit --quiet -am "Fix rounding of totals"
git describe
git tag -a v1.1.1 -m "Fix rounding of totals"
git describe
Output
v1.1.0-1-g4f01802
v1.1.1

A bug fix with no new feature is a patch: v1.1.1. Before tagging, the commit is one after v1.1.0, so git describe names it v1.1.0-1-g followed by its ID. After tagging, it is simply v1.1.1.

Checkpoint. In a practice repository, make three releases: one patch, one minor and one major, each with an annotated tag and a sentence explaining why it got that number. Generate the notes for each with git log --oneline between tags, and package the last one with git archive.

Quick check: Releases

4 questions
1Version 1.4.2 gains a new feature that breaks nothing. What is the next version?
2What does v1.0.0-2-g73d7e6f from git describe mean?
3Which command lists the commits in release v1.1.0 that weren't in v1.0.0?
4Which change needs a new MAJOR version?

Key Takeaways

Quick reference

CommandIt is used to
git tag -a v1.1.0 -m "<message>"Mark a release
git push --follow-tagsPublish it
git describe --dirtyName the current build
git log --oneline v1.0.0..v1.1.0List changes for release notes
git shortlog v1.0.0..v1.1.0Changes grouped by author
git archive -o release.zip v1.1.0Package a release

Was this tutorial helpful?

Lesson 53 of 106