dileep chidurala
dileep chidurala

Reputation: 55

How to create a new branch in new location as continuation from other branch

I have a git repo with branch parent_branch with some commits in /temp/project/. Now I have a new local folder /temp/other_folder/project/ with some files which I need to push to the same above repo with new branch child_branch that emerged from above parent_branch.

One brute-force way I can do it is to clone the git repo with parent_branch checkout at some temp location and then remove all the files except .git and move all the files from new local folder there and then do

git checkout -b child_branch
git add and commit and push

But I want to do it without moving files here and there and do it with git bash from the new local location /temp/other_folder/project/ only.

# /temp/other_folder/project/
git init
git add remote origin <git_url>
somehow create a child_branch from the parent branch
 A -> B -> c ... parent_branch
       \-> D ... child_branch
git add and commit and push to origin/child_branch

Upvotes: 1

Views: 191

Answers (1)

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59983

Sounds like multiple worktrees might be the thing for you.

From the documentation of git-worktree:

A git repository can support multiple working trees, allowing you to check out more than one branch at a time.

In this case, you could create a separate worktree in a subdirectory of your repo and associate it with the new child_branch branch:

git worktree add -b child_branch ./subdirectory parent_branch

This will checkout the parent_branch in the specified subdirectory and associate it with the new child_branch branch.

At this point, you can simply copy your new files into the subdirectory without having to clone the entire repository into a new location.

You might want to read up on how to manage multiple worktrees in the documentation.

Upvotes: 2

Related Questions