Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git tag for a subfolder of a repository

I use Git to import a SVN repository. Then I created my own project as a subfolder in the repository.

I use the SVN repository with Git-SVN. My working procedure is:

  1. git commit -am "message"
  2. git svn rebase
  3. git svn dcommit.

Now I want to tag my project with git tag -a RC1 -m 'Release Candidate 1', but I only want that my project gets the tag.

How can I do that?

like image 632
BuZZ-dEE Avatar asked Oct 09 '12 09:10

BuZZ-dEE


2 Answers

TL;DR version

It's possible to tag specific directories (aka trees) if you know the tree's SHA-1, but it's not often done & not easy to do useful things with that tag.

Long answer

Every object in Git has a unique SHA-1. Most commonly, SHA-1s refer to commits, but they can also refer to blobs (file contents) and trees (directory structures & filenames/file-permission mappings). You can read about it in the Git Objects documentation.

For example, suppose I'm in a particular directory in my repository. I can run git ls-tree HEAD to get the list of files/directories in my path, along with their SHA-1s:

$git ls-tree HEAD
100644 blob ed76d466f5025ce88575770b07a65c49b281ca59    app.css
100644 blob ed58ee4a9be6f5b58e25e5b025b25e6d04549767    app.js
100644 blob e2bed82bd9554fdd89d982b37a8e0659fe82390a    controllers.js
040000 tree f888c44e16f7811ba69a245adf35c4303cb0d4e7    data
100644 blob d68aa862e4746fc9abd0132cc576a4df266b0a9d    directives.js
100644 blob df0ae0e7288617552b373d21f7796adb8fb0d1b6    index.html
040000 tree fa9c05b1bb45fb85821c7b1c27925b2618d646ac    partials
100644 blob 28e9eb6fe697cb5039d1cb093742e90b739ad6af    services.js

I can then tag one of these trees (let's say the data directory above):

$git tag data-1.0 f888c44e16f7811ba69a245adf35c4303cb0d4e7

The tag is now an alias for that SHA-1 and I can use it wherever a SHA-1 for a tree is accepted:

$git ls-tree -rt data-1.0
100644 blob 6ab0a52a17d14cbc8e30c4bf8d060c4ff58ff971    file1.json
100644 blob e097e393fa72007b0c328d67b70ba1c571854db0    file2.json
040000 tree 39573c56941fdd2fc88747a96bf871550f4affb2    subfolder1
...    ...  ...                                         ...

To get back the original SHA-1:

$git rev-parse data-1.0
f888c44e16f7811ba69a245adf35c4303cb0d4e7

What good will all this do you? Not much as-is. However, if you're willing to write your own scripts to reconstruct the contents of a tree, or to find the commits containing a tree, then it might be useful to you. (e.g. this SO answer could be adapted for such a purpose).

But like others have said, you'll probably have an easier time using a versioning/tagging model that's works better with Git, rather than trying to adapt your existing model. As already mentioned by shikjohari & others, if you want projects-within-a-project, which have their own versions, consider Git Submodules instead.

like image 165
tavnab Avatar answered Oct 03 '22 13:10

tavnab


You cannot.

In Git, a tag by design always applies to the repository as a whole (just like commits and branches). This is unlike in Subversion, where a tag (being just a copy) can be applied to a subtree.

BTW: Tagging a subtree is usually discouraged even in Subversion, because it can quickly become confusing just which part of the tree was tagged. Most sources I know (for example Version Control with Subversion recommend to always tag by copying trunk.

About your problem:

Usually, separate projects should get separate Git repositories. "Seperate" in this context usually means that you might want to branch / tag separately.

If you do not / cannot do that, the best option is probably to use some tag prefix, and call all tags myproj-1.0, myproj-1.1 etc.

like image 35
sleske Avatar answered Oct 03 '22 12:10

sleske