如何在 GIT 中标记单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5959622/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:27:01  来源:igfitidea点击:

How to Tag a single file in GIT

gitversion-controlgit-svn

提问by u557022

I am very new to git. I am currently trying to get familiarized with it by using it to track changes in some excel files that I am maintaining to track some continuous activity I am involved with. All the files are in a single repository. I want to tag each file separately with their versions. Is this possible? So far what I found was the capability to tag the entire repository. If what I am trying to do is wrong, do advise me on the best practice.

我对 git 很陌生。我目前正试图通过使用它来跟踪我正在维护的一些 excel 文件中的更改来熟悉它,以跟踪我参与的一些连续活动。所有文件都在一个存储库中。我想用它们的版本分别标记每个文件。这可能吗?到目前为止,我发现能够标记整个存储库。如果我尝试做的事情是错误的,请就最佳实践向我提出建议。

Thanks in advance.

提前致谢。

Edit

编辑

When I was doing this I deliberately deleted a previous tag to make the entire repository be tagged( as I did not find a way to tag single files) as v1.0. As I now want to reset the name of the tag with the file name and quite comfortable with the way things should happen, how can I rollback the deletion and rename the previous tag (the deleted tag)?

当我这样做时,我故意删除了以前的标签,以便将整个存储库标记为 v1.0(因为我没有找到标记单个文件的方法)。由于我现在想用文件名重置标签的名称并且对事情应该发生的方式非常满意,我如何回滚删除并重命名前一个标签(已删除的标签)?

采纳答案by Matthew Scharley

Tags are placed on specific commits as opposed to the repository as a whole as you suggest in your question.

标签放置在特定的提交上,而不是您在问题中建议的整个存储库。

One way of doing what you are suggesting is making sure you only commit changes to a single file with each commit, then you can tag that commit with eg. file1-v1.0to represent v1.0 of file1.

执行您建议的一种方法是确保每次提交时只提交对单个文件的更改,然后您可以使用例如标记该提交。file1-v1.0代表file1的v1.0。

What are you trying to represent by tagging these commits though? That will influence any advice on how to better your process.

你想通过标记这些提交来代表什么?这将影响有关如何改进流程的任何建议。

回答by Rudi

Technically you can tag the content of a single file without it's file name. But such tags are of limited use. Tags are expected to point to commits, and special tags to non-commits have very different behavior (you can't git checkout such a special tag). So I strongly suggest to never use non-commit tags.

从技术上讲,您可以在没有文件名的情况下标记单个文件的内容。但此类标签的用途有限。标签应该指向提交,非提交的特殊标签有非常不同的行为(你不能 git checkout 这样的特殊标签)。所以我强烈建议永远不要使用非提交标签。

When you want only some files to be tagged, it might be better to use a separate repo for them, or at least different branches, since git always looks at the full tree for it's operations.

当您只想标记某些文件时,最好为它们使用单​​独的 repo,或者至少使用不同的分支,因为 git 总是查看完整的树以进行操作。

If you still insist to create such a special tag you do:

如果你仍然坚持创建这样一个特殊的标签,你可以:

> git ls-tree HEAD
040000 tree 2c186ad49fa24695512df5e41cb5e6f2d33c119b    bar
100644 blob 409940768f2a684935a7d15a29f96e82c487f439    foo.txt

> git tag my-bar-tree 2c186ad49fa24695512df5e41cb5e6f2d33c119b
> git tag my-foo-file 409940768f2a684935a7d15a29f96e82c487f439

回答by ralphtheninja

You can only tag a commit, e.g. a certain snap shot in the history of your repository. However, git stores the files as blobs and what you can do is to use git notesto add a note to a blob, like this:

您只能标记提交,例如存储库历史记录中的某个快照。但是,git 将文件存储为 blob,您可以使用git notes注释添加到 blob,如下所示:

$ git ls-tree HEAD
100644 blob 252f7c7df5bd181536a6c9d0f9c371ce1a5dd042    .gitignore
100644 blob 8150ada74aba86c983ac3f8f63ab26aaa76fdcb7    README
100644 blob c4b1ff6dcb2a8e50727df21ced8d2872cd91af79    TODO.txt

$ git notes add -m "Adding a note to TODO" c4b1ff6dcb2a8e507
$ git notes show c4b1ff6dcb2a8e507
Adding a note to TODO

However, note that this note (no pun intended :) )is only attached to this blob, so whenever the file changes a new blob will be created (with another sha1 hash value) and the new blob will not have this note.

但是,请注意,此注释(无意双关语:))仅附加到此 blob,因此每当文件更改时,将创建一个新 blob(具有另一个 sha1 哈希值),而新 blob 将没有此注释。

回答by Adam Dymitruk

You should really explore having separate branches that track changes to specific files. You should be able to work with that.

您应该真正探索使用单独的分支来跟踪对特定文件的更改。你应该能够使用它。

回答by wnoise

Not directly, no.

不直接,不。

A tag is a pointer to a specific revision of the repository. The individual files do not themselves have versions separate from that of the repository. If you want separate versions, your options are to have a separate repository for each file, have a separate branch for each file, or to use a different tool that is file oriented (such as RCS -- though it lacks many of the nice features git has).

标签是指向存储库特定修订版的指针。单个文件本身没有与存储库的版本分开的版本。如果你想要单独的版本,你的选择是为每个文件有一个单独的存储库,为每个文件有一个单独的分支,或者使用面向文件的不同工具(例如 RCS——尽管它缺少许多不错的功能) git有)。

If the files are at all related, you generally do want to tag a specific version of the group of them. If they're not, you can still tag the entire group with the version of each file changed in that revision. Restricting changes to one file per revision can make this process easier to manage.

如果文件完全相关,您通常确实希望标记它们组的特定版本。如果不是,您仍然可以使用该修订版中更改的每个文件的版本来标记整个组。将每个修订版的更改限制为一个文件可以使这一过程更易于管理。