How to Tag a single file in GIT

故事扮演 提交于 2019-12-28 20:35:26

问题


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.

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)?


回答1:


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.0 to represent v1.0 of file1.

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




回答2:


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.

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.

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



回答3:


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 notes to add a note to a blob, like this:

$ 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.




回答4:


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




回答5:


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).

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.



来源:https://stackoverflow.com/questions/5959622/how-to-tag-a-single-file-in-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!