How to Tag a single file in GIT

24,647

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

Solution 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

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

Solution 4

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

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

Share:
24,647
picmate 涅
Author by

picmate 涅

https://www.linkedin.com/in/pathummke

Updated on June 13, 2020

Comments

  • picmate 涅
    picmate 涅 almost 4 years

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

  • picmate 涅
    picmate 涅 almost 13 years
    thanks. As I said in my question as well my primary interest is to get to know more about the git environment. But at this particular instance what I am trying to do is to track a specific milestone which I meant to achieve in maintaining the document. For example: when I finish doing the initial work I tag it as v1.0, when I complete going through the document with the others and do all the necessary changes I want to make it v2.0 etc. In essence if I make one commit as v1.0 and another on another file as v2.0 then I will not be achieving this. In fact both should be v1.0
  • picmate 涅
    picmate 涅 almost 13 years
    thanks, in fact what you've suggested finally is what I thought of doing. Further I have been committing changes only on the basis of one file at a time.
  • exhuma
    exhuma almost 9 years
    Considering your ending note (about the changing SHA after a file change): This looks to me like this is the intended behaviour. No?