如何更改git仓库的fetch URL。

24

我有一个 GitHub 个人资料,并且我将某人的存储库 fork 到我的账户中。并在本地使用 git clone 命令克隆了这个存储库。

git clone https://github.com/my-github-page/repo-name

现在,一些时间已经过去,原始repo所有者已经更新了他的repo,但是我的repo还没有更新。所以,如果我想用git pull将他的更改合并到我的repo中,但是我也想将本地的更改推送到我的repo中。

简单来说,如果我执行git remote show origin,我会得到这个结果:

[root@localhost xxx]# git remote show origin
* remote origin
  Fetch URL: https://github.com/my-github-profile/xxx
  Push  URL: https://github.com/my-github-profile/xxx
  HEAD branch: master
  Remote branches:
    1.0          tracked
    master       tracked
    system_tests tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

现在我想把FETCH的URL更改为仓库原始的src。因为原始版本已经更新,但是我的分支落后了。

2个回答

42

您可以尝试:

git remote set-url origin /original/repo
git remote set-url --push origin /your/fork

这样,只有抓取引用到原始代码库的URL。

当然,另一种更传统的方法是声明另一个远程仓库,正如我在“GitHub上的origin和upstream之间有什么区别?”中详细说明的那样。


1
@PeterHaddad 是的,考虑到日期,这似乎是合适的。 - VonC
2
工作正常,但在我看来相当丑陋。我真的希望有 git remote set-url --fetch 选项,很遗憾它不存在。 - The Godfather
2
@TheGodfather 这个问题在 https://public-inbox.org/git/xmqqftwp47nb.fsf@gitster-ct.c.googlers.com/T/ 中被讨论过,“remote: add --fetch option to git remote set-url”,但是遭到了否决。 - VonC

1

参考以下问题:github, update forked project

请执行以下操作:

git remote add upstream <url of the repo you cloned from>

在此从上游拉取。这将把所有远程更改拉入您的本地副本。
如果将来需要再次从远程拉取更改,则可以再次从上游拉取,这次无需再添加上游url。
建议阅读:同步分叉

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