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?

+3


source to share


2 answers


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.

0


source


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

    remove Examples

    and its subdirectories from your working directory and index (staging area)
  • git symbolic-ref HEAD refs/heads/tfs

    point an anchor point HEAD

    to a new branch calledtfs

  • git commit -m 'New Branch for TheLibrary'

    Sets the index (now only contains TheLibrary

    ) as the new branch commit tfs

    .
  • git checkout master

    back to main
  • git rm -rf TheLibrary

    remove TheLibrary

    from working directory and index.
  • git commit -m 'TheLibrary moved to branch tfs'

    make a new commit on the master that no longer contains TheLibrary

    , and a commit message pointing to the new location.
0


source







All Articles