git Github:将上游分支导入 fork

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4410091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:38:15  来源:igfitidea点击:

Github: Import upstream branch into fork

gitgithub

提问by poke

I have a fork (origin) from a project (upstream) on github. Now the upstream project has added a new branch, I want to import into my fork. How do I do that?

我有一个origin来自upstreamgithub项目 ( )的 fork ( ) 。现在上游项目添加了一个新分支,我想导入到我的fork中。我怎么做?

I tried checking out the remote and creating a branch on top of that, but that configures the branch the way that git pushis trying to push to the upstream:

我尝试检查远程并在其上创建一个分支,但这会以git push尝试推送到的方式配置分支upstream

git checkout upstream/branch
git checkout -b branch

edit

编辑

Maybe that wasn't clear, but I want to add the branch to my local repository, so I can push it to origin(my fork) via git push. Because upstream repositories are usually read-only and you fork it to contribute.

也许这还不清楚,但我想将该分支添加到我的本地存储库,以便我可以origin通过git push. 因为上游存储库通常是只读的,您可以将其分叉以进行贡献。

So I basically want to checkout a non-existent branch on originwhose contents will be pulled in from upstream.

所以我基本上想签出一个不存在的分支,origin其内容将从upstream.

回答by urschrei

  1. Make sure you've pulled the new upstreambranch into your local repo:

    • First, ensure your working tree is clean(commit/stash/revert any changes)
    • Then, git fetch upstreamto retrieve the new upstream branch
  2. Create and switch to a local version of the new upstream branch(newbranch):

    • git checkout -b newbranch upstream/newbranch
  3. When you're ready to push the new branch to origin:

    • git push -u origin newbranch
  1. 确保您已将新的上游分支拉入您的本地仓库

    • 首先,确保您的工作树是干净的(提交/存储/恢复任何更改)
    • 然后,git fetch upstream检索新的上游分支
  2. 创建并切换到新上游分支( newbranch)的本地版本

    • git checkout -b newbranch upstream/newbranch
  3. 当您准备好将新分支推送到origin 时

    • git push -u origin newbranch

The -uswitch sets up tracking to the specified remote (in this example, origin)

-u开关组向上跟踪到指定的远程(在本例中,origin

回答by Svenito

I would use

我会用

git checkout -b <new_branch> upstream/<new_branch>

回答by Ian Will

I had trouble with this too, and google took me here. The solutions didn't work however. My problem was that when i added my upstream, it set up my git config to only fetch master, rather than all branches. e.g. It looked like this

我也遇到了这个问题,谷歌把我带到了这里。然而,这些解决方案并没有奏效。我的问题是,当我添加上游时,它设置了我的 git 配置以只获取 master,而不是所有分支。例如它看起来像这样

[remote "somebody"]
        url = [email protected]:somebodys/repo.git
        fetch = +refs/heads/master:refs/remotes/upstream/master

Editing .git/config as follows fixed my problem

如下编辑 .git/config 解决了我的问题

[remote "somebody"]
        url = [email protected]:somebodys/repo.git
        fetch = +refs/heads/*:refs/remotes/upstream/*

回答by Eneko Alonso

The following steps worked well for me (assuming the upstream branch name is branch):

以下步骤对我来说效果很好(假设上游分支名称是branch):

$ git fetch upstream
$ git checkout branch
$ git push origin

回答by okor

I had a slightly more complicated scenario where I already had an upstreamdefined in my fork (from the canonical repo) but needed to checkout a branch from a different fork. To get that done, the process is slightly different. Here's the config I ended up with:

我有一个稍微复杂的场景,我已经upstream在我的 fork 中定义了一个(来自规范 repo),但需要从不同的 fork 签出一个分支。要完成这项工作,过程略有不同。这是我最终得到的配置:

[remote "origin"]
url = [email protected]:<your_user/org>/<repo>.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
[remote "upstream"]
url = [email protected]:<upstream_user>/<repo>.git
fetch = +refs/heads/*:refs/remotes/upstream/*
[remote "other_user"]
url = [email protected]:<other_user>/<repo>.git
fetch = +refs/heads/*:refs/remotes/<other_user>/*

Now you can checkout a branch from <other_user>fork as well.

现在你也可以从<other_user>fork检出一个分支。

git fetch <other_user> <branch>
git checkout -b <branch> <other_user>/<branch>

That will give you a local branch which is derived from the fork.

这将为您提供一个源自分叉的本地分支。

To push that local branch I had to be specific with my push command.

要推送该本地分支,我必须使用我的推送命令进行具体说明。

git push origin <branch>

回答by troelskn

--track?

--track?

git branch --track branch upstream/branch