如何更改我的工作站(本地)存储库将信息推送到的远程(托管)存储库?

How can I change the remote (hosted) repo to which my workstation (local) repo pushes information?

我从 GitHub 分叉了一个项目。

我希望能够从我的工作站推送到我的 Gitlab,即

git push

到目前为止,这是我在工作站上得到的:

$ git remote -v
origin  https://github.com/OWASP/railsgoat.git (fetch)
origin  https://github.com/OWASP/railsgoat.git (push) 

我认为它会成功,因为我的目的是:

$ git remote -v
origin  https://github.com/OWASP/railsgoat.git (fetch)
origin  https://gitlab.com/myName/railsgoat.git (push)

我确实尝试查看了文档,但是到目前为止的解决方案仍然让我望而却步:

git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <url>

您应该使用

添加一个新的远程主机
git remote add newHostName  https://gitlab.com/myName/railsgoat.git 

然后是

git push newHostName branchname

事实上,为同一个遥控器设置单独的获取和推送 URL 相当尴尬(您可以这样做,但这不是实现您要求的典型方式)。通常你会添加另一个遥控器,然后推送到那个遥控器。

git remote set-url origin https://gitlab.com/myName/railsgoat.git
git remote add upstream https://github.com/OWASP/railsgoat.git

那么你将使用

git fetch upstream

从上游 repo 获取更改和

git push origin

推到你的叉子上。

名称 upstreamorigin 并非一成不变,尽管这些是社区使用的典型名称。您可以命名 upstream 其他名称,它只是一个标识符。 origin 是您最初从中克隆的远程名称。

惯例是对你控制的东西(你的分叉)使用 origin,对你不控制的东西(中央仓库)使用 upstream

然后,下一个概念是给定分支的 "upstream branch"(命名混乱)。

基本上,git push 需要知道你想从哪个分支推送到哪个分支(git 在这种情况下过于灵活,一开始可能会让人望而生畏)。

假设你想从 master 推送到 origin/master,并且你总是希望 git push 将来(在 master 时)做同样的事情,你会做:

git push -u origin master

(-u--set-upstream 的 shorthand,您稍后可以通过 --set-upstream-to 对其进行编辑,这使得 git 记住您推送的分支to right now 是将来要使用的默认分支。

完成后,您以后可以使用

git push

另外注意,如果出于某种原因你还在使用git 1.x,建议执行这个

git config --global push.default simple

改变 git push 的行为(阅读更多 here


要完全按照您的要求去做,您会做的

git remote set-url --push origin https://gitlab.com/myName/railsgoat.git