Home > Net >  Is is possible to clone a repository with a single branch under a different name
Is is possible to clone a repository with a single branch under a different name

Time:06-22

I'm using

git clone --bare -b main --single-branch <internal-repo-url> internal
cd internal && git push --mirror <private-fork-url>

To create a private fork of an internal repository. In this way I can pull in updates from the origin repository through another remote that is not 'origin':

git remote add internal <internal-repo-url>
git pull internal main

Is there a way for me to create a bare clone with the 'main' branch under a different name? My motivation for doing this is wanting to have the private fork's own 'main' branch distinguished from the internal repo

CodePudding user response:

The right sequence would be:

git clone --bare -b main --single-branch <internal-repo-url> internal
git remote rename origin internal
git remote add origin <private-fork-url>
git branch -u origin/main
git push --mirror

That way, your local main branch is tied to the upstream private-fork-url/main, while you still have internal/main

CodePudding user response:

My regular workflow when forking on GitHub is this:

  1. Use the regular GitHub forking functionality.
  2. Locally, I check out from my GitHub repository. The origin remote then points to my repository (created in step 1) on GitHub.
  3. Configure an additional remote upstream to the original repository.

This allows me to use git rebase my-feature-branch upstream/master in order to incorporate latest changes from there.

That said, keep in mind that there are three similar branches now: main (local), origin/main and upstream/main. Similarly, you can also specify where you push your work when you push and you can pick the target repository, the local reference and the remote branch independently.

  • Related