存储库子文件夹的 Git 标记

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

Git tag for a subfolder of a repository

gitsvngit-svngit-tag

提问by BuZZ-dEE

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

我使用 Git 导入一个 SVN 存储库。然后我创建了自己的项目作为存储库中的子文件夹。

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

我将 SVN 存储库与 Git-SVN 一起使用。我的工作流程是:

  1. git commit -am "message"
  2. git svn rebase
  3. git svn dcommit.
  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.

现在我想用 标记我的项目git tag -a RC1 -m 'Release Candidate 1',但我只希望我的项目获得标记。

How can I do that?

我怎样才能做到这一点?

回答by tavnab

TL;DR version

TL;DR 版本

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.

如果您知道树的 SHA-1,则可以标记特定目录(又名树),但通常不这样做并且使用该标记做有用的事情并不容易。

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.

Git 中的每个对象都有一个唯一的 SHA-1。最常见的是,SHA-1 指的是提交,但它们也可以指 Blob(文件内容)和树(目录结构和文件名/文件权限映射)。您可以在Git 对象文档 中阅读它。

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

例如,假设我在我的存储库中的特定目录中。我可以运行git ls-tree HEAD以获取路径中的文件/目录列表以及它们的 SHA-1:

$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 datadirectory above):

然后我可以标记这些树之一(假设data上面的目录):

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

该标签现在是该 SHA-1 的别名,我可以在接受树的 SHA-1 的任何地方使用它:

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

要取回原始的 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 answercould be adapted for such a purpose).

这一切对你有什么好处?没有那么多。但是,如果您愿意编写自己的脚本来重建树的内容,或者查找包含树的提交,那么它可能对您有用。(例如,这个 SO 答案可以适用于这样的目的)。

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

但就像其他人所说的那样,您可能会更轻松地使用更适合 Git 的版本控制/标记模型,而不是尝试调整现有模型。正如 shikjohari 和其他人已经提到的那样,如果您想要具有自己版本的项目内项目,请考虑使用Git 子模块

回答by sleske

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.

在 Git 中,设计的标签始终适用于整个存储库(就像提交和分支一样)。这与 Subversion 不同,在 Subversion 中,标签(只是一个副本)可以应用于子树。

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 Subversionrecommend to always tag by copying trunk.

顺便说一句:即使在 Subversion 中通常也不鼓励标记子树,因为它很快就会混淆树的哪个部分被标记。我知道的大多数来源(例如使用 Subversion 的版本控制建议始终通过复制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.

通常,单独的项目应该获得单独的 Git 存储库。在这种情况下,“分离”通常意味着您可能希望单独分支/标记。

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.1etc.

如果不这样做/不能做到这一点,最好的办法可能是使用一些标签的前缀,并呼吁所有标签myproj-1.0myproj-1.1等等。

回答by me_and

This isn't possible with Git. A Git tag is a pointer to a specific commit, whereas a Subversion tag is a copy of any folder in the Subversion repository. The concept of tagging a single folder in Subversion doesn't carry over very well into Git.

这在 Git 中是不可能的。Git 标签是指向特定提交的指针,而 Subversion 标签是 Subversion 存储库中任何文件夹的副本。在 Subversion 中标记单个文件夹的概念并没有很好地延续到 Git 中。

The problem is that your initial setup doesn't match Git's branching model. The way to do this in a Git-friendly manner would be to have a branch set up for your project, and then to tag commits on that branch.

问题是您的初始设置与 Git 的分支模型不匹配。以对 Git 友好的方式执行此操作的方法是为您的项目设置一个分支,然后在该分支上标记提交。

You've a couple of options:

你有几个选择:

  • Tag the entire repository at a given point using git svn tag. Run git help svnfor instructions on using this command.

  • Tag the directory using regular Subversion commands. This doesn't need to involve downloading a Subversion working copy, since you can just run svn copy {URL to your project on the repository} {URL to your tag directory}, but you will need to install Subversion.

  • Start a new Git clone of your Subversion repository in a whole new directory. Specify your project folder as the trunk URL, rather than the actual trunk. Git-svn will then treat that directory as your main branch and allow you to tag and copy it through Subversion.

  • 使用git svn tag.在给定点标记整个存储库。运行git help svn以获取有关使用此命令的说明。

  • 使用常规 Subversion 命令标记目录。这不需要涉及下载 Subversion 工作副本,因为您可以直接运行svn copy {URL to your project on the repository} {URL to your tag directory},但您需要安装 Subversion。

  • 在一个全新的目录中启动一个新的 Subversion 存储库的 Git 克隆。将您的项目文件夹指定为中继 URL,而不是实际的中继。然后 Git-svn 会将该目录视为您的主分支,并允许您通过 Subversion 标记和复制它。

回答by shikjohari

I found myself in the similiar problem when I had to move a very large project to Git. I found that the team did not handle the SVN properly and did the taggings from wherever they wanted to. So there were tags for individual projects and folders - which is highly discouraged.

当我不得不将一个非常大的项目转移到 Git 时,我发现自己遇到了类似的问题。我发现团队没有正确处理 SVN,而是从他们想要的任何地方进行标记。所以有个别项目和文件夹的标签 - 这是非常不鼓励的。

If you are planning to move to Git, you should keep it as how things work in Git and keep it clean. I would suggest to create seperate repositories of frequently changing projects. Also you can make use of submodules if you find your project dependent on other projects.

如果你打算迁移到 Git,你应该保持它在 Git 中的工作方式并保持干净。我建议为经常更改的项目创建单独的存储库。如果您发现您的项目依赖于其他项目,您也可以使用子模块。