更改 Git 远程 URL 更新获取但不推送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41876631/
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
Changing Git remote URL updates fetch but not push
提问by smilebomb
I am attempting to change the remote URL of my origin branch in Git. All I want to change is the SSH port. First, listing my remote origins gives me this:
我试图在 Git 中更改我的源分支的远程 URL。我想更改的只是 SSH 端口。首先,列出我的远程来源给了我这个:
git remote -v
origin [email protected]:package/name.git (fetch)
origin [email protected]:package/name.git (push)
Then, I run the set-url
command to change my origin URL:
然后,我运行set-url
命令来更改我的原始 URL:
git remote set-url origin ssh://[email protected]:XX/package/name.git (XX is my port #)
Now, I can fetch without issue, but pushing my branch to origin doesn't work, because the push URL didn't change. Listing my remotes again I get this:
现在,我可以毫无问题地获取,但是将我的分支推送到原点不起作用,因为推送 URL 没有改变。再次列出我的遥控器,我得到了这个:
git remote -v
origin ssh://[email protected]:XX/package/name.git (fetch)
origin [email protected]:package/name.git (push)
Why did my set-url
command only change the fetch URL?
为什么我的set-url
命令只更改了获取 URL?
回答by running.t
From git-remote
manual:
从git-remote
手册:
set-url
Changes URL remote points to. Sets first URL remote points to matching regex <oldurl> (first URL if no <oldurl> is given) to <newurl>. If <oldurl> doesn't match any URL,
error occurs and nothing is changed.
With --push, push URLs are manipulated instead of fetch URLs.
So you should additionally execute:
所以你应该另外执行:
git remote set-url --push origin ssh://[email protected]:XX/package/name.git
回答by mafu
As long as the config file for the repo in question contains an entry for the push URL, set-url
will only update the fetch URL by default.
只要有问题的 repo 的配置文件包含推送 URL 的条目set-url
,默认情况下只会更新获取 URL。
[remote "origin"]
url = fetch.git
fetch = ...
pushurl = push.git
As the answer of running.t explains, you can use set-url --push
to change this entry. However, you will have to keep doing this every time the URL changes.
正如 running.t 的答案所解释的,您可以使用set-url --push
更改此条目。但是,每次 URL 更改时,您都必须继续执行此操作。
To restore the default behavior of set-url
(which changes both URLs at once), just delete the entry from the config. Or delete it using set-url --delete
:
要恢复set-url
(同时更改两个 URL)的默认行为,只需从配置中删除该条目即可。或使用删除它set-url --delete
:
git remote set-url --delete --push origin push.git
As for why a repository would ever contain a separate push url without you adding it: Some git clients like Sourcetree "helpfully" do this.
至于为什么存储库会在没有您添加的情况下包含单独的推送 url:像 Sourcetree 这样的一些 git 客户端“有帮助”地做到了这一点。