Checking out Remote Branch into New Branch

Goal: git checkout -b newBranchName remotes/branchForRemote/main to actually go into newBranchName

Hey all,

I started with an empty git repo, which I cloned from github.com

Then, git remote add [urlFromAnOpenSourceProject]

Then, git checkout -b newBranchName remotes/openSourceRepo/main.

The command above only pulled the remotes/openSourceRepo/main into my own main branch. How do I instead make it pull into another of my branches (not my main branch)?

Thanks!

Note: I have a workaround listed below but I would still like to know how to avoid needing to use this workaround.

My Workaround: Create/switch to a different branch, and then run the following commands

brew install git-lfs (if you need git-lfs for your open source project)
git clone [My repo url]
git remote add opensource [Open source Url]
git fetch opensource
git lfs install (if the open source project uses it.)
git checkout -b branchToPullinto
git checkout -b nameThatWillNotBeUsed remotes/opensource/main
git commit -m “I pulled from opensource into the branchToPullinto branch”
git push -> Will throw an error
git push --set-upstream origin anotherNewBranch
The Command Prompt/Terminal will say:
Branch 'anotherNewBranch' set up to track remote branch 'anotherNewBranch' from 'origin'.
Now, run “git push”
0

1 Answer

What you're doing is about right. You just forgot a git fetch --all or git fetch <open source remote name you gave it in remote add>. So basically:

  1. git clone to your empty repo>
  2. git remote add opensource <link to open source project>
  3. git fetch opensource
  4. git checkout -b <any name you want> opensource/master

Without git fetch, your git doesn't know which files/branches/tags your open source remote has in the first place.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest