Detaching a directory to be modified as a branch using Git
Let's say my project is laid out like this:
MyProject/ βββ Examples β βββ ExamplesA β βββ ExamplesB β βββ ExamplesC βββ TheLibrary
So, there is this awesome library that I developed and some use cases that I would like to distribute along with the library.
The point is that the library and only this library must be checked out in the TFS repository. At the same time, my boss asked me to keep a copy of the project, as it can be used later.
The directory MyProject
is a Git repository, of course. I was thinking about branching a single directory (viz. MyProject/TheLibrary
), But how do I do that using Git? I read about git-subtree
, but I'm afraid I am lost somewhere in the jungle of endless pull
and push
es. Can anyone help me with this?
Ideally, I would like to have two branches: master
with the whole project in it and tfs
with the contents of the folder MyProject/TheLibrary
. Then I created development branches and merged them into both of these branches above. Is this possible or something similar with Git?
source to share
OK, this is how I did it.
I split
ted TheLibrary
into a branch TheLibrary
using
git subtree split -P TheLibrary -b TheLibrary
and then I made a separate branch to store the copy TheLibrary
in TFS or wherever.
git branch TheLibrary-tfs TheLibrary
So, I just clone
d the repository into my TFS workspace and checked out the branch TheLibrary-tfs
.
Whenever I make a TFS specific change, I just commit
and push
it and it is saved in my local folder outside the TFS repository.
Whenever I need to make an unrelated TFS change, I do it in my local folder on the branch master
, and then push
with
git subtree push -P TheLibrary . TheLibrary
so the change is propagated to the branch TheLibrary
. Then I merge the changes into a branch TheLibrary-tfs
. Finally, I just go to the TFS workspace and pull
changes from my local folder. Difficult but works and seems to be exactly what I need.
source to share
If you only want to push the latest version TheLibrary
to a new branch, you can do it this way (I'm assuming you're on the master):
$ git rm -rf Examples
$ git symbolic-ref HEAD refs/heads/tfs
$ git commit -m 'New Branch for TheLibrary'
now go back to master and delete TheLibrary
$ git checkout master
$ git rm -rf TheLibrary
$ git commit -m 'TheLibrary moved to branch tfs'
The result will look like this:
Repository View Working Directory View
o-----o-----o-----o MyProject/
^ βββ Examples
| βββ ExamplesA
master βββ ExamplesB
βββ ExamplesC
o MyProject/
^ βββ TheLibrary
|
tfs
So what's going on ...
-
git rm -rf Examples
removeExamples
and its subdirectories from your working directory and index (staging area) -
git symbolic-ref HEAD refs/heads/tfs
point an anchor pointHEAD
to a new branch calledtfs
-
git commit -m 'New Branch for TheLibrary'
Sets the index (now only containsTheLibrary
) as the new branch committfs
. -
git checkout master
back to main -
git rm -rf TheLibrary
removeTheLibrary
from working directory and index. -
git commit -m 'TheLibrary moved to branch tfs'
make a new commit on the master that no longer containsTheLibrary
, and a commit message pointing to the new location.
source to share