Egor Tensin Egor Tensin - 3 months ago 7
Git Question

Detaching a directory to be modified as a branch elsewere using Git

Let's say my project is laid out as follows:

MyProject/
├── Examples
│   ├── ExamplesA
│   ├── ExamplesB
│   └── ExamplesC
└── TheLibrary


So there is this awesome library I've developed and a few usage examples which I would like to be distributed along with the library.

The thing is, the library, and that library alone has to be checked into a TFS repository. At the same time, my boss asked me to keep a copy of the project as it is so that it can be used later on.

The
MyProject
directory is a Git repository, of course. I was thinking about branching a single directory (namely,
MyProject/TheLibrary
), but how do I do that using Git? I've read about
git-subtree
, but I'm afraid I got lost somewhere in the jungle of endless
pull
s and
push
es. Could somebody help me with that?

Ideally, I would love to have two branches:
master
with the whole project in it, and
tfs
with the contents of the
MyProject/TheLibrary
folder. Then I would create development branches and merge them to both of these branches above. Is that or something similar possible using Git?

Answer

OK, this is how I did it.

I splitted TheLibrary to the TheLibrary branch using

git subtree split -P TheLibrary -b TheLibrary

and then I made a separate branch for maintaing a copy of TheLibrary in TFS or wherever.

git branch TheLibrary-tfs TheLibrary

So then I simply cloned the repository into my TFS workspace and checked out the TheLibrary-tfs branch.

Whenever I make TFS-specific change, I simply commit and push it and it's saved in my local folder outside of the TFS repository.

Whenever I need to make a TFS-unrelated change, I do it in my local folder in the master branch and then push using

git subtree push -P TheLibrary . TheLibrary

so that the change is propagated to the TheLibrary branch. Then I merge the changes to the TheLibrary-tfs branch. Finally, I simply navigate to my TFS workspace and pull the changes from my local folder. Complicated, but works and appears to be just what I needed.