bash Git Checkout 最新标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17414104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Checkout Latest Tag
提问by BFTrick
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.
我正在编写一个 shell 脚本,我正在寻找最新版本的 repo。具体来说,我想将此过程分解为多个步骤。
- I want to save the repositories latest tag into a variable
- Print out
Checking out version: XX
- Checkout the latest tag
- 我想将存储库最新标签保存到一个变量中
- 打印
Checking out version: XX
- 签出最新的标签
I've seen similar questionsbut I don't see how to save the name of the tag into a variable (probably because I'm a noob with shell scripts).
我见过类似的问题,但我不知道如何将标签的名称保存到变量中(可能是因为我是 shell 脚本的菜鸟)。
采纳答案by exussum
git describe --tags
should give you info.
git describe --tags
应该给你信息。
bash/ shell script:
bash/shell 脚本:
#!/bin/bash
...
latesttag=$(git describe --tags)
echo checking out ${latesttag}
git checkout ${latesttag}
回答by Josef Je?ek
# 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
回答by Albert Casademont
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.
在某些存储库中,git describe --tags
它不提供任何信息,并且git tag | tail -1
由于 git 以一种奇怪的方式对标签进行排序,因此一个简单的方法可能会让您得到错误的标签。
For me the best command is a variation of the tail one
对我来说最好的命令是尾部的变体
VERSION=$(git tag | sort -V | tail -1)
VERSION=$(git tag | sort -V | tail -1)
回答by Brenda J. Butler
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".
然后,一旦您有一个给出最后一个标签名称的命令,您就需要将该名称分配给一个变量。Bash 使用“命令替换”来做到这一点。
For example: thetagname=$( command_to_get_tag_name )
例如: thetagname=$( command_to_get_tag_name )
So if you were to just take the last tag that git reports like this:
因此,如果您只使用 git 报告的最后一个标签,如下所示:
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:
或如 user1281385 所说,像这样:
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.
它将“ing”附加到 $thetagname 的内容中。大括号是必要的,以防止 bash 认为“thetagnameing”是变量。
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"):
bash 手册页有一个名为 EXPANSION 的部分,其中解释了 7 种扩展。命令替换就是其中之一。bash 的手册页比较大,而且确实把所有有趣的关键字都重复了很多遍,所以在里面搜索东西真的很烦人。这里有一些关于如何找到 EXPANSION 部分的提示(并了解一些关于“less”的寻呼机):
Start the manual reader reading the bash man page like this:
像这样启动手动阅读器阅读 bash 手册页:
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.
/^EXPANSION
进入阅读器后,通过在显示屏上键入内容,在行首搜索术语“扩展” 。键入 后/
,您将在屏幕底部看到一个 /,但手册页仍然存在。这是搜索模式的命令。然后你输入^EXPANSION
,你也会在屏幕底部看到它。^ 表示“搜索行首的内容”,EXPANSION 表示“查找文字字符串“EXPANSION”。然后输入<enter>
- 您应该出现在行首的术语 EXPANSION 的第一次出现处。在这里,它描述了 bash shell 在您键入之后和执行转换后的命令之前在您的行上执行的所有类型的扩展。
When in the pager, you can type h
to get a list of the possible commands.
在寻呼机中,您可以键入h
以获取可能命令的列表。
I hope this wasn't too basic. If you haven't seen it before, it's hard to figure out.
我希望这不是太基本。如果你以前没有见过它,很难弄清楚。
回答by Antutu
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.
git tag --contains
列出当前分支中的所有标签,tail -1
限制输出结果的数量为1,并且将是最新的。