将远程分支签出到新分支

Checking Out Remote Branch into New Branch

目标:git checkout -b newBranchName remotes/branchForRemote/main 真正进入 newBranchName

大家好,

我从一个空的 git 仓库开始,我从 github.com

克隆了它

然后,git remote add [urlFromAnOpenSourceProject]

然后,git checkout -b newBranchName remotes/openSourceRepo/main.

上面的命令只是将 remotes/openSourceRepo/main 拉到我自己的主分支中。我该如何改为将其拉入我的另一个分支(而不是我的主要分支)?

谢谢!

注意:我有下面列出的解决方法,但我仍然想知道如何避免需要使用此解决方法。

我的解决方法:Create/switch到不同的分支,然后运行下面的命令

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”

你所做的几乎是正确的。您只是忘记了 git fetch --allgit fetch <open source remote name you gave it in remote add>。 所以基本上:

  1. git clone https://github.com/<url 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

没有 git fetch,您的 git 根本不知道您的开源遥控器有哪个 files/branches/tags。