git-工作树和共享存储库之间的区别? [英] git - difference between worktrees and shared repositories?

查看:80
本文介绍了git-工作树和共享存储库之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git worktree命令与使用--shared开关多次克隆本地存储库之间有什么区别?

What is the difference between the git worktree command and sharing a repository locally by cloning it multiple times with the --shared switch?

我的目标是只将一个相对较大的存储库克隆到开发人员的计算机磁盘一次,并使用该存储库在多个分支上进行开发.我们无法使用git工作树,因为我们使用Eclipse和 Eclipse目前不支持工作树.

My objective is to clone a relatively large repository to the disk of developers' machines once only and use that one repository for development on multiple branches. We cannot use git worktree because we use Eclipse and Eclipse does not support worktrees at the moment.

我尝试过的并且似乎起作用的是以下内容:

What I've tried and what seems to work is the following:

  1. 从GitLab克隆存储库一次:git clone ssh://host/repo.git repository-master
  2. 再次克隆它,而无需将任何内容检出为共享存储库.这只会在第一个存储库中创建一些对打包文件的参考文件,而不会浪费任何磁盘空间: git clone --shared repository-master/ repository-branch1/
  3. 将GitLab存储库添加为共享存储库repository-branch1的远程存储:git remote add gitlab ssh://host/repo.git
  4. 获取数据git fetch
  5. 在第二个克隆repository-branch1中,将所需的分支检出到新的本地分支中:git checkout -b branch1 gitlab/branch1
  1. Clone the repository once from GitLab: git clone ssh://host/repo.git repository-master
  2. Clone it again without checking out anything as a shared repository. This just creates some reference files to the pack files in the first repository, not wasting any disk space: git clone --shared repository-master/ repository-branch1/
  3. Add the GitLab repository as a remote to the shared repository repository-branch1: git remote add gitlab ssh://host/repo.git
  4. Fetch the data git fetch
  5. Check out the desired branch into a new local branch in the second clone repository-branch1: git checkout -b branch1 gitlab/branch1

这似乎可以解决所有问题:

This seems to solve all problems:

  1. 有多个分支同时签出.
  2. 您可以将提交推送到两个分支.
  3. 您可以将项目导入Eclipse,并且Eclipse将识别该存储库.
  4. 磁盘上只有一个克隆的存储库.

我在这里错过了什么吗?当然,除了方便之外,肯定还有其他原因需要使用worktree命令吗?

Am I missing something here? Surely, there must be a reason to use the worktree command over this other than convenience?

推荐答案

使用--shared(或某些基于路径名的克隆中隐含的--shared)的克隆是一个单独的存储库.通过使用硬链接 1 (操作系统必须支持),您可以完全安全地节省磁盘空间 2 ,因为Git不会覆盖任何现有的数据,它只会添加新数据,或者在相对罕见的情况下,取消链接文件,这现在是无害的,因为这只会减少使用该文件的Git存储库的数量.

A clone that uses --shared (or the implied --shared from some pathname-based clones) is a separate repository. By using hard links1 (which must be supported by your operating system), you get all the goodness of saving disk space,2 with total safety because Git never overwrites any existing data, it only adds new data or, in relatively rare cases, unlinks the file, which is now harmless since that just decreases the number of Git repositories using the file.

git worktree add制成的工作树不是 一个单独的存储库.您在该工作树中执行 到该工作树中您不在 上的任何分支或标签的所有操作,都会立即被所有其他工作树看到共享存储库.工作树具有其自己的HEAD(通常附加到分支名称,但可以选择是分离的),其自己的索引,当然还有其自己的工作树.几乎所有其他内容都是共享的(还有其他一些神奇的私有引用,例如git bisect,但是例如,所有存储都在工作树之间共享).

A work-tree, made with git worktree add, is not a separate repository. Everything you do in that work-tree, to any branch or tag that you're not on in that work-tree, is immediately seen by all the other work-trees that share the repository. The work-tree has its own HEAD (usually attached to a branch name but optionally detached), its own index, and of course its own work-tree. Nearly everything else is shared (there's a few other magically-private references, such as for git bisect, but any stashes, for instance, are shared across work-trees).

您现有的流程很好;只需注意,磁盘空间共享最终将按照脚注2中的说明降级.

Your existing process is fine; just note that the disk-space-sharing will eventually degrade as described in footnote 2.

1 与某些基础文件对象建立硬链接会添加一个新名称,并增加链接数.链接计数始终代表文件的名称数量.上述取消链接操作是名称删除,而不是文件删除;仅当最后一个引用消失时,文件本身才会消失.

1Making a hard link to some underlying file object adds a new name, and increments the link count. The link count always represents the number of names for the file. The unlink operation described above is a name-deletion, rather than a file-deletion; the file itself goes away only when the last reference goes away.

2 随着时间的推移,这种节省的性能会降低.这两个单独的存储库最初共享所有基础存储,但随后在某个时候,一个存储库决定打包松散对象,或重新打包现有包,从而断开某些链接.实际上,另一个存储库现在拥有自己的私有副本.最终,所有链接可能都已断开,因此每个存储库都是真正独立的.存储库继续工作,但是不再节省空间.

2This savings sort of degrades over time. The two separate repositories initially share all the underlying storage, but then at some point, one repository decides to pack loose objects, or repack an existing pack, breaking some the links. The other repository now has, in essence, its own private copy. Eventually, all the links may have been severed, so that each repository is truly independent. The repositories continue to work but there is no space-savings any more.

这篇关于git-工作树和共享存储库之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆