smilebomb
smilebomb

Reputation: 5483

Changing Git remote URL updates fetch but not push

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 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:

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:

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?

Upvotes: 46

Views: 40110

Answers (2)

mafu
mafu

Reputation: 32650

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.

[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.

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:

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.

Upvotes: 10

running.t
running.t

Reputation: 5709

From git-remote manual:

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

Upvotes: 76

Related Questions