【问题标题】:How to update my branch with the latest changes from upstream如何使用上游的最新更改更新我的分支
【发布时间】:2015-05-20 18:26:45
【问题描述】:

这个问题可能听起来微不足道,但我真的不知道如何正确地做...我正在做一个开源项目,源代码在 Github 上,所以我已经分叉了那个 repo,添加了一个“远程上游”指向原始存储库,并且还在我的分叉存储库中创建了一个分支并进行了一些更改。它看起来像这样:

$git remote -v
origin    https://github.com/my_github_account/project_name (fetch)
origin    https://github.com/my_github_account/project_name (push)
upstream  https://github.com/project_name/repo_name (fetch)
upstream  https://github.com/project_name/repo_name (push)

$git branch
  master
* test_branch

在对我的“test_branch”进行一些更改之后,上游也有一些新的更改,所以我想从上游获取最新的更改,将它们合并到我的 test_branch 中,看看是否一切正常。我试过了:

git pull upstream master

为此目的可以正常工作,但它也创建了一个“新提交”,因为当我向项目创建拉取请求时也会显示提交消息,如果我以这种方式频繁拉取上游更改,那么会很多

Merge branch 'master' of https://github.com/project_name/repo_name into test_branch

消息显示,这对其他人来说有点混乱,我也没有看到其他人的拉取请求包含此类消息......(或者也许有一些方法可以摆脱这些消息?)

我也google了一下,试过了:

git pull --rebase upstream master

然后做:

git push origin test_branch

但后来我收到一些错误,例如:

 ! [rejected]        test_branch -> test_branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/my_github_account/project_name'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

嗯...我不清楚这意味着什么...也许我的做法不对...那么从上游获取最新更改的程序通常是什么,整合/将它们合并到我的分支中?

提前致谢!

【问题讨论】:

    标签: github merge branch pull


    【解决方案1】:

    第二种方法是正确的(在更新的master 之上重播您的test_branch)。

    但它也会产生重写你的 test_branch 历史的预期副作用,你必须强制将它推送到你的 fork

    git push --force origin test_branch
    

    因为它是你的 fork 并且没有其他人在 origin/test_branch 上工作,所以没关系,即使你正在进行 PR,Pull Request 也会检测到强制推送并相应地更新自身。

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 2021-07-31
      • 2021-04-29
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多