仅在 Git 中切换子目录的分支
我正在开发一个包含两个主分支和两个主文件夹的 Git 存储库。两个分支之间的这些文件夹之一非常相似,另一个则非常不同。这意味着,即使我只处理公共文件夹,由于另一个文件夹需要所有更新,两个分支之间的切换也会很慢。
是否可以切换 Git 存储库中子文件夹的分支,而不切换存储库其余部分的分支?在 Subversion 中,我可以在任何子文件夹上使用 svn switch
,一切都会正常工作™,但我还没有在 Git 中找到等效项。
理想情况下,当我在两个分支共用的文件夹中工作时,我希望能够切换该文件夹中的分支,提交到任一分支,并执行所有其他有用的 Git 功能,而无需等待另一个文件夹或多或少被完全重写。
让事情变得复杂的是,我正在使用 git svn ;我提到的两个主要分支是主干和底层 Subversion 存储库中的一个分支。
I'm working on a Git repository with two main branches and two main folders. One of these folders is very similar between the two branches, the other is very different. This means that, even if I'm just working on the common folder, switching between the two branches is slow due to all the updates required to the other folder.
Is it possible to switch the branch of a subfolder in a Git repository without switching the branch of the rest of the repository? In Subversion, I could just use svn switch
on any subfolder, and everything would Just Work™, but I haven't found an equivalent in Git.
Ideally, when I'm working in the folder that's common to the two branches, I'd like to be able to switch branches in that folder, make commits to either branch, and do all the other useful Git goodness, without having to wait for the other folder to be more-or-less completely rewritten.
Just to complicate matters, I'm using git svn
; the two main branches I mention are the trunk and a branch in the underlying Subversion repository.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 git 中使用称为“稀疏结帐”的东西来完成此操作。首先,您必须在项目中启用此功能:
然后,您必须设置要签出的目录。在编辑器中打开 .git/info/sparse-checkout ,并列出您想要限制签出的目录(每个目录都在单独的行上)。然后运行以下命令:
您可以在稍后的某个时间禁用 SpareCheckout 以将行为恢复正常。
You can do this in git with something called "sparse checkout". First, you have to enable this on your project:
Then, you have to set up the directories you want to check out. Open
.git/info/sparse-checkout
in an editor, and list the directories you want to limit the checkout to (each on a separate line). Then run this command:You can disable sparecheckout at some later point to return the behavior to normal.
我找到了一个新的解决方案:
git-new-workdir
。它不允许我切换子文件夹,但它确实让我有单独的工作目录,我可以在这些目录之间切换,而不是使用“切换”操作。它解决了根本问题:我可以为每个主分支拥有一个工作目录,只要我想要,就可以在它们之间进行交换,无需任何成本,并且这两个工作目录共享同一个 Git 存储库,因此我不需要两个单独的克隆,因为Ben Lee 在对他的评论中建议回答。
为了在我的 (Cygwin) 系统上获取脚本,我需要下载并安装 Git 源代码;它位于
contrib
目录中。I've found a new solution to this:
git-new-workdir
. It doesn't let me switch a subfolder, but it does let me have separate working directories which I can switch between instead of using a "switch" operation.It solves the underlying problem: I can have one working directory for each of the main branches, swap between them whenever I want with no cost, and the two working directories share the same Git repository, so I don't need two separate clones as Ben Lee suggested in the comments to his answer.
To get at the script on my (Cygwin) system, I needed to download and install the Git source code; it's in the
contrib
directory there.如果您确实希望能够做到这一点,Git 中的唯一方法是将子文件夹设置为子模块 http://book.git-scm.com/5_submodules.html
我猜 SVN 允许这样做的原因是因为它在每个子文件夹中放置单独的 .svn 文件来跟踪当前状态。
If you really wanted to be able to do that, the only way in Git would be to set up the subfolder as a submodule http://book.git-scm.com/5_submodules.html
I guess the reason SVN allows this is because it places separate .svn files in each subfolder which keep track of the current state.
我认为这是不可能的。 Git 是一个内容跟踪器,可以跟踪内容更改,而不是文件或文件夹。
作为一种解决方法,您可以使用 git-svn 在一个 Git 存储库中获取 SVN 主干,并在另一个 Git 存储库中获取 SVN 分支。
I don't think it's possible. Git is a content tracker and tracks content changes, not files or folders.
As a workaround you could use git-svn to fetch your SVN trunk in one Git repository and the SVN branch in another.