Tuesday, September 14, 2021

Adding a branch from another repo in Git

This should be something rather rare, but for some reason I keep needing to do it very often: I want to add a new, unrelated branch into an existing repository, and this branch comes from another repository.

While needing to do this yet again today, I found a rather elegant solution here:

git switch --orphan NEW_BRANCH
git pull FORK_URL FORK_BRANCH

Note that FORK_URL can be a folder path, including a relative path.

It works remarkably well and clean. Props to the guy who shared this.

Then, to push the new branch to the remote:

git push -u origin NEW_BRANCH

No comments:

Post a Comment