使用 Git 下载特定标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/791959/
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
Download a specific tag with Git
提问by Hyman BeNimble
I'm trying to figure out how I can download a particular tag of a Git repository - it's one version behind the current version.
我试图弄清楚如何下载 Git 存储库的特定标签 - 它是当前版本之后的一个版本。
I saw there was a tag for the previous version on the git web page, with object name of something long hex number.
我看到 git 网页上有一个以前版本的标签,对象名称是很长的十六进制数字。
But the version name is "Tagged release 1.1.5
" according the site.
但是Tagged release 1.1.5
根据站点,版本名称是“ ”。
I tried a command like this (with names changed):
我尝试了这样的命令(名称已更改):
git clone http://git.abc.net/git/abc.git my_abc
And I did get something - a directory, a bunch of subdirectories, etc.
我确实得到了一些东西——一个目录、一堆子目录等。
If it's the whole repository, how do I get at the version I'm seeking? If not, how do I download that particular version?
如果是整个存储库,我如何获得我正在寻找的版本?如果没有,我如何下载该特定版本?
回答by besen
$ git clone
will give you the whole repository.
会给你整个存储库。
After the clone, you can list the tags with $ git tag -l
and then checkout a specific tag:
克隆后,您可以列出标签,$ git tag -l
然后签出特定标签:
$ git checkout tags/<tag_name>
Even better, checkout and create a branch (otherwise you will be on a branch named after the revision number of tag):
更好的是,签出并创建一个分支(否则您将位于以标签修订号命名的分支上):
$ git checkout tags/<tag_name> -b <branch_name>
回答by Toni
git clone --branch my_abc http://git.abc.net/git/abc.git
Will clone the repo and leave you on the tag you are interested in.
将克隆 repo 并将您留在您感兴趣的标签上。
Documentation for 1.8.0 of git clonestates.
git clone状态的1.8.0 文档。
--branch can also take tags and detaches the HEAD at that commit in the resulting repository.
--branch 还可以获取标签并在结果存储库中的该提交处分离 HEAD。
回答by Yuan HOng
For checking out only a given tag for deployment, I use e.g.:
为了仅检查给定的部署标签,我使用例如:
git clone -b 'v2.0' --single-branch --depth 1 https://github.com/git/git.git
This seems to be the fastest way to check out code from a remote repository if one has only interest in the most recent code instead of in a complete repository. In this way, it resembles the 'svn co' command.
如果人们只对最新的代码而不是完整的存储库感兴趣,这似乎是从远程存储库中检出代码的最快方法。这样,它类似于 'svn co' 命令。
Note: Per the Git manual, passing the --depth
flag implies --single-branch
by default.
注意:根据Git 手册,默认情况下传递--depth
标志意味着--single-branch
。
--depth
Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.
- 深度
创建一个浅克隆,其历史记录被截断为指定的提交次数。暗示 --single-branch ,除非给出 --no-single-branch 以获取所有分支尖端附近的历史记录。如果你想浅层克隆子模块,也可以通过 --shallow-submodules 。
回答by grossvogel
I'm not a git expert, but I think this should work:
我不是 git 专家,但我认为这应该有效:
git clone http://git.abc.net/git/abc.git
cd abc
git checkout my_abc
OR
或者
git clone http://git.abc.net/git/abc.git
cd abc
git checkout -b new_branch my_abc
The second variation establishes a new branch based on the tag, which lets you avoid a 'detached HEAD'. (git-checkout manual)
第二个变体基于标签建立一个新分支,它可以让您避免“分离的 HEAD”。(git-checkout 手册)
Every git repo contains the entire revision history, so cloning the repo gives you access to the latest commit, plus everything that came before, including the tag you're looking for.
每个 git repo 都包含整个修订历史记录,因此克隆 repo 可以让您访问最新的提交,以及之前的所有内容,包括您要查找的标签。
回答by Chris J
You can use git archive to download a tar ball for a given tag or commit id:
您可以使用 git archive 为给定的标签或提交 ID 下载一个 tar 包:
git archive --format=tar --remote=[hostname]:[path to repo] [tag name] > tagged_version.tar
You can also export a zip archive of a tag.
您还可以导出标签的 zip 存档。
List tags:
git tag 0.0.1 0.1.0
Export a tag:
git archive -o /tmp/my-repo-0.1.0.zip --prefix=my-repo-0.1.0/ 0.1.0
Notes:
- You do not need to specify the format. It will be picked up by the output file name.
- Specifying the prefix will make your code export to a directory (if you include a trailing slash).
列出标签:
git tag 0.0.1 0.1.0
导出标签:
git archive -o /tmp/my-repo-0.1.0.zip --prefix=my-repo-0.1.0/ 0.1.0
笔记:
- 您不需要指定格式。它将被输出文件名选中。
- 指定前缀将使您的代码导出到目录(如果您包含尾部斜杠)。
回答by eyecatchUp
Use the --single-branch
switch(available as of Git 1.7.10). The syntax is:
使用--single-branch
开关(从 Git 1.7.10 开始可用)。语法是:
git clone -b <tag_name> --single-branch <repo_url> [<dest_dir>]
For example:
例如:
git clone -b 'v1.9.5' --single-branch https://github.com/git/git.git git-1.9.5
The benefit: Git will receive objects and (need to) resolve deltas for the specified branch/tag only - while checking out the exact same amount of files! Depending on the source repository, this will save you a lot of disk space. (Plus, it'll be much quicker.)
好处:Git 将接收对象并(需要)仅解析指定分支/标签的增量 - 同时检查完全相同数量的文件!根据源存储库,这将为您节省大量磁盘空间。(另外,它会快得多。)
回答by tk_
first fetch all the tags in that specific remote
首先获取该特定遥控器中的所有标签
git fetch <remote> 'refs/tags/*:refs/tags/*'
orjust simply type
或者只是简单地输入
git fetch <remote>
Then check for the available tags
然后检查可用的标签
git tag -l
then switch to that specific tag using below command
然后使用以下命令切换到该特定标签
git checkout tags/<tag_name>
Hope this will helps you!
希望这会帮助你!
回答by Peter Johnson
If your tags are sortable using the linux sort
command, use this:
如果您的标签可以使用 linuxsort
命令排序,请使用以下命令:
git tag | sort -n | tail -1
eg. if git tag
returns:
例如。如果git tag
返回:
v1.0.1
v1.0.2
v1.0.5
v1.0.4
git tag | sort -n | tail -1
will output:
git tag | sort -n | tail -1
将输出:
v1.0.5
git tag | sort -n | tail -2 | head -1
will output:
git tag | sort -n | tail -2 | head -1
将输出:
v1.0.4
(because you asked for the second most recent tag)
(因为您要求提供第二个最近的标签)
to checkout the tag, first clone the repo, then type:
要签出标签,首先克隆 repo,然后键入:
git checkout v1.0.4
..or whatever tag you need.
..或者你需要的任何标签。
回答by None-da
I checked the git checkout documentation, it revealed one interesting thing:
我检查了git checkout 文档,它揭示了一件有趣的事情:
git checkout -b <new_branch_name> <start_point> , where the <start_point> is the name of a commit at which to start the new branch; Defaults to HEAD
git checkout -b <new_branch_name> <start_point> ,其中 <start_point> 是开始新分支的提交的名称;默认为 HEAD
So we can mention the tag name( as tag is nothing but a name of a commit) as, say:
所以我们可以提到标签名称(因为标签只是一个提交的名称),比如:
>> git checkout -b 1.0.2_branch 1.0.2
later, modify some files
>> git push --tags
>> git checkout -b 1.0.2_branch 1.0.2
以后,修改一些文件
>> git push --tags
P.S: In Git, you can't update a tag directly(since tag is just a label to a commit), you need to checkout the same tag as a branch and then commit to it and then create a separate tag.
PS:在 Git 中,你不能直接更新标签(因为标签只是提交的标签),你需要签出与分支相同的标签,然后提交给它,然后创建一个单独的标签。
回答by Ivan
git fetch <gitserver> <remotetag>:<localtag>
===================================
====================================
I just did this. First I made sure I knew the tag name spelling.
我就是这样做的。首先,我确保我知道标签名称的拼写。
git ls-remote --tags gitserver; : or origin, whatever your remote is called
This gave me a list of tags on my git server to choose from. The original poster already knew his tag's name so this step is not necessary for everyone. The output looked like this, though the real list was longer.
这给了我 git 服务器上的标签列表供我选择。原发帖人已经知道他的标签名称,所以这一步不是每个人都需要的。输出看起来像这样,尽管真正的列表更长。
8acb6864d10caa9baf25cc1e4857371efb01f7cd refs/tags/v5.2.2.2
f4ba9d79e3d760f1990c2117187b5010e92e1ea2 refs/tags/v5.2.3.1
8dd05466201b51fcaf4ca85897347d82fcb29518 refs/tags/Fix_109
9b5087090d9077c10ba22d99d5ce90d8a45c50a3 refs/tags/Fix_110
I picked the tag I wanted and fetched that and nothing more as follows.
我选择了我想要的标签并获取了它,仅此而已,如下所示。
git fetch gitserver Fix_110
I then tagged this on my local machine, giving my tag the same name.
然后我在我的本地机器上标记它,给我的标记相同的名称。
git tag Fix_110 FETCH_HEAD
I didn't want to clone the remote repository as other people have suggested doing, as the project I am working on is large and I want to develop in a nice clean environment. I feel this is closer to the original questions "I'm trying to figure out how do download A PARTICULAR TAG" than the solution which suggests cloning the whole repository. I don't see why anyone should have to have a copy of Windows NT and Windows 8.1 source code if they want to look at DOS 0.1 source code (for example).
我不想像其他人建议的那样克隆远程存储库,因为我正在处理的项目很大,我想在一个干净的环境中进行开发。我觉得这比建议克隆整个存储库的解决方案更接近原始问题“我正在尝试弄清楚如何下载特定标签”。我不明白为什么任何人如果想要查看 DOS 0.1 源代码(例如),就必须拥有一份 Windows NT 和 Windows 8.1 源代码的副本。
I also didn't want to use CHECKOUT as others have suggested. I had a branch checked out and didn't want to affect that. My intention was to fetch the software I wanted so that I could cherry-pick something and add that to my development.
我也不想像其他人建议的那样使用 CHECKOUT。我检查了一个分支,不想影响它。我的目的是获取我想要的软件,以便我可以挑选一些东西并将其添加到我的开发中。
There is probably a way to fetch the tag itself rather than just a copy of the commit that was tagged. I had to tag the fetched commit myself. EDIT: Ah yes, I have found it now.
可能有一种方法可以获取标签本身,而不仅仅是被标记的提交的副本。我必须自己标记获取的提交。编辑:啊,是的,我现在找到了。
git fetch gitserver Fix_110:Fix_110
Where you see the colon, that is remote-name:local-name and here they are the tag names. This runs without upsetting the working tree etc. It just seems to copy stuff from the remote to the local machine so you have your own copy.
你看到冒号的地方就是远程名称:本地名称,这里是标签名称。这运行时不会打乱工作树等。它似乎只是将东西从远程复制到本地机器,因此您拥有自己的副本。
git fetch gitserver --dry-run Fix_110:Fix_110
with the --dry-run option added will let you have a look at what the command would do, if you want to verify its what you want. So I guess a simple
添加 --dry-run 选项后,您可以查看该命令的作用,如果您想验证它是您想要的。所以我想一个简单的
git fetch gitserver remotetag:localtag
is the real answer.
是真正的答案。
=
=
A separate note about tags ... When I start something new I usually tag the empty repository after git init, since
关于标签的单独说明......当我开始新的东西时,我通常在 git init 之后标记空存储库,因为
git rebase -i XXXXX
requires a commit, and the question arises "how do you rebase changes that include your first software change?" So when I start working I do
需要提交,问题就出现了“你如何重新调整包括你的第一次软件更改的更改?” 所以当我开始工作时
git init
touch .gitignore
[then add it and commit it, and finally]
git tag EMPTY
i.e. create a commit before my first real change and then later use
即在我第一次真正改变之前创建一个提交,然后再使用
git rebase -i EMPTY
if I want to rebase all my work, including the first change.
如果我想重新调整我所有的工作,包括第一个更改。