Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?

This function automatically grabs the latest git tag and, based on keyword (major, minor, patch), adds a new tag. (e.g. git_tag patch for v1.2.0 would create v1.2.1)

Drop this into your ~/.bash_profile and run source ~/.bash_profile to use it.

You can find all of my dotfiles here:

## ----- Tag ----- ##
## Automatically set a git tag based on Semantic Versioning keyword
## Note: This assumes you prefix your git tags with 'v' (e.g 'v1.2.0')
## $1 - 'major', 'minor', 'patch'
## Usage: `git_tag major`
function git_tag() {
local tag=$(git describe --abbrev=0 --tags)
local prefix=$(echo $tag | grep -o --color=never "^[A-z]\+")
local major=$(echo $tag | cut -d '.' -f1 | sed "s/^$prefix//")
local minor=$(echo $tag | cut -d '.' -f2)
local patch=$(echo $tag | cut -d '.' -f3)
case "$1" in
local nmajor=$(($major+1))
local version="$nmajor.0.0"
local nminor=$(($minor+1))
local version="$major.$nminor.0"
local npatch=$(($patch+1))
local version="$major.$minor.$npatch"
echo "Automatically set a git tag based on Semantic Versioning keyword"
echo "Usage: git_tag <major|minor|patch>"
echo "Example:"
echo " git_tag major"
if [[ "$version" ]]
git tag -a "$prefix$version" -m "Version $version"
echo "$prefix$version created!"

This comment has been minimized.

Copy link
Owner Author

@jswanner jswanner commented Jul 29, 2014

Differs from original in that: don't leak as globals, v tag prefix is optional, and it prints usage text if you don't give it one of the SemVer keywords.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment