Swagath
Swagath

Reputation: 1

How do i create a branch from an existing folder in an existing branch in a git repository?

How do i create a branch from an existing folder present in another branch in a git repository? For ex:

Existing Repository name: A_repo

Branch Name: 1- Branch

Folders: Develop, Maintenance, Production, Non-Production

Now i need to create Develop branch from Develop folder(present in A_Repo) under new repository B_Repo.

Thanks & Regards,

Swagath

Upvotes: 0

Views: 3317

Answers (1)

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

It sounds like you maybe have a legacy repo with folders per environment (likely from TFS or something like that?) and are trying to migrate to a more git-friendly structure with branches per environment instead?

If so, and if you plan to use git, it's probably a good idea. But be aware that sharing changes between the two going forward wouldn't be straightforward - so this works best if you're able to migrate everything to the new structure and then dispose of the old repo.

You can do this using the subdirectory-filter option of git filter-branch (https://git-scm.com/docs/git-filter-branch). (On current versions of git, you will get a warning that they recommend switching to a different tool called git filter-repo; I haven't yet learned about this new tool, and this is a simple enough operation using filter-branch, but you might choose to look up the filter-repo docs instead.)

For example you could create a new B-repo containing the history of 1-Branch by

$ git clone --single-branch -b 1-branch A-repo ./B-repo
$ cd B-repo
$ git remote remove origin

and then create the develop branch by

$ git checkout -b develop
$ git filter-branch --subdirectory-filter develop --prune-empty -- develop

If need be, you could repeat this procedure to create branches for each environment. You would have to do a little clean-up in between filter-branch runs to remove backup refs it creates, as it won't do a new run if there's a previous backup.

$ git update-ref -d refs/original/refs/heads/develop
$ git checkout 1-branch
$ git checkout -b production
$ git filter-branch --subdirectory-filter production --prune-empty -- production

and so on.

Once you've created whatever new branches you need, you would remove 1-Branch from your new repo.

$ git branch -D 1-branch

If you find that excessive space is being taken up by the now-defunct 1-Branch data (which is still in the repo database) you could clean it up in a couple ways. Just pushing the new branches to a remote will probably suffice, but I don't see anything in the docs to guarantee that. Similarly, you can just re-clone the new repo and probably only the new branches' data will be copied, but again I'm not sure that's guaranteed behavior.

To explicitly force a cleanup you could delete the 1-Branch reflog (and any last original/* refs) and then run git gc. Since this clone was created just now for creating the new branches, it doesn't have any valuable reflogs, so you can safely delete them all.

$ rm -rf .git/refs/original
$ rm -rf .git/logs
$ git gc --prune=now --force --aggressive

Upvotes: 1

Related Questions