Git错误:'upstream'似乎不是一个git仓库。

74

我对Git非常陌生,所以在正确使用它方面遇到了一些问题。以下是我的情况。

我有一个主存储库的分支,并将其克隆到本地。其中,我有两个不同修复的分支。

当我提交时,在git-we中发现我有3次提交,但落后于约20次提交。

我知道我的fork与主分支不同步。我还需要合并这两个分支吗?(或者我应该这样做吗?)

我用Windows客户端进行同步。但好像我没有按照Git的方式操作。因此,我尝试按照https://help.github.com/articles/syncing-a-fork/中描述的步骤操作,但在Windows上给我一个错误。

 $>git fetch upstream
 fatal: 'upstream' does not appear to be a git repository
 fatal: Could not read from remote repository.

 Please make sure you have the correct access rights
 and the repository exists.

我有点困惑,请帮我列出在这种情况下应该遵循的步骤。

@HuStmpHrrr

抱歉在评论中添加细节,我会修改问题以便更易读。

通过执行以下命令:

  $>git remote  returning two values
  acme-development (which the name of my actual/main repository, from where I forked) and
  origin

添加更多信息。


在进行一次fetch操作后,我尝试将origin/master合并到我的master分支上。 请查看以下截图: 图片描述

但是,如果我登录到GitHub在线账户,情况就不同了。

图片描述

我的git客户端显示本地仓库已经是最新的。但是,在线git却说我们落后了42个提交,领先了7个提交。



2
你是怎么进行克隆的?我猜你指的是 git fetch origin。如果你定义了上游,那么仅仅使用 git fetch 就行了。或者大多数人喜欢使用 git pull - Jason Hu
我认为你需要向我们展示 git remote - Jason Hu
$>git remoteacme-development origin - kallada
你有两个远程仓库,acme-developmentorigin。我猜是 origin。那就执行 git fetch origin - Jason Hu
6个回答

100

你提供的链接所指向的文章(虽然自你提问以来可能已经发生了变化)开头提到了https://help.github.com/articles/configuring-a-remote-for-a-fork/。如果你完成了这个步骤,你将会拥有一个名为 upstream 的新远程仓库,它将指向你 fork 的原始仓库,并且 git fetch upstream 将会生效。


57
该文章中唯一必要的命令是git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git,在执行上述命令后可以继续进行git fetch upstream - Ng Sek Long
@NgSekLong 谢谢,这有助于解决 upstream 问题。 - CybeX

20

所以有几点需要澄清:

  • Upstream 是远程存储库(或存储库)的概念名称。大多数项目只有一个上游存储库。

  • 每个项目的 name 上游存储库名称可能不同,但是按照惯例使用 origin

话虽如此,我敢打赌你把你的上游存储库的名称和上游存储库的概念混淆了,并且你应该执行 git fetch origin。通过 git remote 来验证;使用列表中相应的名称。

但是,如果您只有一个上游存储库,则执行 git fetch 将达到相同的效果。


10

以下是我今天遵循的步骤来解决同样的问题

以下步骤将使您能够链接您的git分叉和原始存储库,以便您可以随时从原始存储库中拉取更改。前往github上存储库的页面,然后单击“fork”(如果您还没有这样做)。假设您从https://github.com/account/repo-name派生了一个名为repo-name的repo,那么您会在自己的帐户下获得该repo的副本(fork),类似于https://github.com/your-user-name/repo-name,然后在终端上运行下面的命令。

  • git clone https://github.com/your-user-name/repo-name.git
    
  • cd repo-name
    
  • git remote add upstream https://github.com/account/repo-name.git
    
  • git pull upstream master
    
  • git branch --set-up-stream-to=upstream/master
    
  • git pull
    

当您进行更改并希望将其集成到原始代码库中时,为了将更改添加到原始存储库中,您必须首先将更改推送到Github上的个人存储库,然后发送一个拉取请求,该请求将被验证并合并到代码库中。下面是步骤。

  • git add .
    
  • git commit -m "commit message"
    
  • git push origin master
    

然后访问您在github上的账户,并单击您的仓库名称。在页面上,您将看到创建拉取请求的链接。单击链接并创建拉取请求,然后由维护者审核并合并。


是的,这就是关键:git remote add upstream https://github.com/account/repo-name.git - titleLogin

6

只需在终端上运行以下命令即可:

$ git remote add upstream {your upstream repository link here}

运行此命令后:

git remote -v

这个命令将展示你的 GitHub 仓库路径以及上游仓库路径。

示例输出:

origin  https://github.com/ {Your Github username} / {repository name} (fetch)
origin  https://github.com/ {Your Github username} / {repository name} (push)

upstream    https://github.com/ {Upstream github username} / {repository name} (fetch)
upstream    https://github.com/ {Upstream github username} / {repository name} (push)


1

0

在远程分支中切换你的分支

git remote add upstream https://github.com/repo/original.git

每次当你想要更新你的主分支时,执行以下操作(你必须位于主分支并应用此命令):
git fetch upstream && git fetch upstream --prune && git rebase upstream/master && git push -f origin master

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接