Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Git remote URL updates fetch but not push

Tags:

git

ssh

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?

like image 862
smilebomb Avatar asked Jan 26 '17 15:01

smilebomb


People also ask

Which git command changes an existing remote repository URL?

You can change a Git repository's remote URL using the git remote set-url command.

How do I override git remote origin?

Ist Step:- Change the current working directory to your local project. 2nd Step:- List your existing remotes in order to get the name of the remote you want to change. Change your remote's URL from HTTPS to SSH with the git remote set-url command. 4th Step:- Now Verify that the remote URL has changed.


2 Answers

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 
like image 112
running.t Avatar answered Sep 20 '22 15:09

running.t


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.

like image 32
mafu Avatar answered Sep 16 '22 15:09

mafu