Git Checkout Latest Tag
GitBashShellTagsGit Problem Overview
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.
- I want to save the repositories latest tag into a variable
- Print out
Checking out version: XX
- 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).
Git Solutions
Solution 1 - Git
# 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
Solution 2 - Git
git describe --tags
should give you info.
bash/ shell script:
#!/bin/bash
...
latesttag=$(git describe --tags)
echo checking out ${latesttag}
git checkout ${latesttag}
Solution 3 - Git
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)
Solution 4 - Git
git tag --contains | tail -1
git tag --contains
lists all tags in the current branch, tail -1
limits the count of output results to be 1,and it will be the latest one.
Solution 5 - Git
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.