Git Checkout Latest Tag

33

10

I'm writing a shell script and I'm looking to checkout the latest version of repo. Specifically I want to break this process apart into multiple steps.

  1. I want to save the repositories latest tag into a variable
  2. Print out Checking out version: XX
  3. Checkout the latest tag

I've seen similar questions but I don't see how to save the name of the tag into a variable (probably because I'm a noob with shell scripts).

BFTrick

Posted 2013-07-01T21:34:31.200

Reputation: 1 657

possible duplicate of How to get the latest tag name in current branch in Git?. Only duplicate because you explicitly require I want to save the repositories latest tag into a variable, the rest is just Bash.

– Ciro Santilli 新疆改造中心 六四事件 法轮功 – 2014-06-06T12:20:15.323

Answers

18

git describe --tags should give you info.

bash/ shell script:

#!/bin/bash
...
latesttag=$(git describe --tags)
echo checking out ${latesttag}
git checkout ${latesttag}

exussum

Posted 2013-07-01T21:34:31.200

Reputation: 14 093

1But how do I save step 1 into a variable? I know how to print it to the screen. – BFTrick – 2013-07-02T16:36:22.690

1using var=$(command) – exussum – 2013-07-02T16:37:55.540

This worked! Thanks! :) – BFTrick – 2013-07-04T03:14:13.853

Does anyone know if you need to git fetch --tags before doing this? – Steven Linn – 2015-01-20T22:41:37.077

No, the tag your currently on has to already be in the history – exussum – 2015-12-03T18:04:12.977

if you are on oh-my-zsh, then you can do something like git checkout \gdct`` – elquimista – 2016-12-27T07:11:05.317

I would git checkout master;git pull then the rest of your script – lrkwz – 2018-01-24T13:20:14.887

73

# Get new tags from remote
git fetch --tags

# Get latest tag name
latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

# Checkout latest tag
git checkout $latestTag

Josef Ježek

Posted 2013-07-01T21:34:31.200

Reputation: 1 341

4Even more dense, skip the variable git checkout $(git describe --tagsgit rev-list --tags --max-count=1) – ThorSummoner – 2015-02-18T21:50:13.450

1@ThorSummoner thanks, but your back-ticks got lost in the formatting. Using nested $()s instead: git checkout $(git describe --tags $(git rev-list --tags --max-count=1)) – Daniel Griscom – 2018-02-07T18:32:35.340

Does not work for me is it because I am using -C folder ? – Dimitri Kopriwa – 2018-03-12T16:38:26.003

tks git describe --tags git rev-list --tags --max-count=1 – lanni654321 – 2018-04-16T05:36:11.140

7

In some repositories the git describe --tags gives no info and a simple git tag | tail -1 can get you the wrong tag as git sorts tags in a strange way.

For me the best command is a variation of the tail one

VERSION=$(git tag | sort -V | tail -1)

Albert Casademont

Posted 2013-07-01T21:34:31.200

Reputation: 91

sort: invalid option -- V – highmaintenance – 2015-02-07T16:20:34.323

IF your versions are X.X.X: VERSION=$(git tag | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | sort -t. -k 1,1n -k 2,2n -k 3,3n | tail -1) – Mark C – 2015-07-24T13:13:29.360

1

In order to put information into a variable, you assign it:

myvar=myvalue

However, you want to calculate the value to assign, you're not just assigning a constant to the variable. In your case, you want to assign the output of a command to the variable.

First, you have to figure out how to get the last tag name. I'll leave that up to you, as you haven't said anything about how the tag names are created.

Then once you have a command that gives the last tag name, you need to assign the name into a variable. Bash does that with "command substitution".

For example: thetagname=$( command_to_get_tag_name )

So if you were to just take the last tag that git reports like this:

git tag | tail -1

then you could assign it to a variable like this:

thetagname=$( git tag | tail -1)

and you could use/see the value like this:

echo $thetagname

or as user1281385 says, like this:

echo ${thetagname}

The two methods are the same, except that the second way allows you to combine literal text with the variable value:

echo ${thetagname}ing

which will append "ing" to the contents of $thetagname. The braces are necessary in order to prevent bash from thinking that "thetagnameing" is the variable.

The bash man page has a section called EXPANSION, in which it explains the 7 kinds of expansion. Command substitution is one of them. The bash man page is rather big, and indeed repeats all the interesting keywords multiple times, so it is really annoying to search for stuff in it. Here are a couple of tips on how to find the EXPANSION section (and learn a bit about the pager "less"):

Start the manual reader reading the bash man page like this:

man bash

Search for the term 'EXPANSION' at the beginning of a line once you're in the reader by typing /^EXPANSION into the display. Once you type /, you will see a / at the bottom of the screen, but the man page will still be there. That is the command to search for a pattern. Then you type ^EXPANSION, and you will see that at the bottom of the screen as well. ^ means "search for things at the beginning of the line" and EXPANSION means "look for the literal string "EXPANSION". Then type <enter> - and you should be at the first occurence of the term EXPANSION that occurs at the beginning of the line. Here it describes all the kinds of expansion that the bash shell does on your line after you type it and before it executes the transformed command.

When in the pager, you can type h to get a list of the possible commands.

I hope this wasn't too basic. If you haven't seen it before, it's hard to figure out.

Brenda J. Butler

Posted 2013-07-01T21:34:31.200

Reputation: 750

This won't work if you have tags like v1.15.0 and v1.9.0 -- you'll see v1.9.0 come first. – connorbode – 2018-03-16T02:28:20.313

1I did say "First, you have to figure out how to get the last tag name. I'll leave that up to you, as you haven't said anything about how the tag names are created." and "So if you were to just take the last tag that git reports like this:" [emphasis added]. The method given (pipe git tag to tail) is a stand-in for the person's own method, for demonstration purposes. – Brenda J. Butler – 2018-05-13T13:39:45.953

0

"git tag --contains | tail -1" , (git tag --contains) lists all tags in current branch, (tail -1) limits the count of output results to be l,and it's a latest one.

Antutu

Posted 2013-07-01T21:34:31.200

Reputation: 1