Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a branch for one subdirectory only

Tags:

git

This is a conceptual question, rather than "how do I?" question.

I'm helping migrate a very big SVN repository to a Git repository for a team. I'm simplifying a bit, but let's assume that their current repo structure is like this:

   --trunk
     |-Proj1
     |-Proj2
     |-Proj3
   --branch
     |-Proj1
       |- branch1

etc.

They (owner team of the repo) insist, that it should go to a a single Git repository, not each project in its own. Let's assume that I cannot persuade them otherwise. (I suspect they'll come around, when they start using Git and understand this is impractical.)

Each of ProjN in SVN is getting branched independently of others.

My question about Git, when I branch, can I branch a subfolder of a repo (that is just Proj1) not the whole repo? My gut feeling is that you are always supposed to branch git at the top level. Am I wrong?

like image 303
Andrew Savinykh Avatar asked Sep 14 '16 20:09

Andrew Savinykh


2 Answers

You can't branch a subfolder. Branches always apply to the whole worktree.

I suppose git submodules are what are you looking for. You can think of them like "nested git repositories".

like image 137
fracz Avatar answered Sep 30 '22 11:09

fracz


Well, you can branch a sub-folder. Whether or not you should is a different story.

For the use-case of splitting a sub-folder into its own repo:

https://help.github.com/articles/splitting-a-subfolder-out-into-a-new-repository/

git filter-branch --prune-empty --subdirectory-filter FOLDER-NAME  BRANCH-NAME 

For example:

git checkout master
ls foosub/
git checkout -b foobranch
git filter-branch --prune-empty --subdirectory-filter foosub/ foobranch    

I don't know that this is what's intended for the scenario in the question, but since the search terms I used lead me here first I figure I might as well give the answer that someone with similar search terms that I used may be looking for.

I think you can even do some fancy stuff to make it track... but that's not my use case and it probably shouldn't be yours (the reader) either.

Bonus: merge into a fresh new repo

mkdir ./new-project
pushd ./new-project
git init
git pull ../original-project foobranch
#git remote add origin <new-project-url>
#git push
like image 36
coolaj86 Avatar answered Sep 30 '22 12:09

coolaj86