Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove only one remote origin when there are several?

Tags:

git

Looking at the git documentation I see that I can delete a remote origin using a command like this:

git remote remove origin

But what if I have multiple origins and I only want to delete one of them? In my case it looks something like this:

git remote -v 
origin ssh://host1/path/to/project.git (fetch)
origin ssh://host1/path/to/project.git  (push)
origin ssh://host2/path/to/project.git  (push)

I only want to delete the last origin. How do I do this?

like image 930
Michael Avatar asked Jan 21 '26 06:01

Michael


1 Answers

TL;DR

git config --delete remote.origin.push ssh://host2/path/to/project.git

See below for why that's slightly sloppy.

Long

There's only one remote named origin here.

What it has is three URLs.

A remote usually has two URLs: one fetch and one push. This is true even if you didn't set a particular push URL, as git remote will use the fetch URL as the push URL. In this case, though, someone has configured two push URLs.

(If you configure one push URL, the one that you used to see—copied from the fetch automatically—goes away, leaving you with just the one push URL you just configured. So someone must have configured two.)

To delete one particular setting, use:

  • git config, or
  • your editor (possibly via git config; see below)

To use git config to delete a specific setting:

git config --delete remote.origin.push 'ssh://host2/path/to/project\.git'

The backslash (and quotes) here is to make sure that this matches only that specific setting. The last argument is a regular expression, and in regular expressions, the letter . matches any character. So without the backslash this would delete, for instance, ssh://host2/path/to/projectAgit if it's in there.

Since host names usually have dots, you might want to backslash those. However, all of this is just being extra-careful: using . to match . is usually fine because you probably don't have ssh://host2/path/to/projectAgit, ssh://host2/path/to/projectBgit, and ssh://host2/path/to/project.git in there. (In fact, we know you don't as you showed us some output that says so.) So:

git config --delete remote.origin.push ssh://host2/path/to/project.git

will actually suffice.

You can also run git config --edit, which invokes your chosen editor—the one you use for commit messages and for git rebase -i—on your .git/config file. I typically do this to fix typos, for instance.

like image 120
torek Avatar answered Jan 23 '26 11:01

torek