Switching branch for a subdirectory only in Git
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 commo开发者_运维问答n 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.
You can do this in git with something called "sparse checkout". First, you have to enable this on your project:
git config core.sparsecheckout true
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:
git read-tree -m -u HEAD
You can disable sparecheckout at some later point to return the behavior to normal.
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.
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.
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.
精彩评论